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:
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
24at the bottom and0on the other sides, its border is2all round, and its padding is16at the top and bottom and24at the sides, so the card’s own padding gives only16pixels 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 astyleattribute on that one heading, so the second card’s heading doesn’t move. Nothing inevents/events.csschanges, 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:
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.