Create an overlay without creating a new element

Hi, Anower Jahan Shofol here. I am a senior frontend developer from Bangladesh. Right now I am working remotely as a remote developer and doing freelancing. You can hire or consult me to join your team to accomplish your business needs.
Do you want an overlay over your image, video, background or whatever? Then you can do that by just adding some CSS and you won't need to add another HTML element for that!
How? Let's see-
We can make it with the power of ::before/::after selectors.
In the example below, we will make an overlay only by adding styles to ::before selector.
div {
background-image: url("https://images.pexels.com/photos/585759/pexels-photo-585759.jpeg");
background-size: cover;
background-position: center;
min-height: 100vh;
width: 100vw;
}
div::before {
content: "";
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3);
}
How's it working?
It's working based on the positioning of CSS.
When we're not declaring a position for the only
<div>of the example, by default, it's takingrelativeposition.So, now we are selecting the
::beforeselector and placing it over the<div>by declaringabsoluteposition.This is starting from the top corner and the very left of the div because we set
left:0andtop:0.Also, we are setting its width and height to 100% which will enable it to take the whole space over the mentioned div.
Check this from another perspective!
That's all folks, for now!
Follow me on other platforms-
Twitter Handle
LinkedIn Handle
If you like the article, you can Buy me a coffee ☕



