Codelab 3: Predictable Layouts with border-box
Fix the box model's width surprise and build a clean card grid.
The width surprise
Open your project from Codelab 2: cd ~/box-model-lab && code . Open index.html in the browser — press Ctrl+R if it's already open, or run xdg-open index.html.
In style.css, make sure .card has these properties: width: 300px, padding: 20px, and border: 2px solid #ccc.
Save the file and refresh the browser. Now here's the question: how wide is each card on screen? You set width to 300px, so it should be 300px... right?
Open DevTools (F12), hover over a card, and look at the dimensions shown. It says 344px wide! Where did the extra 44px come from?
Here's the math: • 300px (content width you set) • + 20px (left padding) • + 20px (right padding) • + 2px (left border) • + 2px (right border) • = 344px total width on screen
By default, the CSS width property only controls the content area. Padding and border are added on top of that width. This default behavior is called content-box.