4

So I have an image. I need to put a color overlay rgba(56, 59, 64, 0.7) on top of this image.

HTML:

<div class="home">
   <img src="http://via.placeholder.com/350x150" />
</div>

CSS:

.home img {
  width: 100%;
  padding: 0;
  margin: 0;
}

.home img {
  width: 100%;
  padding: 0;
  margin: 0;
}
    
<div class="home">
   <img src="http://via.placeholder.com/350x150" />
</div>
Ali Almohsen
  • 1,311
  • 3
  • 13
  • 24

3 Answers3

11

Here you go

.home {

}

img {
  width: 100%;
  padding: 0;
  margin: 0;
  display:block;
}

.wrap {
  position:relative;
}

.wrap:before {
  content:"";
  position: absolute;
  top:0;
  left:0;
  height:100%;
  width:100%;
  background: rgba(0,0,0,0.5);
  z-index:999;
}
<div class="home">
<div class="wrap">
   <img src="http://via.placeholder.com/350x150" />
 </div>
</div>
RacoonOnMoon
  • 1,556
  • 14
  • 29
  • why add another div ? if you can use the `home` div ? – Mihai T Jun 21 '17 at 10:40
  • @MihaiT his assumption was correct, there will be some other content in home. Granted, both answers are correct, but Traver's was more spot on for my case. – Ali Almohsen Jun 21 '17 at 10:45
  • @AliAlmohsen well if that is the case, you should give that info in the question :). We don't ( at least i don't ) have a crystal ball to know what's going on in your html structure – Mihai T Jun 21 '17 at 10:46
5

You can use pseudo-elements like before and absolute position it on top of the image

Added a blue background color for example purposes , so you can see it better, but you can use any color with opacity

img {
  width: 100%;
  padding: 0;
  margin: 0;
}
.home {
 position:relative;
}
.home:before {
  content: "";
  width: 100%;
  height: 100%;
  position: absolute;
  background: rgba(0,0,255,0.5);
}
<div class="home">
  <img src="http://via.placeholder.com/350x150">
</div>
Mihai T
  • 17,254
  • 2
  • 23
  • 32
0

HTML

    <div class="home">
       <img src="mango.jpg" />
        <div class="overlay"></div>
    </div>

SCSS

.home {
    position: relative;
    img {
        max-width: 100%;
        padding: 0;
        margin: 0;
    }
    .overlay {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background-color: rgba(56, 59, 64, 0.7);
    }
}
Mr.Pandya
  • 1,899
  • 1
  • 13
  • 24