领先的免费Web技术教程,涵盖HTML到ASP.NET

网站首页 > 知识剖析 正文

利用CSS filter的属性给img修改颜色

nixiaole 2024-11-20 19:52:11 知识剖析 15 ℃

效果图:

所有代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 150px;
        height: 50px;
        border: 1px solid black;
        display: flex;
        overflow: hidden;
        margin-bottom: 20px;
      }

      .box-filter img {
        /* 
        filter中的drop-shadow,该滤镜可以给图片非透明区域添加投影 
        -80是因为, 原始图片移动到屏幕外面80px的位置, 它的投影也会跟着走,
        因此需要设置-80px, 把投影放到最初的位置
        */
        filter: drop-shadow(-80px 0 red);
        /* 然后把原始图片移除box外面, 由于设置了超出部分隐藏, 因此看不见 */
        transform: translate(80px);
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>原始图片</span>
      <img src="imgs/error-icon.png" alt="" />
    </div>
    <div class="box box-filter">
      <span>使用filter换色</span>
      <img src="imgs/error-icon.png" alt="" />
    </div>
  </body>
</html>

Tags:

最近发表
标签列表