Exercise 2: A Fresh Centering Example With Different Numbers — Possible Solution ==================================================================== THE TEST ------------------------------ f = block({'width': '250px', 'margin': '10px auto'}) root_cb2 = Dimensions(Rect(0, 0, 400, 0), zero, zero, zero) # 400px container calculate_block_width(f, root_cb2) f.dimensions.margin.left f.dimensions.margin.right f.dimensions.margin.top RESULT ------------------------------ margin.left -> 75.0 margin.right -> 75.0 margin.top -> 10.0 CONFIRMING THE BOX IS GENUINELY CENTERED ------------------------------ margin.left (75) + width (250) + margin.right (75) = 400 -- exactly the containing block's own width. Both margins are equal, so the box has identical empty space on its left and right -- the textbook definition of horizontal centering. Working the arithmetic directly: 2-value shorthand '10px auto' gives top=bottom=10, right=left='auto'. Both horizontal margins are auto, so calculate_block_width takes the `if ml_auto and mr_auto:` branch: total = 0(margin_left, zeroed) + 0(border) + 0(padding) + 250(width) + 0(padding) + 0(border) + 0(margin_right, zeroed) = 250 underflow = 400 - 250 = 150 margin_left = margin_right = underflow / 2 = 75.0 WHY THE VERTICAL COMPONENT IS COMPLETELY UNAFFECTED ------------------------------ '10px auto' expands via expand_margin_shorthand's own 2-value rule: `t = b = parts[0]` (10.0) and `r = l = parts[1]` ('auto') -- top and bottom are set from the FIRST token, completely independently of whatever happens to the second token (right/left). The auto-margin- solving logic inside calculate_block_width only ever reads and writes margin_left and margin_right -- it has no branch, no code path, and no variable that touches raw_margin.top or raw_margin.bottom at all, beyond the two unconditional lines near the end: top_margin = 0.0 if raw_margin.top == 'auto' else raw_margin.top bottom_margin = 0.0 if raw_margin.bottom == 'auto' else raw_margin.bottom Since raw_margin.top is 10.0 (a real number, never the string 'auto'), this line just passes it through unchanged: top_margin = 10.0. WHY THIS WORKS AS AN ANSWER ------------------------------ CSS's own constraint equation is defined purely in terms of HORIZONTAL measurements (widths, horizontal padding/border/margin) -- vertical margin plays no role in solving for width at all, which this engine's own code structure reflects directly: the entire auto- resolution block operates exclusively on margin_left/margin_right, never once referencing margin_top/margin_bottom in any of its arithmetic. Testing with genuinely different numbers than the chapter's own 300px/100px example (400px container, 250px width, and a non-zero vertical margin mixed in) confirms the centering behavior and the vertical-margin independence both generalize, rather than being coincidences of the chapter's own specific numbers.