Find a course

Module 3: Styling with CSSLesson 6 of 1112 min

The Box Model and Spacing

Understand content, padding, border and margin, calculate how wide an element really is, and fix the spacing problems every beginner runs into.

Step 1 of 10Learn

Everything is a box

Here's the secret that makes CSS layout click: every element on a page is a rectangle. Headings, paragraphs, images, even links are boxes. Each box has four layers, from the inside out:

  1. Content: the text or image itself, sized by width and height.
  2. Padding: space inside the box, between the content and the border. It takes the box's background color.
  3. Border: a line around the padding, like border: 2px solid black;.
  4. Margin: space outside the border that pushes other boxes away. It's always transparent.
.card {
  width: 300px;
  padding: 20px;
  border: 2px solid #2b4c7e;
  margin: 16px;
}

Open your browser's developer tools (right-click, then Inspect) and hover over any element. The browser highlights the content, padding and margin in different colors and shows the exact numbers. It's the best way to see why something sits where it does.

A handy shortcut: padding: 10px 20px; sets top and bottom to 10px and left and right to 20px. With four values, margin: 5px 10px 15px 20px;, the order goes clockwise from the top: top, right, bottom, left.

Step 2 of 10Practice · Diagram

The box model, outside in

Tap the layer that adds space inside the colored box around the text.

Tap the shape that answers the question, then press Check.

The box model, outside inLayer diagram: Margin, Border, Padding, Content.MarginBorderPaddingContent
Margin

Transparent space outside the border, pushing other boxes away.

Border

A line around the padding.

Padding

Space inside the border, around the content; shows the background color.

Content

The text or image itself.

Step 3 of 10Tap to reveal

The four layers, inside out

  1. Content

    The actual text or image. Its size comes from the width and height you set, or from the content itself.

  2. Padding

    Breathing room inside the box. Increase it when text feels cramped against the edge of a colored card or button.

  3. Border

    The visible outline, set with a width, a style (solid, dashed, dotted) and a color. A border of 0 removes it.

  4. Margin

    Distance from neighboring boxes. Increase it to separate two cards; set the left and right margins to auto on a box with a width to center it on the page.

Step 4 of 10Learn

How wide is the box, really?

Mia sets a card to width: 300px, adds padding: 20px and a 2px border, puts two cards side by side in a 600-pixel space, and the second card drops to the next line. Why?

By default, width sets only the content width. Padding and border are added on top. So her card actually takes up:

300 + 20 + 20 + 2 + 2 = 344 pixels of visible box, plus any margin.

Two of those are 688 pixels, which doesn't fit in 600.

The fix almost every professional site uses is one line at the top of the style sheet:

* { box-sizing: border-box; }

With border-box, the width you set includes the padding and border. A 300px card is 300 pixels from border edge to border edge, and the content shrinks to make room. The * selector means "every element," so the rule applies everywhere.

Margins are still added outside. And there's one more surprise: when two vertical margins meet, like the bottom margin of one paragraph and the top margin of the next, they collapse into the larger of the two rather than adding up. A 20px bottom margin above a 30px top margin gives a 30px gap, not 50.

Step 5 of 10Try the calculator

Box size calculator (default content-box)

Change the values to see how much space a box takes up with the default sizing, where padding and border are added to the width.

Visible box width (border to border)
344 px
Total space taken including margin
376 px
Content width if box-sizing were border-box
256 px

Step 6 of 10Practice · Work it out

With default sizing, a button has width: 120px, padding: 10px on every side, and a 3px border. How many pixels wide is the visible button, from border edge to border edge?

Step 7 of 10Practice · Chart

Mia's card: width: 300px, padding 20px, border 2px

Tap the sizing where the card is exactly 300px wide.

Tap the bar that answers the question, then press Check.

Mia's card: width: 300px, padding 20px, border 2pxStacked bar chart with 2 categories: content-box (default): Content 300 px, Padding 40 px, Border 4 px; border-box: Content 256 px, Padding 40 px, Border 4 px.0 px100 px200 px300 px400 pxcontent-box (default)border-box344 px300 px
Mia's card: width: 300px, padding 20px, border 2px
ItemContentPaddingBorder
content-box (default)300 px40 px4 px
border-box256 px40 px4 px

Step 8 of 10Note

Step 9 of 10Practice · True or false

A paragraph with a 20px bottom margin followed by a paragraph with a 30px top margin will have a 50px gap between them.

Step 10 of 10Practice · What would you do?

What would you do?

Jay's text sits right against the edge of a colored box and looks cramped. He also wants more space between this box and the next one. What should he change?