Skip to the content

Lesson 17 of 296

The box model

What you will learn

  • Why every element is a box
  • The four layers of the box
  • How width is really calculated
  • box-sizing: border-box and why everyone uses it
  • Margin collapsing
  • Outline vs border
On this page

Every element is a box

The browser draws every element as a rectangle, even round-looking buttons and single words. Understanding this box is the key to all CSS layout.

The four layers

From the inside out:

+-------------------------------- margin --------------------------------+
|  +------------------------------ border ----------------------------+  |
|  |  +-------------------------- padding ------------------------+  |  |
|  |  |                                                           |  |  |
|  |  |                   content (text, image)                   |  |  |
|  |  |                                                           |  |  |
|  |  +-----------------------------------------------------------+  |  |
|  +-----------------------------------------------------------------+  |
+------------------------------------------------------------------------+
LayerWhat it isHas the background colour?
ContentThe text or imageYes
PaddingSpace inside the border, around the contentYes
BorderA line around the paddingHas its own colour
MarginSpace outside the border, pushing other elements awayNo, always see-through
.card {
  padding: 16px;
  border: 1px solid #ccc;
  margin: 24px;
}

Writing padding and margin

WrittenMeans
margin: 10px;All four sides 10px
margin: 10px 20px;Top and bottom 10px, left and right 20px
margin: 10px 20px 30px;Top 10px, left and right 20px, bottom 30px
margin: 10px 20px 30px 40px;Top, right, bottom, left (clockwise from the top)
margin-top: 10px;One side only

Remember the four-value order with the word TRouBLe: Top, Right, Bottom, Left.

Centring a block horizontally:

.container {
  width: 800px;
  margin: 0 auto;
}

auto on the left and right shares the free space equally.

How width is calculated: the trap

By default, width sets only the content width. Padding and border are added on top.

.box {
  width: 300px;
  padding: 20px;
  border: 5px solid black;
}

Real visible width = 300 + 20 + 20 + 5 + 5 = 350px. Not 300.

This surprises almost everyone. It is called the content-box model.

The fix: box-sizing: border-box

.box { box-sizing: border-box; }

Now width: 300px means the whole box including padding and border is 300px. The content shrinks to fit. This is much easier to think about.

Nearly every project starts with this rule for every element:

*,
*::before,
*::after {
  box-sizing: border-box;
}

Margin collapsing

When two vertical margins touch, they do not add up. Only the larger one is used. This is called margin collapsing.

h2 { margin-bottom: 30px; }
p  { margin-top: 20px; }

The gap between the heading and the paragraph is 30px, not 50px.

Facts about collapsing:

  • It only happens with top and bottom margins, never left and right.
  • It does not happen inside Flexbox or Grid containers.
  • A child's top margin can "escape" through its parent if the parent has no padding or border. Adding padding to the parent, or making it a flex or grid container, stops this.

Padding vs margin: which one?

  • Padding for space inside an element (between the text and the edge of a button).
  • Margin for space between elements.
  • In Flexbox and Grid layouts, prefer the gap property for space between items (Lessons 21 and 22).

Outline vs border

BorderOutline
Part of the box modelYesNo
Takes space and moves other elementsYesNo, drawn on top
Main useDesignFocus rings for keyboard users

Seeing the box model

In Chrome dev tools, select an element and open the Computed tab. It shows a diagram of the content, padding, border and margin with exact numbers. Use it whenever spacing looks wrong.

Common mistakes

  • Expecting width: 300px with padding to be 300px wide, without border-box.
  • Being surprised by margin collapsing.
  • Using margins on every item instead of gap in Flexbox and Grid.
  • Removing outlines without adding another clear focus style.

How to say it in an interview

"Every element is a box made of content, padding, border and margin. By default, width only sets the content, so padding and border are added on top. I set box-sizing to border-box on everything so width includes padding and border. Vertical margins between blocks collapse, so only the larger one applies, but that does not happen in flex or grid containers."

Key words

WordMeaning
Box modelThe content, padding, border and margin of every element
PaddingSpace inside the border
MarginSpace outside the border
border-boxWidth and height include padding and border
Margin collapsingTouching vertical margins merging into the larger one
OutlineA line drawn outside the border that takes no space

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.