I am a little confused about using z-index
to decide stack order.
I do not quite understand how browsers treat elements with the position
property in conjunction to those without it.
Is there a general rule to decide the stack order of elements whether it has explicitly positioned elements or not?
Examples of different situations are appreciated. Generally speaking:
<div>
s with position set and without position set. <div>
s mixed with sibling <div>
s with position set and without position set.z-index
propertyA Simple Concept
The z-index
property is based on a simple concept: Elements with higher values will sit in front of elements with lower values along the z-axis. So if you apply z-index: 1
to div.box1
, and div.box2
has a z-index: 0
, then div.box1
will overlay div.box2
.
In terms of the z-axis, it refers to depth on a three-dimensional plane. On your computer it can be interpreted as the plane on which objects move closer and farther from you. (Learn more about the Cartesian coordinate system.)
Source: Wikipedia
z-index
works on positioned elements
Unless you're dealing with flex items or grid items, the z-index
property works only on positioned elements. This means you can use z-index
on elements with position: absolute
, position: relative
, position: fixed
or position: sticky
. If the element has position: static
(the default value), or some other positioning scheme like a float
, then z-index
will have no effect.
As noted, although z-index
, as defined in CSS 2.1, applies only to positioned elements, flex items and grid items can create a stacking context even when position
is static
.
4.3. Flex Item Z-Ordering
Flex items paint exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and
z-index
values other thanauto
create a stacking context even ifposition
isstatic
.5.4. Z-axis Ordering: the
z-index
propertyThe painting order of grid items is exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and
z-index
values other thanauto
create a stacking context even ifposition
isstatic
.
Here's a demonstration of z-index
working on non-positioned flex items: https://jsfiddle.net/m0wddwxs/
Stacking Contexts
Once an element is positioned and a z-index
is applied, a stacking context is created.
(Also see: Full list of circumstances where a stacking context is created.)
The stacking context is a set of rules for managing the positioned element with z-index
, and its descendants. These rules govern the placement of child elements in the stacking order and the scope of the property's influence.
Essentially, the stacking context limits the z-index
scope to the element itself, and its child elements cannot affect the stacking order of elements in another stacking context.
If you've ever tried to apply increasingly higher z-index
values only to find that the element never moves out in front, you could be trying to overlay an element in a different stacking context.
Groups of elements with a common parent that move forward or backward together in the stacking order make up what is known as a stacking context. A full understanding of stacking contexts is key to really grasping how z-index and the stacking order work.
Every stacking context has a single HTML element as its root element. When a new stacking context is formed on an element, that stacking context confines all of its child elements to a particular place in the stacking order. That means that if an element is contained in a stacking context at the bottom of the stacking order, there is no way to get it to appear in front of another element in a different stacking context that is higher in the stacking order, even with a z-index of a billion!
~ What No One Told You About Z-Index
Stacking Order
CSS adheres to a stacking order when laying out elements on a page. These are the stacking rules when there is no z-index
specified, from farthest to closest:
If a z-index
property is applied, the stacking order is modified:
z-index
of less than 0z-index
of greater than 0Source: W3C
Bottom line: Once you understand stacking contexts, z-index
is easy.
For examples of z-index
in action see: How z-index works!
For a brief but highly informative article explaining z-index
(including how opacity
affects the stacking order) see: What No One Told You About Z-Index
For a complete rundown on z-index
, with many examples and illustrations, see: MDN Understanding CSS z-index
And for a deep dive into stacking contexts read: W3C Elaborate description of Stacking Contexts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With