How to Create an Overlay Effect with CSS?
Overview
CSS as we all know is used to make websites look good. It has many tricks up its sleeve that enable us to do so. An overlay is one such thing. Overlay means covering the surface of an element. You must have encountered them when visiting a website. Many websites have a popup with some form, advertisement, etc, these are examples of an overlay.
In this article, you will learn how to create overlays in CSS.
Pre-requisites
To create an overlay in css you should have basic knowledge of
- CSS positions
That's it. Let's learn how to create CSS overlays
CSS overlay Syntax
Here is the syntax that will create a CSS overlay.
Let's move to a detailed break down of the syntax in the next section.
Basic Example
For an overlay css to work properly we need some JavaScript also. This example will explain to you how the HTML, CSS, and JavaScript can be linked together to create an overlay.
The example we will see below is illustrates the concept of overlay using a single HTML file that contains both JS and CSS, but we can separate them into different files and that will also work just fine.
First let's start with the HTML part:
Output

CSS
In place of position:fixed we can also use position:absolute both will work. With position absolute, we have to specify the top/left/right positions.
JavaScript
These two functions are responsible for showing and hiding the overlay css. The function changes the display property of the element.
So after combining HTML, CSS, and JavaScript your output will look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Overlay</title>
<style>
#overlay {
position: fixed;
display: none;
height: 100%;
width: 100%;
background-color: lightblue;
opacity: 50%;
cursor: pointer;
}
</style>
</head>
<body>
<div id="overlay" onclick="hideOverlay()"></div>
<h2>CLICK TO SEE OVERLAY</h2>
<button onclick="showOverlay()">Open Overlay</button>
<script>
function showOverlay() {
document.getElementById("overlay").style.display = "block";
}
function hideOverlay() {
document.getElementById("overlay").style.display = "none";
}
</script>
</body>
</html>
Output:

How to Create Different Overlay Effects
Fade Overlay Effect
In this effect, an overlay is displayed when we hover over the image.
HTML
CSS
.container { position: relative; width: 400px; } .image { display: block; width: 100%; height: auto; } /*Overlay that will show after hovering over container */ .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; height: 100%; width: 100%; opacity: 0; transition: 0.5s ease; background-color: #008cba; } /* This will trigger overlay after overing over container*/ .container:hover .overlay { opacity: 1; } /* Text over overlay*/ .projectedText { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); text-align: center; }
Output:

Image Overlay Slide
Four types of sliding effects can be created using a css overlay, these are top, bottom, left, and right.
Let's look at them one by one-
Slide in Overlay from top to bottom Here the overlay will appear from the top and will continue to the bottom.
HTML
CSS
.container { position: relative; width: 50%; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; bottom: 100%; left: 0; right: 0; background-color: lightblue; overflow: hidden; width: 100%; height:0; transition: .5s ease; } .container:hover .overlay { bottom: 0; height: 100%; }
Output:

Slide in Overlay from bottom to top
.container { position: relative; width: 50%; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; bottom: 0; left: 0; right: 0; background-color: lightblue; overflow: hidden; width: 100%; height: 0; transition: 0.5s ease; } .container:hover .overlay { height: 100%; }
Output:

Slide in Overlay from left to right HTML
CSS
.container { position: relative; width: 50%; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; bottom: 0; left: 0; right: 0; background-color: lightblue; overflow: hidden; width: 0; height: 100%; transition: 0.5s ease; } .container:hover .overlay { width: 100%; }
Output:

Slide in Overlay from right to left HTML
CSS
.container { position: relative; width: 50%; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; bottom: 0; left: 100%; right: 0; background-color: lightblue; overflow: hidden; width: 0; height: 100%; transition: 0.5s ease; } .container:hover .overlay { width: 100%; left: 0; }
Output:

Image Overlay title
HTML
CSS
.container { position: relative; width: 50%; max-width: 300px; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; bottom: 0; background: rgb(0, 0, 0); background: rgba(0, 0, 0, 0.5); color: #f1f1f1; width: 85%; transition: 0.5s ease; opacity: 0; color: white; font-size: 20px; padding: 20px; text-align: center; } .container:hover .overlay { opacity: 1; }
Output:

Image Overlay icon
For icon we are using Font-Awesome. To make that work we will insert this link tag in the <Head> section of the HTML
HTML
CSS
.container { position: relative; width: 100%; max-width: 400px; } .image { display: block; width: 100%; height: auto; } .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; height: 100%; width: 100%; opacity: 0; transition: 0.3s ease; background-color: lightblue; } .container:hover .overlay { opacity: 1; } .icon { color: white; font-size: 100px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); text-align: center; } .fa-user:hover { color: #eee; }
Output:

Conclusion
Till now you must have learned
- Overlay means covering the surface of an element.
- HTML, CSS, and JavaScript can be linked together to create an overlay.
- The CSS overlay can be triggered by elements when we hover over them or we could also make separate buttons to trigger overlay.
- We could also display text in the CSS overlay and make them slide as we show in the above examples.