Skip to content

Beginner · CSS basics · topic 16 of 64 · 30 min

The box model

Explanation

Every element is a rectangle box with four layers: the content, padding around it, a border, and margin outside. Setting box-sizing: border-box makes widths easy to predict. The display property decides whether boxes sit on their own line or flow inside text.

Four layers of every box

  • Content: the text or image itself.
  • Padding: space inside the box, between content and border. It takes the background colour.
  • Border: a line around the padding.
  • Margin: space outside the border that pushes other boxes away. It is always transparent.
Think of it like this: Think of a framed photo on a wall. The photo is the content, the white mount around it is the padding, the wooden frame is the border, and the gap between this frame and the next one is the margin.
css
.card {
  width: 300px;
  padding: 16px;          /* all four sides */
  border: 2px solid #ddd;
  margin: 24px auto;      /* 24px top and bottom, centred left-right */
}
/* Shorthand order: top right bottom left (clockwise) */
.note { padding: 8px 16px 8px 16px; }
Padding, border and margin on a card

box-sizing: the width surprise

By default (content-box), width means only the content. Padding and border are added on top: 300px + 2 x 16px + 2 x 2px = 336px wide. With box-sizing: border-box, width includes padding and border, so the card is exactly 300px. Almost every project sets this for all elements.

css
*, *::before, *::after {
  box-sizing: border-box;
}
The common reset at the top of a stylesheet

display: how boxes sit

ValueBehaviourDefault for
blockStarts on a new line, takes the full widthdiv, p, h1, section, ul
inlineFlows inside text; width and height are ignoredspan, a, strong
inline-blockFlows inside text but accepts width, height and paddingOften used for badges
noneRemoved from the page completelyHidden elements
flex / gridLay out the children (intermediate level)Layouts

Width, max-width and centring

width: 100% fills the parent. max-width stops a box from growing too wide on big screens while letting it shrink on phones. A block with a max-width and margin-inline: auto (or margin: 0 auto) sits in the centre.

css
.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 16px;
}
A centred page container that still fits small phones

Margin collapsing and overflow

When two block boxes sit one above the other, their vertical margins do not add up; the bigger one wins. This is called margin collapsing. And when content is bigger than its box, overflow decides what happens: visible (spills out), hidden (cut off), or auto (scroll bar when needed).

Tip: In DevTools, select an element and look at the box diagram in the Computed tab. It shows the exact content, padding, border and margin in pixels.
Careful: Avoid fixed heights for boxes that contain text. Text can grow (bigger font settings, longer translations in Hindi or Tamil) and will overflow. Let the height follow the content.

Watch the lesson

The box model · 1/6

Everything is a box

Every element is a box. Learning the box model removes half of all CSS confusion.

Prefer a video by a teacher? Find free videos on “css box model margin padding border box-sizing tutorial” on YouTube ↗

In real life: A product card on a shopping site

Each product card has 16px padding so the photo and price do not touch the edge, a thin grey border, rounded corners and a 12px margin from the next card. With box-sizing: border-box, the developer can say "each card is 50% wide" on phones and know two cards fit side by side exactly.

Practice (0/2 done)

Task 1 · Code · HTML

Style the .card: padding 16px, a border "1px solid #cccccc", margin-bottom 12px, and box-sizing border-box.

Task 2 · Code · Javascript

Write a function boxWidth(width, padding, border, sizing) that returns the total width a box takes on screen (without margin). With sizing "content-box", padding and border are added on both sides; with "border-box" the total is just width. Example: boxWidth(300, 16, 2, "content-box") is 336.

Quick quiz

Question 1

1.Which layer is between the content and the border?

Question 2

2.With box-sizing: border-box and width: 200px, padding: 20px, how wide is the box?

Question 3

3.Which display value removes an element from the page?

Question 4

4.Why is width ignored on a <span>?

Question 5

5.What does margin: 0 auto do to a block with max-width?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Selectors, specificity and the cascade