Phase 9: Advanced Layout#
Implement CSS Flexbox (Flexible Box Layout Level 1) for one-dimensional layout of items along a main axis.
Dependencies#
- box-sizing should be implemented first
- Viewport units and percentage resolution should be implemented first
Current State#
display: flexanddisplay: inline-flexare NOT parsed- No flex-related CSS properties are parsed
- Layout only supports block and inline display modes
Requirements#
-
Parse new display values:
flex,inline-flex -
Parse flex container properties:
flex-direction:row(default),row-reverse,column,column-reverseflex-wrap:nowrap(default),wrap,wrap-reversejustify-content:flex-start(default),flex-end,center,space-between,space-around,space-evenlyalign-items:stretch(default),flex-start,flex-end,center,baselinealign-content:stretch(default),flex-start,flex-end,center,space-between,space-aroundgap,row-gap,column-gap
-
Parse flex item properties:
flex-grow:<number>(default 0)flex-shrink:<number>(default 1)flex-basis:<length>|auto(defaultauto)flexshorthand:none,auto,<grow> <shrink> <basis>align-self:auto(default),flex-start,flex-end,center,baseline,stretchorder:<integer>(default 0)
-
Flex layout algorithm (CSS Flexbox Level 1 §9):
- Determine main axis and cross axis from
flex-direction - Collect flex items, resolve
flex-basis(auto → content size or specified width/height) - Distribute free space: grow items with
flex-grow, shrink withflex-shrink - Handle wrapping: when
flex-wrap: wrap, create new flex lines when items exceed main axis size - Align items on cross axis per
align-items/align-self - Distribute flex lines per
align-content - Justify items on main axis per
justify-content - Apply
orderproperty for visual ordering - Apply
gapbetween items
- Determine main axis and cross axis from
-
Flex formatting context: Flex containers establish a new formatting context for their children
Files to Modify#
crates/css/src/values.rs— Parse flex shorthand and individual propertiescrates/style/src/computed.rs— Add flex properties toComputedStyle, resolve values, addDisplay::Flex/InlineFlexcrates/layout/src/lib.rs— Implement flex layout algorithm (new major code path alongside block/inline)
Tests#
display: flexwith default settings: items laid out in a rowflex-direction: column: items stacked verticallyflex-grow: items expand to fill containerflex-shrink: items shrink when container is too smalljustify-content: center: items centered on main axisalign-items: center: items centered on cross axisflex-wrap: wrap: items wrap to new linegap: correct spacing between itemsorder: visual order differs from DOM order
Acceptance Criteria#
display: flexcreates a flex container- All flex container and item properties work per CSS Flexbox Level 1
- Flex algorithm correctly distributes space (grow/shrink/basis)
- Wrapping, alignment, and justification work correctly
- All tests pass, clippy clean, fmt clean