z-index
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The z-index
CSS property sets the z-order of a positioned element and its descendants or flex and grid items. Overlapping elements with a larger z-index cover those with a smaller one.
Try it
z-index: auto;
z-index: 1;
z-index: 3;
z-index: 5;
z-index: 7;
Change my z-index
z-index: 6
z-index: 4
z-index: 2
z-index: auto
z-index: auto
z-index: auto
#example-element {
top: 15px;
left: 15px;
width: 180px;
height: 230px;
position: absolute;
/* center the text so it is visible even when z-index is set to auto */
line-height: 215px;
font-family: monospace;
background-color: #fcfbe5;
border: solid 5px #e3e0a1;
z-index: auto;
color: black;
}
.container {
display: inline-block;
width: 250px;
position: relative;
}
.block {
width: 150px;
height: 50px;
position: absolute;
font-family: monospace;
color: black;
}
.blue {
background-color: #e5e8fc;
border: solid 5px #112382;
/* move text to the bottom of the box */
line-height: 55px;
}
.red {
background-color: #fce5e7;
border: solid 5px #e3a1a7;
}
.position1 {
top: 0;
left: 0;
z-index: 6;
}
.position2 {
top: 30px;
left: 30px;
z-index: 4;
}
.position3 {
top: 60px;
left: 60px;
z-index: 2;
}
.position4 {
top: 150px;
left: 0;
z-index: auto;
}
.position5 {
top: 180px;
left: 30px;
z-index: auto;
}
.position6 {
top: 210px;
left: 60px;
z-index: auto;
}
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;
/* 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: revert-layer;
z-index: unset;
The z-index
property is specified as either the keyword auto
or an
.
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
. -
This
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 value | auto |
---|---|
Applies to | positioned elements |
Inherited | no |
Computed value | as specified |
Animation type | an integer |
Creates stacking context | yes |
Formal syntax
Examples
Visually layering elements
HTML
Dashed box
Gold box
Green box
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 # z-index |
Browser compatibility
See also
- CSS
position
property - Understanding CSS z-indexes