
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Layers
CSS Layers are a concept that is used to control the stacking order of different DOM elements in a webpage. The z-index property determines the stacking order of elements within a stacking context.
The elements with a higher z-index are layered above those with a lower z-index and if elements share the same z-index, they stack according to their order in the DOM. We will look more into details and examples of CSS layering in this tutorial.
Table of Contents
CSS Layering With z-index Property
As mentioned above the z-index property can be used to decide stacking order of element in a webpage. The following example demonstrates how to create vertically stacked elements using z-index property. The elements with higher z-index value are positioned above the elements of lower z-index value.
Example
<html> <head> <style> div{ position: absolute; height: 100px; width: 100px; padding: 5px; color: white; } .box1 { background-color: darkred; z-index: 1; left: 10px; top: 10px; } .box2 { background-color: darkblue; z-index: 2; margin: 10px; left: 50px; top: 30px; } .box3 { background-color: darkgreen; z-index: 3; margin: 20px; left: 80px; top: 50px; } </style> </head> <body> <div class="box1"> z-index: 1 </div> <div class="box2"> z-index: 2 </div> <div class="box3"> z-index: 3 </div> </body> </html>
Layer Text Above Image
In CSS position property can be used to position text in different locations above an image.
First we need to set position property for image container as `position: relative` and position property of text as `position: absolute`. Now you can position of text elements using CSS inset properties like top, bottom, left and right.
Example
<html> <head> <style> .container { position: relative; border: 2px solid #ef2c2c; } .center { position: absolute; top: 45%; width: 100%; text-align: center; } .top-left { position: absolute; top: 12px; left: 30px; } .top-right { position: absolute; top: 12px; right: 30px; } .bottom-left { position: absolute; bottom: 12px; left: 30px; } .bottom-right { position: absolute; bottom: 12px; right: 30px; } img { width: 100%; opacity: 0.7; } </style> </head> <body> <div class="container"> <img src="/https/www.tutorialspoint.com/css/images/red-flower.jpg" width="1000px" height="350px"> <h3 class="center"> Text at Center </h3> <h3 class="top-left"> Text at Top Left </h3> <h3 class="top-right"> Text at Top Right </h3> <h3 class="bottom-left"> Text at Bottom Left</h3> <h3 class="bottom-right"> Text at Bottom Right </h3> </div> </body> </html>
Positioning Without z-index Property
The following example demonstrates how to create layers without using z-index property.
Example
<html> <head> <style> div{ position: absolute; height: 100px; width: 100px; padding: 5px; color: white; } .box1 { background-color: darkred; padding: 10px; left: 10px; top: 10px; } .box2 { background-color: lightblue; padding: 10px; margin: 10px; left: 30px; top: 30px; } .box3 { background-color: yellow; padding: 5px; margin: 10px; left: 60px; top: 60px; } </style> </head> <body> <div class="box1"> Box 1 </div> <div class="box2"> Box 2 </div> <div class="box3"> Box 3 </div> </body> </html>