Chapter 21: Box Model
The Box Model
If you understand the box model properly today, 70–80% of your future “why is this element too wide?”, “why is there extra space?”, “why isn’t my padding working?” questions will disappear forever.
So let’s go very slowly, step-by-step, with drawings in words, common traps, and real code you can copy-paste right now.
1. The Core Idea – Everything is a Box
In CSS, every visible element on the page is a rectangular box — even text, images, buttons, divs, headings… everything.
No matter how round the corners look, how many shadows you add — underneath it’s still a box.
This box has four layers (from inside to outside):
- Content → the actual stuff (text, image, video…)
- Padding → breathing space inside the border
- Border → the line/frame around the padding
- Margin → space outside the border (separates this box from neighbors)
Visual in words:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
margin (outside space) ┌───────────────────────────────┐ │ border │ │ ┌───────────────────────┐ │ │ │ padding │ │ │ │ ┌───────────────┐ │ │ │ │ │ content │ │ │ │ │ └───────────────┘ │ │ │ └───────────────────────┘ │ └───────────────────────────────┘ |
2. The Two Box-Sizing Modes (This is Where Most People Get Confused)
CSS gives two ways to calculate the total width & height of the box.
Old & Annoying Way (default until you change it): content-box
|
0 1 2 3 4 5 6 7 8 |
width: 300px; padding: 20px; border: 10px solid black; |
→ Total width = 300 + 20 + 20 + 10 + 10 = 360px → Padding and border are added outside the width you set → very frustrating math!
Modern & Sensible Way (what everyone uses in 2026): border-box
|
0 1 2 3 4 5 6 7 8 |
* { box-sizing: border-box; } |
→ Total width = 300px (exactly what you wrote) → Padding + border are eaten inside the 300px → life becomes peaceful
Golden rule (do this in every project forever):
|
0 1 2 3 4 5 6 7 8 9 10 |
*, *::before, *::after { box-sizing: border-box; } |
Put this at the very top of your CSS — it changes everything to border-box (including pseudo-elements).
3. Real Example – Let’s See Both Modes Side by Side
index.html
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Box Model Explained – Webliance</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h1>Box Model – content-box vs border-box</h1> <section class="demo"> <div class="box content-box"> <h2>content-box (default)</h2> <p>width: 300px<br>padding: 30px<br>border: 10px</p> <p class="size-info">Actual total width: 380px</p> </div> <div class="box border-box"> <h2>border-box (modern)</h2> <p>width: 300px<br>padding: 30px<br>border: 10px</p> <p class="size-info">Actual total width: 300px</p> </div> </section> <section class="explanation"> <h2>Quick Summary</h2> <ul> <li><strong>content-box</strong>: width = content only → padding + border added outside</li> <li><strong>border-box</strong>: width = content + padding + border → total stays what you set</li> <li><strong>margin</strong> is always outside — never included in width</li> <li>Use <code>box-sizing: border-box</code> everywhere — it's the 2026 standard</li> </ul> </section> </div> </body> </html> |
style.css
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/* The golden reset – do this always */ *, *::before, *::after { box-sizing: border-box; } body { font-family: system-ui, sans-serif; line-height: 1.6; background: #f8fafc; color: #1e293b; padding: 40px 20px; } .container { max-width: 900px; margin: 0 auto; } h1 { text-align: center; color: #1d4ed8; margin-bottom: 3rem; } .demo { display: flex; gap: 4rem; justify-content: center; margin: 4rem 0; flex-wrap: wrap; } .box { width: 300px; padding: 30px; border: 10px solid; background: white; border-radius: 12px; box-shadow: 0 6px 20px rgba(0,0,0,0.08); text-align: center; } /* content-box (shows the problem) */ .content-box { box-sizing: content-box; /* ← this is the old default */ border-color: #ef4444; } .content-box .size-info { color: #991b1b; font-weight: bold; } /* border-box (the modern way) */ .border-box { border-color: #10b981; } .border-box .size-info { color: #065f46; font-weight: bold; } .explanation { background: #f1f5f9; padding: 2.5rem; border-radius: 12px; border-left: 6px solid #3b82f6; } ul { margin-left: 2rem; margin-top: 1.5rem; } li { margin-bottom: 1rem; } |
Now open it:
- The red-bordered box is 380px wide (content-box pain)
- The green-bordered box is exactly 300px wide (border-box happiness)
That difference is why everyone uses border-box globally.
4. Margin Collapse – The Sneaky One
Vertical margins collapse (merge) — this surprises almost everyone.
Example:
|
0 1 2 3 4 5 6 7 |
<p class="p1">First paragraph</p> <p class="p2">Second paragraph</p> |
|
0 1 2 3 4 5 6 7 |
.p1 { margin-bottom: 40px; } .p2 { margin-top: 60px; } |
→ Space between them = 60px (not 100px) The bigger margin wins — they collapse into one.
Horizontal margins don’t collapse.
5. Quick Mastery Checklist – Box Model
- Always use * { box-sizing: border-box; }
- Width usually means total outer width (with border-box)
- Padding = inside space, border = frame, margin = outside space
- Margin collapse only happens vertically between block elements
- Use DevTools → hover element → see the box model diagram (colored layers)
How does the demo look on your screen? Try changing box-sizing back to content-box on the first box — see how it suddenly becomes wider?
Next possible steps (tell me what you want):
- “margin vs padding deep dive with examples”
- “how to center elements using box model”
- “box-shadow & border-radius tricks”
- “flexbox – next after box model”
- “common box model bugs & fixes”
You’ve just unlocked one of the biggest “aha!” moments in CSS. Chai second round? Then let’s keep building! 🚀 😄
