Separate structure, style, geometry, and painting
The same DOM can produce different line wrapping and positions when the viewport or CSS changes. DOM describes document structure; style supplies presentation conditions; layout determines geometry under those conditions. Begin with vertically stacked fixed-width blocks and a viewport origin at the upper left. Painting then turns boxes and text into drawing operations. The demo separates DOM, layout boxes, and paint commands so that a plausible final image cannot hide a missing intermediate stage.
Give the layout model a viewport width, computed style for each element, and an explicit line-height rule. Changing only an h1 font size cannot yield reproducible height without specifying how line height is modeled. If actual font measurement is excluded, declare a fixed teaching approximation and show where it enters geometry. The first demo focuses on font-size, color, margin, width, and display. Full padding and border layout is a later extension. Read the following box-model calculation as an independent worked example of the dimensions that extension would need. Distinguishing a numerical exercise from a currently implemented demo feature prevents a learner from attributing unsupported CSS behavior to the model.
Calculate box-model dimensions numerically
For a 100px content width, 8px padding on each side, and 2px borders, the border-box width is 120px under content-box sizing. Margins sit outside that border box. Begin with content-box sizing and exclude margin collapsing and complex automatic widths. Using the declared width alone for painting and placement loses the additional areas. Show content, padding, and border separately with dimensions. State whether each displayed width is a content width, border-box width, or occupied outer width.
| Part | Calculation | Result |
|---|---|---|
| Content | 100 | 100px |
| Horizontal padding | 8 + 8 | 16px |
| Horizontal border | 2 + 2 | 4px |
| Border box | 100 + 16 + 4 | 120px |
Position vertically stacked boxes
With zero margins, a first box at y=0 and height 30 places the second box at y=30. If the second height is 20, the third begins at 50. Removing the first box from layout moves the second to 0. That differs from reserving its space but painting nothing. Change one box height and follow the resulting positions. Such a change does not necessarily require reparsing HTML or rebuilding DOM. Identify which stage received new input and which derived states must be recalculated.
When margins are included, declare whether the model simply adds gaps or implements CSS margin collapsing. A simple teaching calculation with A height 30, trailing gap 8, and B leading gap 4 places B at 42. That is not a universal prediction for production normal flow, where collapsing conditions can change the result. Recalculate positions from current inputs rather than repeatedly adding deltas to previous displayed coordinates. This avoids accumulated rounding or stale offsets during reset and back-and-forth parameter changes. Restore the same viewport and style values and require identical coordinates. That replay property is useful even before the model supports all layout rules.
A display:none element can remain in the DOM
In this model, display:none prevents the element and its descendants from generating layout boxes. Their DOM nodes remain and can become layout participants again after a style change. Merely omitting paint commands while retaining the old box positions would leave a gap and fail to model display:none. A later exercise can compare visibility behavior. First show three separate questions: does the DOM node exist, does it participate in layout, and does it generate paint operations? One Boolean is not enough to answer all three.
Generate drawing operations before showing the page
Translate geometry into a display list: fill a rectangle, draw a border, and draw text. Each operation carries coordinates, dimensions, color, content, and a link to its source node. Production browsers add more stages such as compositing, but the first model uses a simple documented paint order. Reveal operations one at a time. Feeding the original HTML to an iframe would show the host browser result without proving that the teaching layout engine calculated anything. Produce the picture from the model values instead.
Validating paint operations does not require executing source strings. Create structured data such as a rectangle with x=0, y=30, width=120, and height=20. Check finite coordinates and allowed dimensions before passing those values to SVG or canvas operations. Treat text-drawing content as text rather than active HTML. A background-color-only change should preserve operation coordinates and dimensions while changing the color field. Exposing the display list as a table lets a learner locate a misplaced shape: either layout produced wrong geometry, or the painting adapter transformed correct geometry incorrectly. Keep those failure cases separate in tests instead of checking only a final screenshot.
Compare width changes with color changes
In a model with line wrapping, reducing width can increase line count and height, moving later boxes. A color-only change that does not affect dimensions leaves coordinates unchanged while changing paint data. This comparison teaches dependency, not a universal claim about browser optimization internals. If the teaching engine uses fixed character width and line height, state that approximation. It does not reproduce actual font shaping or Japanese line-breaking rules and should not promise identical wrapping to every production browser.
Exercise: calculate positions after hiding a box
Use zero margins and box heights A=30, B=20, C=40, starting at y=0. Initially their y coordinates are 0, 30, and 50. After B receives display:none, three DOM nodes remain but layout contains A at 0 and C at 30. Changing only the background color of A leaves those coordinates unchanged. Report DOM node count, layout box count, the y coordinate of C, and the changed paint data separately. Replace the vague observation that the page changed with a precise explanation of the affected stage.
As another test, hide B, restore it, then hide it again. C should move to 30, back to 50, and then to 30. Merely toggling a visibility flag may leave cached coordinates or paint operations stale. Rebuild positions from the current layout participants, then derive a fresh display list. Also define progress conditions for zero or very narrow widths so a line-wrapping loop cannot run forever without consuming a character. Report unsupported placement or font conditions explicitly. An empty screen with no explanation should not count as successful layout. These checks make an editable demonstration reliable when users explore boundary inputs rather than following only the default example.