Loading video player…

Find Gaps in the Box Diagram

In this lesson, you found out where the extra space above each event card’s heading comes from. You hovered over each box around the gap, read its box diagram in DevTools, tried a value there, and copied the fix into events/events.css.

Follow Along

The video starts from the events page and events/events.css as you left them in See Every Element as a Box, with this rule at the end of the file:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px 24px;
  border: 2px solid var(--green);
  margin-bottom: 24px;
}

Open events/upcoming.html in Chrome or Edge, and open DevTools beside it. With 16px of padding at the top and 24px at the sides, each heading should sit closer to its card’s top edge than the text sits to the left edge, but it doesn’t. Rosa would like each card’s heading closer to the card’s top edge.

Key Points

  • Hover to see the layers. In the Elements panel, open <main>, then the <section>, and hover over the first <article> line.
  • Read the colors. The browser paints the card’s layers on the page: blue for the content, green for the padding, yellow for the border (a thin line here, two pixels wide), and orange for the margin. The white strip between the two cards is this card’s margin. The green band at the top is thinner than the space above the heading, because part of that space is inside the blue. The card’s content is everything inside its padding, not only the text.
  • Read the box diagram. Click the line to select the card, and open the Computed tab. At its top is the box diagram: the same four layers, nested, in the same colors, with a number on each side.
  • Read its numbers. The card’s margin is 24 at the bottom and 0 on the other sides, its border is 2 all round, and its padding is 16 at the top and bottom and 24 at the sides, so the card’s own padding gives only 16 pixels of the space above the heading. The middle shows the content’s width and height.
  • Padding and margin aren’t interchangeable. Padding sits inside the border, under the card’s background. Margin sits outside it and is transparent, so the strip between the cards stays white.
  • Check the boxes inside, too. Hover over the heading’s line, and an orange band appears above it, inside the card. That’s the heading’s own margin, the blank stretch of blue you saw on the card, and it comes from the browser’s default styles. Select the heading, and its box diagram shows a top margin of 18.720. Part of the space above the heading is the card’s padding, and the rest is the heading’s margin.
  • Try a fix in the diagram. Double-click the heading’s top margin, type 0, and press Enter. The heading moves up, and the space above it is now the card’s padding alone. DevTools writes the edit into a style attribute on that one heading, so the second card’s heading doesn’t move. Nothing in events/events.css changes, so a reload loses the edit.
  • The rule: when a gap surprises you, hover over each box around it and read the colors. Then check the numbers in the box diagram, try a value there, and copy the one that works into your file.

Final Code

Copy the fix into events/events.css as a rule for every card’s heading, after the .event-card rule:

Language: CSS Filename: events/events.css
.event-card h3 {
  margin-top: 0;
}

Save, and the rule applies to both cards, so the second card’s heading moves up to match the first.

Next is Size Boxes With border-box, where you’ll see why a box can come out wider than the width you give it.

00:00 In the last lesson, you turned the two events into cards. In this video, you’ll find out which layer of which box makes a gap.

00:09 Look at the work day card. You gave it 16 pixels of padding at the top and 24 at the sides. So its heading should sit closer to the top edge than the text sits to the left edge.

00:20 It doesn’t. Rosa would like the heading closer to the top. The last lesson said the heading brings a margin of its own. Here’s how to see which box owns which part of that space.

00:33 Open DevTools on the events page. In the Elements panel, open the main element, then the section. Hover over the first article’s line, and the browser paints the card’s layers right on the page.

00:46 Each layer gets its own color. Blue is the content. Green is the padding. Yellow is the border, a thin line here, two pixels wide. Orange is the margin. The white strip between the cards is this card’s margin.

01:03 And the green band at the top is thinner than the space above the heading, because part of that space is inside the blue. The card’s content is everything inside its padding, not only the text.

01:16 Click the line to select the card, and open the Computed tab. At its top is the box diagram: the same four layers, nested, in the same colors, with a number on each side.

01:28 The margin is 24 at the bottom and zero on the other sides. The border is 2 all round. The padding is 16 at the top and bottom, and 24 at the sides. So the card’s own padding gives only 16 pixels of the space above the heading.

01:46 In the middle are the content’s width and height. Watch out: padding sits inside the border, under the card’s honeydew. Margin sits outside it and is transparent, so the strip between the cards stays white.

01:59 The two aren’t interchangeable.

02:03 Now find where the rest comes from. Open the first article, and hover over the heading’s line. An orange band appears above it, inside the card. The heading has a margin of its own.

02:14 That’s the blank stretch of blue you saw on the card. It comes from the browser’s default styles, like the paragraph margins you met in the last lesson.

02:23 Select the heading, and its box diagram shows a top margin of 18.72 pixels.

02:30 So the space above the heading belongs to two boxes. Part is the card’s padding, and the rest is the heading’s margin.

02:39 Try a fix right in the diagram. Double-click the heading’s top margin, type zero, and press Enter. The heading moves up, and the space above it is now the card’s padding alone.

02:52 But the second card’s heading hasn’t moved. The Elements panel shows why. DevTools wrote your edit into a style attribute on this one heading. Nothing in events.css changed, so a reload loses it.

03:06 So copy the fix into events.css as a rule for every card’s heading. Save, and the rule applies to both cards. The second heading moves up to match the first.

03:18 When a gap surprises you, hover over each box around it and read the colors. Then check the numbers in the box diagram, try a value there, and copy the one that works into your file.

03:31 The space above each heading now comes from a value you chose. Next is the lesson Size Boxes With border-box, where you’ll see why a box can come out wider than the width you give it.

Become a Member to join the conversation.