Tutorialspoint.dev

How to overlay one div over another div using CSS

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:

  • z-index
  • position

Example 1:

<!DOCTYPE html>
<html>
    <head>
        <title>overlay div</title>
        <style>
            .geeks {
                position: absolute;
                height: 250px;
                width: 500px;
                background-color:green;
            }
            .gfg {
                color:white;
                padding-top:80px;
                padding-left:70px;
                font-size:40px;
                font-weight:bold;
            }
            .g4g {
                padding-left:95px;
            }
        </style>
    </head>
    <body>
        <div class = "geeks">
            <div class = "gfg">GeeksforGeeks</div>
            <div class = "g4g">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:

<!DOCTYPE html>
<html>
    <head>
        <title>overlay div</title>
        <style>
            .geeks {
                position: absolute;
                height: 250px;
                width: 500px;
                background-color:green;
            }
            .gfg {
                color:white;
                padding-top:80px;
                padding-left:70px;
                font-size:40px;
                font-weight:bold;
            }
            .g4g {
                padding-left:90px;
            }
            .gfg:hover {
                z-index: -1;
                opacity: 0.5;
                font-size:20px;
                text-align:center;
                transform-style:all;
                transition-duration: 1s;
            }
            .g4g:hover {
                z-index: -1;
                opacity: 0.3;
                font-size:40px;
                text-align:center;
                transform-style:all;
                transition-duration: 1s;
            }
        </style>
    </head>
    <body>
        <div class = "geeks">
            <div class = "gfg">GeeksforGeeks</div>
            <div class = "g4g">A computer science portal for geeks</div>
        </div>
    </body>
</html>                    

Output:
Before hovering:

After hovering:



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter