How to overlay one div over another div using CSS Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like 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:z-indexpositionExample 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. Comment More infoAdvertise with us R RahulRanjan4 Follow Improve Article Tags : Technical Scripter Web Technologies CSS Technical Scripter 2018 CSS-Misc +1 More Similar Reads What is a clearfix? A clearfix is a way for an element to automatically clear or fix its elements so that it does not need to add additional markup. It is generally used in float layouts where elements are floated to be stacked horizontally. If the element is taller than the element containing it then use the overflow 3 min read Making a div Vertically Scrollable using CSS The overflow property in CSS controls how content that goes beyond an element's boundaries is handled. It can either hide the extra content or add scrollbars so users can scroll to see more without leaving the current view. This helps display large amounts of content, such as text, images, or tables 3 min read How to give a div tag 100% height of the browser window using CSS CSS allows to adjustment of the height of an element using the height property. While there are several units to specify the height of an element. Set the height of a <div> to 100% of its parent container with height: 100%, or use height: 100vh; for a full viewport height, ensuring the <div 3 min read Wildcard Selectors (*, ^ and $) in CSS for classes Wildcard selectors can be used with attribute selectors. The asterisk (*) matches any character, the caret (^) matches the start, and the dollar sign ($) matches the end of an attribute value. For example, .class* selects all elements with a class attribute containing "class" anywhere, .class^ selec 3 min read How to style a dropdown using CSS? We will learn how to style the dropdown list using CSS and explore its implementation through examples. Dropdown menus are commonly used to allow users to select an option from a predefined list. Styling them properly can enhance your website's user experience and visual appeal.What is a <select 3 min read Remove border from IFrame using CSS The <iframe> tag is used to embed another web page within a webpage. To remove the border from an <iframe>, you can use the frameBorder attribute in the <iframe> tag, setting its value to "0".Syntax:<iframe frameBorder="value"></iframe>Note:The frameBorder attribute is 2 min read How to Hide Scrollbar with CSS Hiding the scrollbar with CSS can enhance the look of your webpage by removing visible scrollbars while still allowing users to scroll through content. This can create a cleaner, more streamlined user interface without sacrificing usability. In this article, weâll explore different methods for hidin 3 min read How to apply !important in CSS? The !important rule in CSS is used to add more importance to a property/value than normal. It forces a style to override any other declarations, ensuring the specified property value is applied, regardless of specificity. It helps resolve conflicts but should be used sparingly to avoid complicating 2 min read How to use a not:first-child Selector in CSS? The :not(:first-child) selector in CSS targets all elements that are not the first child within their parent. It allows you to style elements except the first one, making it useful for applying styles to subsequent items in a list or layout.Syntax:not( element ) { // CSS property } Example: In this 2 min read How to Auto-Resize an Image to Fit a Div Container? To resize an image or video to fit inside a div container, use the object-fit property for precise control over how the content fits.object-fit maintains the aspect ratio or stretches the content to fill the container.Common values include cover (fills the container, may crop) and contain (fits with 3 min read Like