How to Create an Overlay Using CSS:
There are many techniques for creating overlays. In this snippet, we’re going to show how to create
an overlay using CSS properties.
One of the ways of creating an overlay is by absolutely positioning an HTML element on the page.
We create a div in the markup then it is position absolutely with the position property after it we give
the div high z-index value to make it on top of all other elements on the page with the z-index
property. We give higher z-index to the next div which is opened on the top of our overlay.
1. Create HTML ¶
Create two <div> elements with the classes "overlay" and "modal".
Create another <div> element and write some text in it.
2. Create CSS ¶
Set the position to absolute and set the z-index to 10.
Specify the width, height, top and left properties.
<h2>Creating an Overlay with an Absolutely Positioned Element</h2>
<div class="overlay"></div>
<div class="modal">Lorem Ipsum is simply dummy text </div>
<div>Lorem Ipsum is simply dummy text…</div>
.overlay{
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 2/8
Style the "modal" div.
Set the position to fixed and z-index to 11, 1px higher than the overlay layer.
Specify margin-top and margin-left, border-radius and line-height of the class.
Also, style the body setting the font, color, min-height.
Set the position to "relative" for the overlay to extend when you scroll.
So, we’ve created an overlay. let’s see the outcome!
Example
z-index: 10;
}
.modal {
width: 300px;
height: 200px;
line-height: 200px;
position: fixed;
top: 40%;
left: 50%;
margin-top: -100px;
margin-left: -150px;
background-color: #8ebf42;
border-radius: 40px;
text-align: center;
z-index: 11;
}
body{
position: relative;
padding: 20px;
margin:0;
min-height:100%;
font-family: 'Open Sans', sans-serif;
background: #8ebf42;
color: #eeeeee;
}
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 3/8
We can also create an overlay using ::before and ::after selectors.
We can have the same styles and considerations used in the previous example by styling the
::before and ::after pseudo-classes on the body.
Example
Try it Yourself »
five centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s, when an unknown printer took a galley of type
and scrambled it to make a type specimen book. It has survived not only
five centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s, when an unknown printer took a galley of type
and scrambled it to make a type specimen book. It has survived not only
five centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of
Lorem Ipsum.
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>overlay div</title>
<style>
body{
position: relative;
padding: 20px;
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 4/8
But using pseudo-class instead is not supported in Safari and Safari Mobile.
css effect scroll modal
Try it Yourself »
margin:0;
min-height:100%;
font-family: 'Open Sans', sans-serif;
background: #8ebf42;
color: #eeeeee;
}
body :after {
content: "";
display: block;
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: 10;
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 5/8
Related Resources ¶
How to Create a Modal Dialog Box with CSS and JavaScript
CSS position Property
CSS z-index Property
Do you find this helpful?
Yes No
Related articles
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 6/8
Quizzes
HTML Basic
CSS Basic
Javascript Basic
PHP basic
ES6 Basic
TypeScript Basic
Angular Basic
React Basic
Sass Basic
Vue.js Basic
Git Basic
SQL Basic
Snippets
How To JavaScript
How To NodeJs
How To Linux
How To AngularJs
How To PHP
How To HTML
How To CSS
How To Symfony
How To Google Maps
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 7/8
How To Git
How To Apache
How To Java
Our Books
Learn HTML
Learn CSS
Learn Git
Learn Javascript
Learn PHP
Our Tools
Password Generator
HTML Editor
HTML Encoder
CSS Maker
Color Picker
Base 64
Code Diff
Geometric Images
String Functions
String Length Calculator
String to MD5 Hash Generator
String to Sha256 Hash Generator
String Reverse
URL Encoder
URL Decoder
Base 64 Encoder
Base 64 Decoder
Extra Spaces Remover
String to Lowercase Converter
String to Uppercase Converter
3/5/2020 How to Create an Overlay Using CSS
https://www.w3docs.com/snippets/css/how-to-create-an-overlay-using-css.html 8/8
String Word Count
Empty Lines Remover
HTML Tags Remover
Binary Data to Hex Converter
Hex Encoded Binary String Decoder
Rot13 to Text Converter
String to Binary Converter
Duplicate Lines Remover
About Us Privacy Policy for W3Docs
Follow Us
Company © W3docs. All rights reserved.

0 Comments