使用css在img标签上的渐变

时间:2014-05-29 14:26:36

标签: html css angularjs css3 gradient

我想在<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 ">

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

4 个答案:

答案 0 :(得分:38)

使用z-index:

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

&#13;
&#13;
.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;
}
&#13;
<div class="pickgradient">
  <img src="http://i.imgur.com/HDssntn.jpg" />
</div>
&#13;
&#13;
&#13;


使用伪元素:

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

&#13;
&#13;
.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;
}
&#13;
<div class="pickgradient">
  <img src="http://i.imgur.com/HDssntn.jpg" />
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

对于2020年,蒙版图像效果很好。它可以在现代浏览器中运行(当前在许多浏览器中不是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%);
}
   <img src="http://i.imgur.com/HDssntn.jpg" />

答案 2 :(得分:0)

尝试在问题图像上放置div并将渐变放在div而不是图像上。

答案 3 :(得分:0)

我建议您将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;
}