The box model

Topic 1 of 4 in The box model7 min readBeginnerFree course

Overview

Every element on a page — a heading, a link, an image, a <div> — is laid out as a rectangular box. Understanding how that box is built is the foundation of all CSS layout. Most “why is this 20 pixels too wide?” questions are answered by the box model.

The four layers

The box model as nested rectangles: content in the centre, sized by width and height; padding around it — inner space that shows the background; the border — the edge you can see; and margin outside — transparent space that pushes boxes apart.

From the inside out:

  1. Content — the text, image or child elements. Its size is set by width and height (or by the content itself).
  2. Padding — space between the content and the border. It’s inside the box, so the element’s background colour or image shows through it.
  3. Border — a line around the padding. It can have a width, style and colour, and it takes up space.
  4. Margin — space outside the border that separates the box from its neighbours. Margins are always transparent.
CSS
.card {
  width: 20rem;
  padding: 1rem;
  border: 2px solid #2d5bd7;
  margin: 1.5rem;
  background: #eaf0fc;   /* fills content and padding, not margin */
}

Padding

Padding gives content breathing room inside its box — without it, text would touch the border.

CSS
padding: 1rem;                 /* all four sides */
padding: 1rem 2rem;            /* top & bottom | left & right */
padding: 1rem 2rem 0.5rem;     /* top | left & right | bottom */
padding: 1rem 2rem 0.5rem 0;   /* top | right | bottom | left */

padding-top: 1rem;             /* one side at a time */
padding-inline: 2rem;          /* left and right (logical) */
padding-block: 1rem;           /* top and bottom (logical) */

The one-to-four value pattern goes clockwise from the top — top, right, bottom, left (remember “TRouBLe”). It’s shared by margin, border-width, border-style, border-color and inset.

Padding can’t be negative. Percentages in padding are calculated from the width of the containing block — even for top and bottom padding — which was once the standard trick for aspect ratios (now replaced by aspect-ratio).

Border

CSS
border: 2px solid #2d5bd7;              /* width style colour */
border-bottom: 1px dashed #9a9ea7;      /* one side */
border-radius: 12px;                    /* rounded corners */
border-radius: 50%;                     /* a circle, if the box is square */

A border needs a style to appear — solid, dashed, dotted, double… The default style is none, so border: 2px #2d5bd7 shows nothing.

border-radius rounds the corners of the border and the background, and doesn’t change the box’s size.

Outline is not border

outline draws a line around an element without taking up space, so it never shifts the layout. Browsers use it for focus indicators. Never remove focus outlines without replacing them (see Layouts that work for everyone).

Margin

Margins push boxes apart:

CSS
h2 { margin-block: 2rem 0.5rem; }   /* more space above a heading than below */
.button + .button { margin-inline-start: 0.5rem; }

Margins have some special abilities:

  • auto — in normal flow, margin-inline: auto on a block with a set width centres it horizontally by sharing the leftover space equally on both sides.
  • Negative values — pull an element towards (or over) its neighbours. Useful occasionally; confusing often.
  • Collapsing — adjacent vertical margins in normal flow merge into one. This surprises everyone and gets its own lesson, Margins and collapsing.
CSS
.page {
  max-width: 60rem;
  margin-inline: auto;   /* centred column */
}

In flex and grid layouts, the gap property often replaces margins for spacing between items — it’s cleaner because it only adds space between items, never at the edges.

Adding it up

With the default box-sizing: content-box, width and height set only the content area. Padding and border are added on the outside:

Text
rendered width = width + padding-left + padding-right + border-left + border-right

.card { width: 20rem; padding: 1rem; border: 2px solid; }
      = 320 + 16 + 16 + 2 + 2 = 356px

Margins don’t count towards the element’s own size, but they do take up space in the layout — the card above occupies 356px plus its margins.

This addition is the source of countless layout bugs: you set width: 50% on two side-by-side columns, add some padding, and suddenly they don’t fit. The next lesson shows the fix that almost every stylesheet uses: box-sizing: border-box.

Seeing the box model

In DevTools, select an element and open the Computed tab: the box model diagram shows each layer’s exact size, and hovering an element on the page colours its content, padding, border and margin. Use it whenever an element’s size surprises you.

Rule of thumb: use padding for space inside a component (between its border and its content) and margin or gap for space between components.

Exercise

A pricing card

Style a .plan card so that: its content area is 18rem wide; it has 1.5rem of padding on all sides; a 2px solid border in #2d5bd7; rounded corners of 12px; and 2rem of space below it (so a second card sits 2rem lower). Using the default box-sizing, calculate the card’s total rendered width in pixels, then confirm it in DevTools.

HTML
<article class="plan">
  <h2>Pro</h2>
  <p>All courses, certificates and projects.</p>
</article>
<article class="plan">
  <h2>Team</h2>
  <p>Everything in Pro, for five people.</p>
</article>

<style>
  /* your code here */
</style>

Summary

  • Every element is a rectangular box made of content, padding, border and margin.
  • Padding is inside the border and shows the background; margin is outside and is always transparent.
  • With the default box-sizing: content-box, width sets only the content; padding and border are added on top.
  • Shorthands take one to four values in the order top, right, bottom, left (clockwise).
  • Margins can be negative or auto; padding can’t be negative.