Phase 9: Advanced Layout#
Implement position: absolute and position: fixed layout with stacking contexts and z-index.
Dependencies#
- Relative positioning should be implemented first (provides the containing block concept)
- Overflow clipping should be implemented first (absolute positioning interacts with overflow)
Current State#
Position::AbsoluteandPosition::Fixedare parsed in computed stylestop,right,bottom,leftare parsed asLengthOrAutoz-indexis NOT parsed- Layout treats all elements as
position: static
Requirements#
-
Absolute positioning:
- Remove from normal flow (does not affect sibling layout)
- Position relative to the nearest positioned ancestor (not
static), or the initial containing block (viewport) - Resolve
top/right/bottom/leftagainst the containing block - If both
topandbottomare specified with aheight, the element is over-constrained (bottomis ignored for LTR) autooffsets: use the element's static position (where it would have been in normal flow)
-
Fixed positioning:
- Same as absolute but the containing block is always the viewport
- Does not scroll with page content
-
Stacking context and z-index:
- Parse
z-index:auto(default),<integer> - Elements with
position: absolute/fixedandz-index!=autocreate a stacking context - Painting order within a stacking context: backgrounds → negative z-index → block flow → floats → inline flow → z-index 0/auto → positive z-index
- Parse
-
Width/height resolution:
- An absolutely positioned element with
leftandrightspecified (andwidth: auto) stretches to fill - Same for
top/bottomwithheight: auto
- An absolutely positioned element with
Files to Modify#
crates/style/src/computed.rs— Parsez-index, ensure offsets resolve correctly for positioned elementscrates/layout/src/lib.rs— Remove absolutely positioned elements from normal flow, position them relative to containing blockcrates/render/src/lib.rs— Implement stacking context paint order
Tests#
position: absolutewithtop: 10px; left: 20pxpositions relative to nearest positioned ancestorposition: fixedpositions relative to viewport- Absolutely positioned element does not affect sibling layout
z-indexordering: higher z-index paints on top- Stretching:
left: 0; right: 0; width: autofills containing block width
Acceptance Criteria#
- Absolute positioning works relative to nearest positioned ancestor
- Fixed positioning works relative to viewport
- z-index ordering is correct
- Elements are correctly removed from normal flow
- All tests pass, clippy clean, fmt clean