CSS Positioning Models Under the Hood
In frontend layouts, a foundational interview question is:
Explain the differences between the CSS positioning models (
static,relative,absolute,fixed, andsticky). How do they impact the normal document flow?
To answer this, you must analyze how each position scheme behaves relative to its container and how it impacts surrounding elements.
1. The Positioning Schemes
A. static (Default)
- Reference Point: The normal document layout flow.
- Document Flow: The element occupies its natural space in the flow. Sizing is determined by normal block/inline box model rules.
- Offset Properties:
top,bottom,left,right, andz-indexhave no effect.
B. relative
- Reference Point: The element's own default static position.
- Document Flow: The original space of the element is preserved in the flow. It acts as if it is still positioned statically, and nearby elements do not move to fill the gap.
- Offset Properties: Offsets (
top,left, etc.) shift the element visually from its resting place without altering surrounding elements. It also establishes a coordinate system for any child elements set toabsolute.
C. absolute
- Reference Point: The closest ancestor container that has a position value other than
static(e.g.,relative,absolute,fixed, orsticky). If no such ancestor exists, it positions itself relative to the Initial Containing Block (the viewport boundary). - Document Flow: The element is completely removed from the normal document flow. Other elements behave as if it does not exist, collapsing to fill its space.
- Offset Properties: Sizing and offset properties (
top,left, etc.) determine the box's coordinates relative to its parent container's borders.
D. fixed
- Reference Point: The browser viewport (or a container with a
transform,perspective, orfilterproperty other thannoneacting as the containing block). - Document Flow: The element is completely removed from the normal document flow.
- Offset Properties: Position coordinates remain locked to the viewport and do not shift during page scrolling.
E. sticky
- Reference Point: It acts as
relativeuntil the viewport scroll offset crosses a specified threshold, at which point it acts asfixedwithin the boundaries of its parent element. - Document Flow: Space is preserved in the normal document flow.
- Important: It stays active only within its parent box. Once the parent box scrolls out of view, the sticky element scrolls out with it.
2. Quick Stacking Context & Stacking Order
A positioning property other than static combined with a z-index value other than auto creates a new Stacking Context.
Within a stacking context, child elements are stacked based on:
- Root element of the stacking context.
- Positioned elements with negative z-index.
- Non-positioned block elements.
- Non-positioned float elements.
- Inline elements.
- Positioned elements with z-index:
autoor0. - Positioned elements with positive z-index (higher number on top).
3. Comparison Matrix
| Property | Removed from Flow? | Positioning Context | Parent Stays Intact? |
|---|---|---|---|
static | No | Default Document Flow | Yes |
relative | No | Itself | Yes (space held) |
absolute | Yes | Closest non-static ancestor | No (collapsed) |
fixed | Yes | Viewport | No (collapsed) |
sticky | No | Viewport scroll + parent | Yes (space held) |
Key Takeaways
- Relative Container: Use
relativeon parent elements when you want to containabsolutechildren inside their boundaries. - Sticky Elements: Use
stickyfor layout headers, tables of contents, or sidebar widgets that should follow scrolling but remain inside their sections. - Layout Collapse: Remember that
fixedandabsolutecollapse parent dimensions, which often requires explicitly setting dimensions on parent wrappers.