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:

Recommended Posts:
- Convert a String to a Singly Linked List
- Error Handling in Perl
- Tricks in Windows 10 | Set -1
- Perl | File I/O Functions
- How to reset a form using jQuery with .reset() method?
- Deploy cloud function on Google Cloud Platform
- Bajaj Electricals Interview Experience
- Perl | Line Action Commands in a Debugger
- 8 Ways to Shipwreck Your Next Website Design
- Difference between LAN and WLAN
- PHP | Regular Expressions
- What is the equivalent of ngShow and ngHide in Angular 2+?
- How to append Angular styles in footer ?
- Get and Post method using Fetch API
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



