CSS Essentials·Foundations

The box model

Content, padding, border and margin — the four boxes behind every element.

Beginner 13 min read Lesson 2 of 480 Challenge included
What you will be able to do
  • Measure an element's size
  • Use border-box sizing
  • Collapse margins intentionally

Every element is a rectangle made of four layers. Confusion about layout usually comes from forgetting one of them.

Live exampleAnatomy of a box
reset.cssCSS
*, *::before, *::after {
  box-sizing: border-box;
}

.card {
  width: 320px;
  padding: 24px;
  border: 1px solid #e7e0d6;
  margin-block: 16px;
}
Your turn

Fix the overflowing card

Easy

A 400px card with 32px padding overflows its 400px container. Add the global border-box reset.

CSS · starter
.card {
  width: 400px;
  padding: 32px;
}
Check your understanding before moving on.
Knowledge checkQuestion 1

With box-sizing: border-box, what does width: 300px include?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Color, units and type
Lesson 2 of 480 · 0% of course