What is a stacking context?
A stacking context is a self-contained layer in CSS that controls the z-order of overlapping elements, where nested contexts restrict z-index interactions; it is created by properties like z-index on positioned elements, opacity
A stacking context is a three-dimensional conceptual layer in CSS that determines the visual order of elements on a webpage when they overlap. Even though web pages are built in a 2D plane, CSS allows elements to stack on top of one another — like sheets of paper piled up — and the stacking context defines which elements appear above or below others.

Think of it like this: when elements overlap (due to positioning, opacity, flexbox, etc.), the browser needs rules to decide who shows up on top. That’s where the stacking context comes in.
How Stacking Context Works
Elements don’t all compete globally for z-order. Instead, stacking contexts can be nested. Each stacking context is self-contained, meaning elements inside it don’t affect elements in other stacking contexts. The browser paints elements in a specific order, from back to front:

- Background and borders of the parent element
- Block-level elements in normal flow, in order of appearance
- Positioned elements (and flex/grid children), in order of their
z-index
:- First, those with negative
z-index
(painted back to front) - Then, non-positioned elements and those with
z-index: auto
- Finally, elements with positive
z-index
(painted front to back)
- First, those with negative
But here’s the key: a new stacking context limits how z-index
values interact.
When Is a Stacking Context Created?
A new stacking context is formed when any of the following are applied to an element:

-
position
withz-index
other thanauto
(only if the element is positioned:relative
,absolute
,fixed
, orsticky
) -
opacity
less than 1 -
transform
(e.g.,scale
,rotate
) -
filter
(e.g.,blur(2px)
) -
will-change
(if it hints attransform
oropacity
) -
perspective
orclip-path
isolation: isolate
-
mix-blend-mode
other thannormal
-
contain
with valuelayout
,paint
, orstrict
Once a new stacking context is created, any z-index
values inside are relative to that context, not the whole page.
Practical Example
<div class="box1">Box 1 (z-index: 2)</div> <div class="container"> <div class="box2">Box 2 (z-index: 100)</div> </div>
.box1 { position: relative; z-index: 2; background: red; } .container { position: relative; z-index: 1; /* Creates a new stacking context */ opacity: 0.9; /* Also creates a new stacking context */ } .box2 { position: relative; z-index: 100; background: blue; }
Even though .box2
has z-index: 100
, it’s contained within the stacking context of .container
, which has a lower effective stacking level than .box1
. So red .box1
will appear above blue .box2
— counterintuitive, but correct.
Why It Matters
Understanding stacking contexts helps avoid frustrating layout bugs. For example:
- A dropdown menu getting "cut off" by a nearby element
- A modal not appearing on top despite high
z-index
- Tooltips or popovers appearing behind other content
The fix often involves checking if a parent element created an unintended stacking context (e.g., via opacity
or transform
), which then traps the child’s z-index
.
Basically, it’s not just about z-index
— it’s about the hierarchy of stacking contexts.
The above is the detailed content of What is a stacking context?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Backdrop-filter is used to apply visual effects to the content behind the elements. 1. Use backdrop-filter:blur(10px) and other syntax to achieve the frosted glass effect; 2. Supports multiple filter functions such as blur, brightness, contrast, etc. and can be superimposed; 3. It is often used in glass card design, and it is necessary to ensure that the elements overlap with the background; 4. Modern browsers have good support, and @supports can be used to provide downgrade solutions; 5. Avoid excessive blur values and frequent redrawing to optimize performance. This attribute only takes effect when there is content behind the elements.

The style of the link should distinguish different states through pseudo-classes. 1. Use a:link to set the unreached link style, 2. a:visited to set the accessed link, 3. a:hover to set the hover effect, 4. a:active to set the click-time style, 5. a:focus ensures keyboard accessibility, always follow the LVHA order to avoid style conflicts. You can improve usability and accessibility by adding padding, cursor:pointer and retaining or customizing focus outlines. You can also use border-bottom or animation underscore to ensure that the link has a good user experience and accessibility in all states.

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.

Use the border attribute to set the dashed style to quickly create dotted lines, such as border-top:2pxdashed#000; 2. You can customize the appearance of the dotted line by adjusting the border width, color and style; 3. When applying the dotted line to dividers or inline elements, it is recommended to set height:0 or reset the default style of hr; 4. If you need to accurately control the length and spacing of the dotted line, you should use background-image and linear-gradient to cooperate with linear-gradient, for example, background:linear-gradient(toright, black33%, transparent33%) repe

Define@keyframesbouncewith0%,100%attranslateY(0)and50%attranslateY(-20px)tocreateabasicbounce.2.Applytheanimationtoanelementusinganimation:bounce0.6sease-in-outinfiniteforsmooth,continuousmotion.3.Forrealism,use@keyframesrealistic-bouncewithscale(1.1

Tocenteradivhorizontally,setawidthandusemargin:0auto.2.Forhorizontalandverticalcentering,useFlexboxwithjustify-content:centerandalign-items:center.3.Alternatively,useCSSGridwithplace-items:center.4.Forolderbrowsers,useabsolutepositioningwithtop:50%,l

To achieve CSS element overlap, you need to use positioning and z-index attributes. 1. Use position and z-index: Set elements to non-static positioning (such as absolute, relative, etc.), and control the stacking order through z-index, the larger the value, the higher the value. 2. Common positioning methods: absolute is used for precise layout, relative is used for relatively offset and overlap adjacent elements, fixed or sticky is used for fixed positioning of suspended layers. 3. Actual example: By setting the parent container position:relative, child element position:absolute and different z-index, the card overlap effect can be achieved.

Use pure CSS to implement modal pop-up windows to control the visible and hidden checkbox. 1. Use input[type="checkbox"] as the status switch; 2. Use: checked .modal to control the display of modal boxes; 3. Use label[for] to trigger checking to achieve opening and closing; 4. Add @keyframes animation to achieve fade-in pop-up effect; 5. The close button or mask click area in the modal box can be bound to label control hidden. The entire process does not require JavaScript, is very compatible and has strong accessibility, and is suitable for static pages or lightweight interactive scenarios.
