My goal is to have a div with any background, which then uses a pseudo element to create a transparent white overlay, thus "lightening" the background of the div. The "overlay" must be UNDER the contents of the div, though. So, in the following example:
<div class="container">
<div class="content">
<h1>Hello, World</h1>
</div>
</div>
.container {
background-color: red;
width: 500px;
height: 500px;
position: relative;
}
.content {
background-color: blue;
width: 250px;
}
.container::before {
content:"";
display: block;
height: 100%;
position: absolute;
top: 0;
left: 0;
width: 100%;
z-index: 1;
background-color: rgba(255, 255, 255, .8);
}
The .content
div
should not be "underneath" the white overlay, aka .container::before
.
I would prefer not having to use z-index
on .content
, but I can if that is the only solution.
End goal: The red should be covered while the text and blue are not.
JS fiddle: http://jsfiddle.net/1c5j9n4x/
We can use the property to provide an overlay to the background image. We can use the background-blend-mode property after setting the background image. For example, create a div in HTML. In CSS, set the background image using the url() function and set the no-repeat and fixed values in the background property.
You can use a pseudo element to create the overlay. Show activity on this post. background-image takes multiple values. so a combination of just 1 color linear-gradient and css blend modes will do the trick.
You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).
If the pseudo element has a z-index
, then you would need to position the .content
element and add a z-index
value to establish a stacking context.
Updated Example
.content {
background-color: blue;
width: 250px;
position: relative;
z-index: 1;
}
..you could also remove the z-index
from the pseudo element and then merely position the .content
element. In doing so, none of the elements need a z-index
. The reason this works is because the :before
pseudo element is essentially a previous sibling element. Thus, the succeeding .content
element is positioned on top.
Alternative Example
.content {
background-color: blue;
width: 250px;
position: relative;
}
.container::before {
content:"";
display: block;
height: 100%;
position: absolute;
top: 0;
left: 0;
width: 100%;
background-color: rgba(255, 255, 255, .8);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With