Display, Positioning and Stacking
Understand normal flow, display modes, positioning schemes, containing blocks, stacking contexts, z-index, and layering.
Normal Flow
Normal flow is the browser's default layout behavior. Block-level elements generally occupy available inline space and stack vertically, while inline elements participate in line formatting.
Modern CSS layout systems such as Flexbox and Grid provide controlled alternatives when normal flow alone is insufficient.
.article {
max-width: 70ch;
}
.article p {
margin-block: 1rem;
}Paragraphs naturally participate in document flow and stack according to their block formatting behavior.
Normal flow should generally be the starting point.
Avoid absolute positioning for ordinary document layout.
What is normal flow?
Display Values
The display property controls how an element participates in layout. Common values include block, inline, inline-block, flex, grid, none, table, and newer multi-keyword display syntax.
display: none removes the element from layout and accessibility trees in typical browser behavior, while visibility: hidden generally keeps the element's layout space but hides it.
.toolbar {
display: flex;
}
.badge {
display: inline-block;
}
.desktop-only {
display: none;
}Different display values establish different formatting contexts and layout behaviors.
display: noneremoves an element from layout.inline-blockallows inline participation with block-like dimensions.
What happens to layout space when display: none is applied?
Positioning
CSS positioning includes static, relative, absolute, fixed, and sticky.
relative keeps an element in normal flow while establishing a positioning context for descendants. absolute removes the element from normal flow and positions it relative to a containing block. fixed typically positions an element relative to the viewport, while sticky combines normal flow with scroll-based sticking behavior.
.card {
position: relative;
}
.card__badge {
position: absolute;
inset-block-start: 1rem;
inset-inline-end: 1rem;
}The card establishes a positioning context and the badge is positioned relative to it.
Use
relativeto establish a positioning context without removing the element from flow.Absolute positioning should be used for overlays and anchored elements rather than ordinary page layout.
Why is position: relative commonly applied to a parent of an absolutely positioned child?
z-index and Stacking Contexts
The z-index property controls stacking order within stacking contexts. A common mistake is assuming that a very large z-index always places an element above everything else. It does not cross stacking-context boundaries.
Stacking contexts can be created by several CSS conditions, including positioned elements with non-auto z-index, opacity below 1, transforms, isolation, and other properties.
.modal {
position: fixed;
inset: 0;
z-index: 1000;
}
.modal__dialog {
position: relative;
z-index: 1;
}A modal can establish a deliberate layering hierarchy, but application-wide z-index values should be organized rather than arbitrarily increased.
z-indexis meaningful within stacking contexts.Create a documented layering scale instead of using random huge values.
Does z-index: 999999 guarantee an element appears above every element on the page?