we (web engine): Experimental web browser project to understand the limits of Claude

Margin collapsing for block-level elements #80

open opened by pierrelf.com

Phase 9: Advanced Layout#

Implement vertical margin collapsing for adjacent block-level elements per the CSS specification.

Current State#

  • Block layout stacks children vertically using margin_box_height() which simply sums margin + border + padding + content height
  • Adjacent vertical margins are NOT collapsed — they stack, producing double spacing
  • This causes incorrect spacing compared to browser behavior (e.g., two <p> elements with margin: 1em should have 1em gap, not 2em)

Requirements#

  1. Adjacent sibling collapsing: When two block-level siblings are adjacent (no border/padding between them), collapse their adjoining margins — the gap is max(bottom_margin, top_margin), not the sum
  2. Parent-child collapsing: A parent's top margin collapses with its first child's top margin if there is no border, padding, or inline content separating them. Same for bottom margin with last child
  3. Empty block collapsing: An empty block's top and bottom margins collapse with each other
  4. Negative margins: When one margin is negative, the collapsed margin is max_positive + min_negative. When both are negative, use the more negative value
  5. Collapsing is blocked by:
    • Elements with overflow other than visible
    • Floated elements
    • Absolutely positioned elements
    • Inline-block elements
    • Elements with border or padding between margins

Files to Modify#

  • crates/layout/src/lib.rs — Rewrite layout_block_children() to track and collapse margins instead of naively stacking

Tests#

  • Two adjacent <p> elements: gap equals the larger margin, not the sum
  • Parent with no padding/border: first child's top margin collapses with parent's
  • Negative margin collapsing: correct result for positive+negative and negative+negative cases
  • Collapsing blocked by border/padding between elements
  • Empty block margin collapsing

Acceptance Criteria#

  • Adjacent sibling vertical margins collapse correctly
  • Parent-first-child and parent-last-child collapsing works
  • Negative margins handled per spec
  • All tests pass, clippy clean, fmt clean
sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mhlhlmeh532k