C
HTML/CSS/CSS Basics/Lesson 07

The Box Model — *Every Element Is a Rectangle*

45 min·theory
This chapter
3/3

The Box Model — *Every Element Is a Rectangle*

🎯 By the End of This Lesson

Once you finish this lesson, you'll be able to confidently handle these three things:

  • ✅ The four regions: Content / Padding / Border / Margin
  • ✅ Why box-sizing: border-box is the modern standard
  • ✅ Patterns for resolving margin collapsing

Keep these goals as a checklist — close the lesson only when you can answer all of them.

Every HTML Element Is a *Box*

The One-Line Takeaway

Every HTML element is rendered as a rectangular box. That box has four concentric layers: content - padding - border - margin.

code
        ┌─────── margin ───────┐
        │   ┌─── border ────┐   │
        │   │  ┌─ padding ─┐ │   │
        │   │  │ content  │ │   │
        │   │  └──────────┘ │   │
        │   └────────────────┘   │
        └────────────────────────┘

What Each Region Means

  • content — the actual content (text, images)
  • padding — the space between content and border; the inner gap
  • border — the outline surrounding the element
  • margin — the gap between this element and others; the outer space
css
.card {
    width: 300px;
    padding: 20px;
    border: 2px solid gray;
    margin: 10px;
}

The Biggest Source of Confusion — What width Really Means

With the default content-box sizing:

css
.card {
    width: 300px;      /* content only: 300 */
    padding: 20px;      /* 20 on each side = +40 */
    border: 2px;        /* 2 on each side = +4 */
}
/* Actual rendered width: 300 + 40 + 4 = 344px */

You wrote width: 300px, but the element actually takes up 344px. That's counterintuitive.

border-box — The Intuitive Approach

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

.card {
    width: 300px;      /* 300 including border */
    padding: 20px;     /* included */
    /* Actual rendered width: exactly 300px */
}

The modern CSS standard — every project starts with box-sizing: border-box. width means actual width.

Two Traps with margin

1. Vertical margin collapsing:

html
<div style="margin-bottom: 30px;">A</div>
<div style="margin-top: 20px;">B</div>

Vertical margins merge into the larger value. The gap between A and B is 30px, not 50px.

2. Vertical margin has no effect on inline elements:

css
span { margin: 20px; }   /* only left/right apply; top/bottom are ignored */

Top and bottom margins don't work on inline elements (<span>, <a>). Switch to display: inline-block instead.

Quick Summary

  • Every element = a 4-layer box (content · padding · border · margin)
  • box-sizing: border-box is the modern standard
  • margin = outer gap, padding = inner space
  • Watch out for vertical margin collapsing and inline element limitations

🤖 Try Asking AI Like This

Once you understand the concepts in this lesson, you can give AI precise instructions. Instead of a vague 'fix it,' you can make vocabulary-driven requests — and that's where token savings begin.

  • "Reorganize the padding/margin of this card component based on box-sizing: border-box"
  • "Fix the collapsing margin issue in this layout"
  • "Set height: 100vh + overflow so these elements fill the viewport completely"

Why This Reduces Tokens

Without the right vocabulary, you end up asking "what does that mean?" after every AI response. Those follow-up questions eat up tokens. Learn the concept once, and the conversation wraps up in a single exchange.

Read this first: Selector Specificity
Box Model - HTML/CSS