Skip to content
Codewilds

Lesson 16 of 29 · CSS Basics

CSS Box Model: Padding, Border, Margin and box-sizing

medium20 XP2 min read

Here's the secret behind every CSS layout: every element is a rectangular box. Headings, paragraphs, images, links — all boxes. The box model describes the layers each box is made of, and once you understand it, spacing things out stops being guesswork.

The four layers

From the inside out, every box has:

  1. Content — the text or image itself. width and height size it.
  2. Padding — the space between the content and the border. The background colour fills it.
  3. Border — a line around the padding.
  4. Margin — the space outside the border, which pushes other boxes away. It's always transparent.
.card {
  width: 300px;
  padding: 20px;
  border: 2px solid #333;
  margin: 24px;
}

Every browser can show you these layers: right-click an element, choose Inspect, and look for the box diagram in the developer tools.

Shorthands and single sides

padding and margin take one to four values:

padding: 10px;               /* all four sides */
padding: 10px 20px;          /* 10px top and bottom, 20px left and right */
padding: 5px 10px 15px 20px; /* top, right, bottom, left: clockwise */
margin-top: 32px;            /* one side only */

border combines a width, a style and a colour: border: 2px dashed teal. The style — solid, dashed, dotted and so on — is required. Without it, there is no border at all.

How wide is a box, really?

Here's the surprise: by default, width sets the size of the content only, and the padding and border are added on top. The .card above is 300 + 2 × 20 + 2 × 2 = 344px wide, not 300px.

box-sizing: border-box changes the rule. Now width includes the padding and the border, and the content shrinks to make room. A 300px card is exactly 300px wide, however much padding you give it. That's so much easier that most developers switch it on for every element at once, with the universal selector *:

* {
  box-sizing: border-box;
}

Centring with auto margins

A block with a fixed width can be centred horizontally with margin: 0 auto: the two auto margins on the left and right share the free space equally. One more quirk to know: when two vertical margins meet, they don't add up — the larger one wins. This is called margin collapsing.

Common mistakes

  • A border without a style. border: 2px red draws nothing; add solid.
  • Expecting width to be the final size without box-sizing: border-box.
  • Mixing up padding and margin. Space inside the coloured box is padding; space between boxes is margin.
  • margin: auto without a width. A block that already fills the whole line has no free space to share.

Assignment

  1. Add a rule with the universal selector that gives every element box-sizing: border-box.
  2. Give .card a width of 320px, 24px of padding on every side, a 4px solid #b5432c border and margin: 40px auto (40px above and below, centred).

Thanks to border-box, the finished card is exactly 320px wide, border included.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 16 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.