Phase 9: Advanced Layout#
Implement position: relative so that elements can be offset from their normal flow position without affecting the layout of surrounding elements.
Current State#
Position::Relativeis already parsed instyle/computed.rstop,right,bottom,leftare parsed asLengthOrAutoin computed styles- Layout currently ignores position offsets entirely — all elements behave as
static
Requirements#
- Apply relative offsets after normal layout: After computing the normal-flow position for a
position: relativeelement, shift its rendered position by the resolvedtop/right/bottom/leftvalues - Do not affect surrounding elements: Relative positioning only moves the element visually — siblings and parent layout remain unchanged (the element's original space is preserved)
- Offset resolution rules (per CSS spec):
- If both
topandbottomare specified,topwins (fordirection: ltr) - If both
leftandrightare specified,leftwins (fordirection: ltr) autovalues resolve to0
- If both
- Percentage resolution:
top/bottompercentages resolve against containing block height;left/rightagainst containing block width (can defer percentage support to the viewport units/percentage issue)
Files to Modify#
crates/layout/src/lib.rs— Apply offsets incompute_layout()or a new post-layout passcrates/style/src/computed.rs— Ensuretop/right/bottom/leftvalues are accessible
Tests#
- Element with
position: relative; top: 10px; left: 20pxrenders shifted from normal position - Sibling elements are not affected by the relative offset
- Conflicting offsets (
top+bottom) resolve correctly autooffsets have no effect
Acceptance Criteria#
position: relativewithtop/left/bottom/rightoffsets works correctly- Normal flow layout of surrounding elements is unaffected
- All tests pass, clippy clean, fmt clean