使用css在img标签上的渐变

Ali*_*roz 17 html css gradient css3 angularjs

我想在<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)));
}
Run Code Online (Sandbox Code Playgroud)

<img src={{value.angitem.image}} class="pickgradient ">
Run Code Online (Sandbox Code Playgroud)

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

web*_*iki 43

使用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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="pickgradient">
  <img src="http://i.imgur.com/HDssntn.jpg" />
</div>
Run Code Online (Sandbox Code Playgroud)


使用伪元素:

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

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="pickgradient">
  <img src="http://i.imgur.com/HDssntn.jpg" />
</div>
Run Code Online (Sandbox Code Playgroud)

  • 通过将渐变应用于div的`:after`或`:before`伪元素,可以避免负z-index. (4认同)

TLa*_*add 11

对于 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%);
}
Run Code Online (Sandbox Code Playgroud)
   <img src="http://i.imgur.com/HDssntn.jpg" />
Run Code Online (Sandbox Code Playgroud)