Skip to content
Related Articles

Related Articles

How to overlay one div over another div using CSS

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 13 Jun, 2022
View Discussion
Improve Article
Save Article

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:

Before hovering:

 

After hovering:

 

 

Supported Browsers are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!