Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient over img tag using css

I want to place a gradient over an <img> tag. src attribute of the tag is angular-item. For example: <img src={{value.angitem.image}}>

I've tried to make css class:

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

and

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

but it doesn't work. What should I do?

like image 772
donutello Avatar asked May 29 '14 14:05

donutello


People also ask

How do you add a gradient to a IMG tag?

With z-index : You may use a container and put the gradient on that container. Then use a negative z-index to position image behind the gradient.

How do I combine a background image and CSS3 gradient on the same element?

You can combine a background-image and CSS3 gradient on the same element by using several backgrounds. In our example below, we set the height and width of our <div> and add a background. Then, we set the background image fallback using the “url” value of the background-image property.


2 Answers

With z-index :

You may use a container and put the gradient on that container. Then use a negative z-index to position image behind the gradient.

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

With a pseudo element :

As commented, you can also use a pseudo element with the gradient and absolute positioning to put the gradient over the image :

.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>
like image 132
web-tiki Avatar answered Sep 20 '22 11:09

web-tiki


For 2020, mask-image can work well. It works in modern browsers (not IE, -webkit- prefix in many browsers currently). 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" />
like image 29
TLadd Avatar answered Sep 19 '22 11:09

TLadd