Layout output represents the result of computing layout for a single node. It contains the node's outer size, content size, baseline positions, and margin collapsing data for CSS block layout. Layout algorithms return this structure to their parent nodes during the layout computation phase.
Key Concepts
Outer Size vs Content Size
The size field represents the node's outer dimensions including padding and borders. The content_size field represents the dimensions of content within the node, which may exceed size when content overflows. This distinction enables computation of scroll dimensions for scrollable containers.
Baselines
Baselines indicate the line on which text sits, used for aligning inline content and flex items. Text nodes and nodes containing text descendants typically have baselines. For nodes without baselines, use Geometry.(Point.none) for the first_baselines field.
Margin Collapsing
Margin collapsing is specific to CSS block layout, where adjacent vertical margins collapse according to CSS rules. The top_margin, bottom_margin, and margins_can_collapse_through fields track collapsible margins during block layout computation.
For layout modes that do not support margin collapsing (flexbox, grid), set top_margin and bottom_margin to Collapsible_margin_set.zero and margins_can_collapse_through to false.
make ~size ~content_size ~first_baselines ~top_margin ~bottom_margin ~margins_can_collapse_through creates a layout output with all fields specified.
Use this constructor for CSS block layout where margin collapsing is relevant. For flexbox and grid layouts, prefer from_sizes_and_baselines which sets margins to zero automatically.
hidden is a zero-sized layout output for nodes with Display.None.
All sizes are zero (size and content_size), baselines are None, top and bottom margins are zero, and margins cannot collapse through. Use this when a node is hidden and should not contribute to layout.
from_outer_size size creates a layout output from just the outer size.
Sets size to the provided value and content_size to zero. Use this when short-circuiting layout computation after determining the container's outer dimensions but before computing content layout. Baselines are None, margins are zero, and margins cannot collapse through.
from_sizes_and_baselines size content_size first_baselines creates a layout output from sizes and baselines.
Use this for flexbox and grid layouts after computing both outer size and content size. The content_size should reflect the actual dimensions of content within the node, which may differ from size when content overflows or when the container is larger than its content.
Margins are set to zero and margins cannot collapse through, as flexbox and grid do not support margin collapsing.