The Wayback Machine - https://web.archive.org/web/20240118172047/https://www.geeksforgeeks.org/how-to-overlay-one-div-over-another-div-using-css/
Open In App
Related Articles

How to overlay one div over another div using CSS

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 04 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials