z-index
Quick Summary for z-index
The z-index CSS property sets the z-order of a positioned element and its descendants or flex items. Overlapping elements with a larger z-index cover those with a smaller one.
Code Usage for z-index
/* Keyword value */ z-index: auto;  /* <integer> values */ z-index: 0; z-index: 3; z-index: 289; z-index: -1; /* Negative values to lower the priority */  /* Global values */ z-index: inherit; z-index: initial; z-index: revert; z-index: unset; 
More Details for z-index

z-index

The z-index CSS property sets the z-order of a positioned element and its descendants or flex items. Overlapping elements with a larger z-index cover those with a smaller one.

For a positioned box (that is, one with any position other than static), the z-index property specifies:

The stack level of the box in the current stacking context. Whether the box establishes a local stacking context.

Syntax

/* Keyword value */ z-index: auto;  /* <integer> values */ z-index: 0; z-index: 3; z-index: 289; z-index: -1; /* Negative values to lower the priority */  /* Global values */ z-index: inherit; z-index: initial; z-index: revert; z-index: unset; 

The z-index property is specified as either the keyword auto or an <integer>.

Values

auto

The box does not establish a new local stacking context. The stack level of the generated box in the current stacking context is 0.

<integer>

This <integer> is the stack level of the generated box in the current stacking context. The box also establishes a local stacking context. This means that the z-indexes of descendants are not compared to the z-indexes of elements outside this element.

Formal definition

Initial valueauto
Applies topositioned elements
Inheritedno
Computed valueas specified
Animation typean integer
Creates stacking contextyes

Formal syntax

auto | <integer>

Examples

Visually layering elements

HTML
<div class="wrapper">   <div class="dashed-box">Dashed box</div>   <div class="gold-box">Gold box</div>   <div class="green-box">Green box</div> </div> 
CSS
.wrapper {   position: relative; }  .dashed-box {   position: relative;   z-index: 1;   border: dashed;   height: 8em;   margin-bottom: 1em;   margin-top: 2em; } .gold-box {   position: absolute;   z-index: 3; /* put .gold-box above .green-box and .dashed-box */   background: gold;   width: 80%;   left: 60px;   top: 3em; } .green-box {   position: absolute;   z-index: 2; /* put .green-box above .dashed-box */   background: lightgreen;   width: 20%;   left: 65%;   top: -25px;   height: 7em;   opacity: 0.9; } 
Result

Specifications

Specification
Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification # z-index

See also

CSS position property Understanding CSS z-indexes

Last modified: Feb 2, 2022, by MDN contributors

Select your preferred language English (US)DeutschEspañolFrançais日本語한국어PolskiРусский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
A.A. Milne’s Winnie-the-Pooh: “If you live to be a hundred, I want to live to be a hundred minus one day so I never have to live without you.”
A.A. Milne
Random CSS Property

revert

The revert CSS keyword reverts the cascaded value of the property from its current value to the value the property would have had if no changes had been made by the current style origin to the current element. Thus, it resets the property to its inherited value if it inherits from its parent or to the default value established by the user agent's stylesheet (or by user styles, if any exist). It can be applied to any CSS property, including the CSS shorthand all.
revert css reference