🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
🌐 Web (HTML & CSS)

The Box Model

Every HTML element is a box! The CSS box model has four layers: content (the actual text/image), padding (space inside the border), border (the line around it), and margin (space outside the border). Understanding this is key to spacing elements correctly. Use box-sizing: border-box so padding does…

3 min 10 XP Lesson 17 of 26
The Box Model
🌐

Appy Says…

Why is there mystery space around your element? Why does adding padding make your layout break? The answer is almost always the box model. Understanding this one concept solves 80% of beginner CSS layout puzzles.

📖

What is the CSS Box Model?

Every HTML element is a rectangular box made of four layers, from inside out: contentpaddingbordermargin.

  • Content — the actual text/image area (set by width and height)
  • Padding — space inside the border (between content and border)
  • Border — the visible outline: border: 2px solid black
  • Margin — space outside the border (between this element and neighbours)
  • Default: width only measures content — padding and border are added ON TOP
  • Fix: box-sizing: border-boxwidth includes padding and border
🎮

Think of it like a Minecraft item in a chest

The item is the content. The gap around it inside the slot is padding. The slot border is the border. The gap between adjacent slots is margin. Change any layer and the space the element takes up changes.

⚙️

How It Works

  • 1. Content box: width: 200px; height: 100px;
  • 2. Padding adds space inside: padding: 20px; — total visual width becomes 240px (default)
  • 3. Border adds stroke: border: 2px solid; — total becomes 244px
  • 4. Margin pushes other elements away: margin: 10px;
  • 5. Set globally: *, *::before, *::after { box-sizing: border-box; }
  • 6. With border-box, width: 200px always means 200px total — predictable layouts
🌍

Real-World Examples

  • Card component: padding: 24px gives breathing room around the content
  • Button: padding: 12px 24px makes it larger without changing font size
  • Spacing between sections: margin-bottom: 48px
  • Every major CSS framework (Tailwind, Bootstrap) applies border-box globally first
💡

Key Facts

  • The original CSS box model (content-box) is considered a design flaw — border-box is the modern standard
  • Margin collapse: vertical margins between block elements merge — the larger one wins (not both added)
  • Inline elements (like <span>) ignore width/height and vertical margin
  • outline is drawn outside the border but doesn't affect layout (doesn't take up space)
⚠️

Watch Out!

If you don't set box-sizing: border-box, adding padding to a fixed-width element makes it overflow its container. Set it globally at the top of every stylesheet: *, *::before, *::after { box-sizing: border-box; }.

📌

Remember

content → padding → border → margin. Always use box-sizing: border-box globally. Padding is inside; margin is outside.

What You Learned

  • Every element is a box: content + padding + border + margin
  • box-sizing: border-box makes width predictable — always set it globally
  • Unlocks: predictable layouts, card design, button sizing, spacing control

Key Facts

  • The original CSS box model (content-box) is considered a design flaw — border-box is the modern standard
  • Margin collapse: vertical margins between block elements merge — the larger one wins (not both added)
  • Inline elements (like <span>) ignore width/height and vertical margin
  • outline is drawn outside the border but doesn't affect layout (doesn't take up space)

Real-World Examples

• Card component: <code>padding: 24px</code> gives breathing room around the content • Button: <code>padding: 12px 24px</code> makes it larger without changing font size • Spacing between sections: <code>margin-bottom: 48px</code> • Every major CSS framework (Tailwind, Bootstrap) applies <code>border-box</code> globally first

Remember

content → padding → border → margin. Always use box-sizing: border-box globally. Padding is inside; margin is outside.

Quick Quiz

1 / 2

What is padding?