使用CSS在img标签上添加渐变

44

我想在 <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个回答

86

使用 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>


4
你可以通过将渐变应用于 div 元素的 :after:before 伪元素来避免负 z-index。 - pstenstrm
@pstenstrm 是的,那是真的。这两种解决方案都是有效和可用的。 - web-tiki
谢谢!我已经修改了您的代码,像这样:.pickgradient { display: inline-block; background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0, 0, 0, 0)), color-stop(70%, rgba(0, 0, 0, 0.5)));} .pickgradient img{ position:relative; z-index:-1; display:block;} 并使用<span>而不是<div>。现在它可以工作了! - donutello

27

针对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%);
}
   <img src="http://i.imgur.com/HDssntn.jpg" />


2

我建议您将 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演示示例:

.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;
}

0
尝试在相关图片上放置一个 div,并将渐变效果应用于该 div 而非图片本身。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接