【问题标题】:Gradient over img tag using css使用css在img标签上渐变
【发布时间】:2014-07-19 02:26:14
【问题描述】:

我想在<img> 标记上放置一个渐变。 src 标签的属性是 angular-item。例如: <img src={{value.angitem.image}}>

我已经尝试过创建 css 类:

.pickgradient {
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0)), color-stop(100%,rgba(0,0,0,0.65)));
}

<img src={{value.angitem.image}} class="pickgradient ">

但它不起作用。我该怎么办?

【问题讨论】:

    标签: html angularjs css gradient


    【解决方案1】:

    使用 z-index :

    您可以使用容器并将渐变放在该容器上。然后使用负 z-index 将 图像置于渐变之后

    .pickgradient {
      display:inline-block;
      background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.65) 100%); /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Opera 11.10+ */
      background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* IE10+ */
      background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* W3C */
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
    }
    
    img{
      position:relative;
      z-index:-1;
      display:block;
      height:200px; width:auto;
    }
    <div class="pickgradient">
      <img src="http://i.imgur.com/HDssntn.jpg" />
    </div>

    带有伪元素:

    正如评论,您还可以使用具有渐变和绝对定位的伪元素将渐变放在图像上

    .pickgradient{
      position:relative;
      display:inline-block;
    }
    .pickgradient:after {
      content:'';
      position:absolute;
      left:0; top:0;
      width:100%; height:100%;
      display:inline-block;
      background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.65) 100%); /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* Opera 11.10+ */
      background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* IE10+ */
      background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%); /* W3C */
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
    }
    
    img{
      display:block;
      height:200px;width:auto;
    }
    <div class="pickgradient">
      <img src="http://i.imgur.com/HDssntn.jpg" />
    </div>

    【讨论】:

    • 您可以通过将渐变应用到 div 的 :after:before 伪元素来避免负 z-index。
    • @pstenstrm 是的,这是真的。两种解决方案都有效且可用。
    • 谢谢!我已经像这样修改了您的代码: .pickgradient { display: inline-block;背景:-webkit-渐变(线性,左上,左下,颜色停止(0%,rgba(0, 0, 0, 0)),颜色停止(70%,rgba(0, 0, 0, 0.5 )));} .pickgradient img{ 位置:相对; z-索引:-1; display:block;} 并使用 而不是
      。现在可以了!
    【解决方案2】:

    对于 2020 年,mask-image 可以很好地工作。它适用于现代浏览器(不是 IE,目前在许多浏览器中都是 -webkit- 前缀)。 https://caniuse.com/#feat=css-masks

    img {
       height: 200px;
       width: auto;
       mask-image: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%);
       -webkit-mask-image: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%);
    }
       &lt;img src="http://i.imgur.com/HDssntn.jpg" /&gt;

    【讨论】:

      【解决方案3】:

      我建议您将background-color:black; 设置为您的容器,然后设置class img{opacity:0.4}。然后你会得到和你一样的效果

      backgroundImage:linear-gradient(rgba(0, 0, 0, 0.8),rgba(0, 0, 0, 0.8),rgba(0, 0, 0, 0.8),rgba(0, 0, 0, 0.8)),url(img_url))
      

      我在幻灯片上的示例:

      .Slide {
          position: relative;
          border: 1px solid blue;
          min-width: 100%;
          height: 100%;
          transition: 0.5s;
          background-color: rgb(0, 0, 0);
      }
      
      .Slide img{
          position: relative;
          border: 1px solid blue;
          min-width: 100%;
          height: 100%;
          transition: 0.5s;
          opacity: 0.4;
      }
      

      【讨论】:

        【解决方案4】:

        尝试在相关图像上放置div,并将渐变放置在 div 上而不是图像上。

        【讨论】:

          猜你喜欢
          • 2020-03-29
          • 1970-01-01
          • 2021-11-20
          • 2017-06-13
          • 2021-02-23
          • 1970-01-01
          • 2012-08-21
          • 2013-05-11
          • 1970-01-01
          相关资源
          最近更新 更多