Stacking Context
May 20, 2023
A stacking context is a fundamental concept used in web development that provides a mechanism for organizing the order in which elements are displayed on the screen. Stacking contexts are essential for web developers who need to control the visibility of overlapping elements in a layout. By creating a stacking context, web developers can control the order in which elements are displayed, allowing them to create complex layouts with overlapping elements that are still easy to read and navigate.
Purpose
The purpose of a stacking context is to provide a way for web developers to control the order in which elements are displayed on a webpage. This is important because elements on a webpage are stacked on top of one another, and the order in which they are stacked determines which elements are visible and which are not. By creating a stacking context, web developers can control the visibility of overlapping elements, ensuring that the most important elements are visible and easily accessible.
Usage
Stacking contexts are created by applying a set of CSS rules to an element. These rules define the stacking context, including the order in which elements are stacked and whether they are visible or hidden. The most common way to create a stacking context is to set the z-index
property of an element. The z-index
property is a numeric value that determines the order in which elements are stacked. Elements with a higher z-index
value are stacked on top of elements with a lower z-index
value.
To create a stacking context, web developers can set the z-index
property to a value other than auto
. This value can be a positive or negative integer or zero. Positive values create a new stacking context, while negative values do not create a new stacking context but instead place the element behind the default stacking context. A value of zero places the element in the default stacking context.
/* Create a new stacking context */
#element {
position: relative;
z-index: 1;
}
/* Place element behind the default stacking context */
#element {
position: relative;
z-index: -1;
}
/* Place element in the default stacking context */
#element {
position: relative;
z-index: 0;
}
In addition to the z-index
property, there are several other CSS properties that can be used to create a stacking context. These include opacity
, transform
, filter
, isolation
, and others. Each of these properties creates a new stacking context when used with certain values.
Nested Stacking Contexts
Elements within a stacking context can also have their own stacking contexts. These nested stacking contexts are created by applying the same CSS properties to child elements as are used to create the parent stacking context. When a child element has a stacking context, it is stacked relative to its parent stacking context, rather than the default stacking context.
/* Parent Stacking Context */
#parent {
position: relative;
z-index: 1;
}
/* Child Stacking Context */
#child {
position: relative;
z-index: 1;
}
In this example, the #child
element has a z-index
value of 1
, creating a new stacking context within the #parent
element’s stacking context. This means that the #child
element is stacked relative to the #parent
element, not relative to the default stacking context.
Stacking Order
The stacking order of elements within a stacking context is determined by several factors, including the z-index
property, the order in which the elements appear in the HTML, and the position of the elements on the page. When two elements have the same z-index
value, the element that appears last in the HTML is stacked on top.
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
</div>
In this example, both the #child1
and #child2
elements have a z-index
value of auto
. This means that they are both in the default stacking context and are stacked based on their position in the HTML. Since #child2
appears last in the HTML, it is stacked on top of #child1
.
Stacking Context Hierarchy
When multiple stacking contexts exist on a webpage, they form a hierarchy based on their position in the HTML tree. The stacking context with the highest z-index
value is at the top of the hierarchy, and the stacking context with the lowest z-index
value is at the bottom.
<div id="parent1">
<div id="child1"></div>
<div id="child2"></div>
<div id="child3"></div>
</div>
<div id="parent2">
<div id="child4"></div>
<div id="child5"></div>
</div>
In this example, the #parent1
element has a z-index
value of 1
, creating a new stacking context. The #parent2
element also has a z-index
value of 1
, creating a new stacking context. Since #parent1
appears before #parent2
in the HTML, it is at the top of the stacking context hierarchy. Any elements within #parent1
will be stacked on top of elements within #parent2
.