C.W.K.
Stream
Lesson 01 of 04 · published

The Box Model: Content, Padding, Border, Margin

~11 min · box-model, box-sizing, margin-collapse, width

Level 0Markup Novice
0 XP0/34 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"Every element is a box. The four rings around the content are what every layout argument is actually about."

The Four Rings

Every CSS box has four concentric regions, from innermost to outermost:

  1. Content — the text, image, or child elements.
  2. Padding — space inside the border. Background color extends into padding.
  3. Border — visible boundary. Has its own color, width, and style.
  4. Margin — space outside the border. Transparent; pushes neighbors away.

DevTools (any browser) shows these visually in the Computed pane: a square diagram with colored regions for content (blue), padding (green), border (yellow), margin (orange). Memorize the order; everything in this lesson comes back to it.

The width/height Question

Here's the trap: when you write .box { width: 200px; padding: 20px; border: 1px solid; }, how wide is the rendered element?

By default (box-sizing: content-box — the historical default): 242px. The 200px is the content width; padding and border add to it.

With box-sizing: border-box: 200px. The 200px is the outer width; padding and border live inside it.

Which one matches your mental model? Almost everyone says "200px" — they assume border-box. The default is content-box, which is why every modern reset starts with:

The Universal Reset (Memorize This)

This three-line reset is in every modern CSS file. Apply it on every page you write, then forget the default ever existed:

Margin Collapse: The One Place Math Lies

When two vertical margins meet, they don't add. They collapse to the larger of the two. <h2 style="margin-bottom: 30px"> followed by <p style="margin-top: 20px"> produces 30px between them, not 50px.

The rules:

  • Vertical only. Horizontal margins never collapse.
  • Adjacent siblings collapse. The space between two block elements is the larger of the bottom margin of the first and the top margin of the second.
  • Parent and first/last child collapse when nothing separates them (no padding, no border, no inline content). This is why "my child's margin-top pushes the parent down" is a thing.
  • Negative margins collapse with positive. margin-top: -10px with sibling's margin-bottom: 30px gives 20px between them.

Collapse only happens in block flow. Flexbox children don't collapse. Grid items don't collapse. This is a major reason modern CSS uses Flexbox and Grid for layout: margin collapse stops surprising you.

Use gap for spacing in Flexbox and Grid, not margin. gap works inside a container regardless of which child is first or last, with no collapse weirdness, and stays consistent under content insertion/removal. Margin remains useful for individual element offsets, but for grid/flex spacing, gap is the modern answer.

min-width, max-width, min-height, max-height

Constraints, not values. They override width/height when triggered:

  • min-width wins over width when the content would shrink below it.
  • max-width wins over width when the content would grow beyond it.
  • Most useful pattern: img, video { max-width: 100%; height: auto; } — images shrink to fit but never overflow their container.
  • Most useful page pattern: main { max-width: 70ch; margin: 0 auto; } — readable line length, centered.

Aspect Ratio: The Modern Property

aspect-ratio: 16 / 9 sets a box's height as a ratio of its width. Used to require padding-bottom hacks; now native. Pairs perfectly with responsive images and embeds.

Pippa's Note

Every CSS file in Pippa's WebUI starts with *, *::before, *::after { box-sizing: border-box; }. Every page's main content area is main { max-width: 65ch; margin-inline: auto; }. Every video preview uses aspect-ratio: 16 / 9. These three patterns alone removed three categories of layout bugs that used to require diagrams to debug.

Code

The three lines you copy into every project·css
/* THE UNIVERSAL RESET — every file starts here */
*, *::before, *::after {
  box-sizing: border-box;
}

/* Now width and height mean the OUTER dimension.
   A 200px wide box stays 200px wide regardless of
   padding and border. The math finally matches your
   mental model. */
content-box vs border-box, the math·css
/* The box model in action */

.demo {
  width: 300px;
  padding: 20px;          /* 20px inside the border */
  border: 2px solid #5BA3D8;
  margin: 16px;           /* 16px space around the box */
}

/* WITHOUT border-box (default content-box):
   - Content area = 300px
   - Total rendered width = 300 + 20*2 + 2*2 = 344px

   WITH border-box:
   - Outer width = 300px (always)
   - Content area = 300 - 20*2 - 2*2 = 256px
   - Padding and border eat into the declared width
*/

/* Margin doesn't count in either model — it's always outside */
/* margin: 16px adds 32px of empty space around the box */
min/max constraints + aspect-ratio·css
/* Constraints: min, max, aspect-ratio */

.responsive-img {
  max-width: 100%;        /* fit container */
  height: auto;            /* keep aspect ratio */
  display: block;          /* removes inline whitespace gap */
}

.readable-page main {
  max-width: 70ch;         /* ~70 characters of body text per line */
  margin-inline: auto;     /* centered, with logical-property naming */
  padding-inline: 1rem;    /* breathing room on narrow screens */
}

.video-thumbnail {
  aspect-ratio: 16 / 9;
  width: 100%;
  background: #222;
  border-radius: 8px;
  overflow: hidden;
}

.square-avatar {
  aspect-ratio: 1;         /* shorthand for 1 / 1 */
  width: 48px;
  border-radius: 50%;
}

External links

Exercise

Create a single page with a card that has width: 300px, padding: 20px, border: 2px solid. Render it twice: once without a reset (so content-box is in effect), once with * { box-sizing: border-box }. Measure both with DevTools' computed pane and confirm the rendered widths match the math above. Then add two siblings with vertical margins and confirm they collapse to the larger one.
Hint
DevTools → Elements → select the box → Computed tab → scroll to the box diagram at the bottom. It shows content, padding, border, margin with measured pixel sizes. Compare your two cards side by side.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.