In this lesson, you gave the Coming Up section a green background, tried to give its heading room with a top margin, and found the margin outside the section instead. You saw why touching margins collapse into one, and how padding keeps a child’s margin inside its parent.
Follow Along
The video starts from events/upcoming.html and events/events.css as you left them in Choose How to Hide Content, with the “Needs water” badge as a link and the .visually-hidden rule at the end of events.css. Open the page in Chrome or Edge, with DevTools docked beside it. Rosa would like the Coming Up section to stand out from the white page.
Key Points
- A green section with a gap above it. With a class on the section and a pale green background, the green shows, but the heading sits right against its top edge, and there’s a white strip between the banner and the green.
- The natural attempt. The heading already has a top margin from the browser, yet it touches the green, so a bigger margin seems like the fix. A
margin-topof 24 pixels on the section’s heading doesn’t put green above the heading. The white strip above the green grows from about 17 pixels to 24, because your 24 replaced the heading’s default margin of about 17. The heading still touches the green’s top edge. - Where the margin went. In DevTools, hover over the heading, and its orange margin sits above the green, over the white strip. Click the section’s row, and its box starts where the heading starts. In the Computed tab, its box diagram shows a margin of 0. The white strip is the heading’s margin, outside the section.
- Touching margins collapse. In the work day card, the heading’s bottom margin is about 19 pixels (18.72), and the date paragraph’s top margin is 16. The paragraph’s margin sits in the same gap, not below it. When the vertical margins of two blocks in normal flow touch, the browser collapses them into one, the size of the larger.
- Watch out: touching margins don’t add up. The gap is 19 pixels, not 35.
- A parent’s edge doesn’t stop it. The section’s top margin and its first child’s top margin touch, because there’s no padding or border between them. So they collapse into one 24-pixel margin, which pushes the whole section down. The box diagrams still show each element’s own margin, 0 and 24. The collapse happens between them, on the page.
- The same at the bottom. The section has no set height, so its last child, the seed swap card, does the same. The section ends at the card’s bottom edge, and the card’s 24-pixel margin sits below it. That strip is honeydew, not white, because the margin leaks out of the
<main>element too. - Padding keeps the margin inside. With
padding: 24pxon the section, the white strip is gone, and at the bottom, the card’s margin is inside the green now too. The heading’s margin is inside the green, under the padding. A border between them would also stop the collapse. - Let the padding make the room. Padding plus the heading’s margin is 48 pixels, more than the heading needs. Deleting your rule would bring back the heading’s default margin, so set its
margin-topto0. Your cards already do the same, with padding on the card and no top margin on its heading. - The rule: between blocks in normal flow, vertical margins that touch collapse into one, the size of the larger. A first or last child’s margin can touch its parent’s and leak out. Padding or a border on that edge is one way to keep it inside.