Reputation: 527
I'm learning some basic css and am trying to make an overlay without having to set the height on the overlay div. (I want the overlay to cover just the background image in the .bg-img
class)
But the problem is that the content after the overlay is behind the overlay. https://jsfiddle.net/vxbu07z9/10/
Can someone point out what I'm missing?
html:
.bg-img {
background-image: url('https://placeimg.com/600/600?t=3');
background-attachment: fixed;
background-size: cover;
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.6);
}
.content {
color: #fff;
}
<div class="bg-img">
<div class="overlay"></div>
<div class="content">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi, labore!</div>
<ul>
<li>List Item 1</li>
<li>List Item 1</li>
<li>List Item 1</li>
<li>List Item 1</li>
</ul>
</div>
</div>
Upvotes: 0
Views: 1416
Reputation: 115046
It's just a stacking order issue.
position:relative
to the .content
divto reset the order
.bg-img {
background-image: url('https://placeimg.com/600/600?t=3');
background-attachment: fixed;
background-size: cover;
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.6);
}
.content {
color: #fff;
position: relative;
}
<div class="bg-img">
<div class="overlay">
</div>
<div class="content">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi, labore!</div>
<ul>
<li>List Item 1</li>
<li>List Item 1</li>
<li>List Item 1</li>
<li>List Item 1</li>
</ul>
</div>
or more simply..
z-index
of the overlay.bg-img {
background-image: url('https://placeimg.com/600/600?t=3');
background-attachment: fixed;
background-size: cover;
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.6);
z-index: -1;
}
.content {
color: #fff;
}
<div class="bg-img">
<div class="overlay"></div>
<div class="content">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi, labore!</div>
<ul>
<li>List Item 1</li>
<li>List Item 1</li>
<li>List Item 1</li>
<li>List Item 1</li>
</ul>
</div>
</div>
Upvotes: 4