Open In App

How to overlay one div over another div using CSS

Improve
Improve
Like Article
Like
Save
Share
Report

Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CSS properties:

Example 1:

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>overlay div</title>
    <style>
        .geeks {
            position: absolute;
            justify-content: center;
            text-align: center;
            align-items: center;
            height: 250px;
            width: 500px;
            background-color: rgb(36, 168, 36);
        }
 
        .gfg1 {
            margin-top: 80px;
            color: white;
            font-size: 40px;
            font-weight: bold;
        }     
          .gfg2{
              font-size: 20px;
          }
    </style>
</head>
 
<body>
   
    <div class="geeks">
        <div class="gfg1">
          GeeksforGeeks
          </div>
        <div class="gfg2">
          A computer science portal for geeks
          </div>
    </div>
</body>
 
</html>


Output:

Note: Customize the overlay effects by adding more CSS to the page to make it look more elegant.

Example 2:

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>overlay div</title>
    <style>
        .geeks {
            position: absolute;
            justify-content: center;
            text-align: center;
            align-items: center;
            height: 250px;
            width: 500px;
            background-color: rgb(36, 168, 36);
              font-size: 20px;
        }
 
        .gfg1 {
            margin-top: 80px;
            color: white;
            font-size: 40px;
            font-weight: bold;
        }
 
        .gfg1:hover {
            z-index: -1;
            opacity: 0.5;
            font-size: 20px;
            text-align: center;
            transform-style: all;
            transition-duration: 1s;
        }
 
        .gfg2:hover {
            z-index: -1;
            font-size: 40px;
            text-align: center;
            transform-style: all;
            transition-duration: 1s;
        }
    </style>
</head>
 
<body>
   
    <div class="geeks">
        <div class="gfg1">GeeksforGeeks</div>
        <div class="gfg2">A computer science portal for geeks</div>
    </div>
   
</body>
 
</html>


Output:

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

CSS is the foundation of web pages and is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.



Last Updated : 04 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads