Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Watch Margins Collapse

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-top of 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: 24px on 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-top to 0. 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Every box on the events page stacks in normal flow, one under the other. In this video, you’ll see what happens when the margins between those boxes meet.

00:10 Rosa would like the Coming Up section to stand out from the white page. In upcoming.html, give the section the class coming-up, so a rule can select it. In events.css, a rule for that class gives it a pale green background.

00:26 Save. The green shows, but the heading sits right against its top edge. And between the banner and the green, there’s a white strip.

00:35 The heading already has a top margin from the browser, so it should sit away from the green. Maybe that margin is too small. Add a rule for the section’s heading with a margin-top of 24 pixels.

00:47 Your turn: pause the video and predict. Where will those 24 pixels show up: as green above the heading, or somewhere else? They show up in white, above the green.

01:00 The strip grows from about 17 pixels to 24: the 17 was the heading’s default margin, and your 24 replaced it. The heading still touches the green’s top edge.

01:13 Open DevTools, and hover over the heading. Its orange margin sits above the green, over the white strip. Now click the section’s row. Its box starts where the heading starts, and in the Computed tab, its box diagram shows a margin of 0.

01:30 So the white strip is the heading’s margin, outside the section.

01:35 To see why, look at two blocks inside the work day card: its heading and its date paragraph. The heading’s bottom margin is about 19 pixels. The paragraph’s top margin is 16, and it sits in the same gap, not below it.

01:51 When the vertical margins of two blocks in normal flow touch, the browser collapses them into one, the size of the larger. Watch out: they don’t add up. The gap is 19 pixels, not 35.

02:06 The section’s edge doesn’t stop this. Its 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, and it pushes the whole section down.

02:21 The box diagrams still show each element’s own margin, 0 and 24. The collapse happens between them, on the page. The section has no set height, so the same happens at the bottom, with its last child, the seed swap card.

02:37 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.

02:50 To keep a child’s margin inside its parent, put something between them. Add padding of 24 pixels to the section’s rule. Save. The white strip is gone, and at the bottom, the card’s margin is inside the green now too.

03:05 Hover over the heading again. Its margin is inside the green, under the padding. A border between them would also stop the collapse.

03:13 That’s 48 pixels of room, more than the heading needs. Deleting your rule would bring back the heading’s default margin, so set its margin-top to 0, and let the padding make the room.

03:25 Your cards already do the same, with padding on the card and no top margin on its heading.

03:32 So 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.

03:43 Padding or a border on that edge is one way to keep it inside. The Coming Up section is green now, with room inside it. Next is the lesson Center a Column With Auto Margins, where you’ll center the events column in a wide window.

Become a Member to join the conversation.