The first part of this article, Stacking without the z-index property, explains how stacking is arranged by default. If you want to create a custom stacking order, you can use the z-index
property on a positioned element.
The z-index
property can be specified with an integer value (positive, zero, or negative), which represents the position of the element along the z-axis. If you are not familiar with the z-axis, imagine the page as a stack of layers, each one having a number. Layers are rendered in numerical order, with larger numbers above smaller numbers.
- bottom layer (farthest from the observer)
- ...
- Layer -3
- Layer -2
- Layer -1
- Layer 0 (default rendering layer)
- Layer 1
- Layer 2
- Layer 3
- ...
- top layer (closest to the observer)
Note:
- When no
z-index
property is specified, elements are rendered on the default rendering layer 0 (zero). - If several elements share the same
z-index
value (i.e., they are placed on the same layer), stacking rules explained in the section Stacking without the z-index property apply.
In the following example, the layers' stacking order is rearranged using z-index
. The z-index
of element #5 has no effect since it is not a positioned element.