On this page9 sections
  1. Learning Objectives
  2. 1. The Four Layers
  3. 2. Working Out the Total Size
  4. 3. box-sizing: border-box
  5. 4. Seeing Boxes in DevTools
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 005: The Box Model

Category2: The Box Model
Lecture005 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

To CSS, every element is a rectangle made of four layers: the content, padding around it, a border, and margin outside. Once you can picture these four layers, spacing and sizing stop being a mystery.


Learning Objectives

  • Name the four layers of the box model and what each does
  • Calculate the total size an element takes up
  • Use box-sizing: border-box to make sizes predictable
  • Inspect boxes with the browser's developer tools

1. The Four Layers

output
+--------------------- margin ----------------------+
|  +------------------ border -------------------+  |
|  |  +--------------- padding --------------+   |  |
|  |  |                                      |   |  |
|  |  |        content (text, images)        |   |  |
|  |  |                                      |   |  |
|  |  +--------------------------------------+   |  |
|  +---------------------------------------------+  |
+---------------------------------------------------+
LayerWhat it is
ContentThe text or image itself
PaddingSpace between the content and the border. The background fills it
BorderA line around the padding
MarginSpace outside the border, pushing other elements away

Example

html
<style>
  .box {
    background: #e7f5ff;
    padding: 20px;
    border: 4px solid #1c7ed6;
    margin: 24px;
  }
</style>

<div class="box">Content sits in the middle. Padding is the blue space around it.</div>
<div class="box">The margin keeps the two boxes apart.</div>

2. Working Out the Total Size

By default, width sets the content width only. Padding and border are added on top:

output
width: 200px
padding: 20px  → + 20 left + 20 right
border: 5px    → +  5 left +  5 right
---------------------------------------
total width    = 250px

Example: both boxes say width: 200px, but they're not the same size:

html
<style>
  div { width: 200px; background: #ffe8cc; margin-bottom: 8px; }
  .padded { padding: 20px; border: 5px solid #e8590c; }
</style>

<div>200px wide</div>
<div class="padded">Also width: 200px, but 250px in total</div>

3. box-sizing: border-box

With border-box, width includes the padding and border. 200px means 200px.

Example

html
<style>
  div { width: 200px; padding: 20px; border: 5px solid #2f9e44; background: #d3f9d8; margin-bottom: 8px; }
  .border-box { box-sizing: border-box; }
</style>

<div>content-box: 250px in total</div>
<div class="border-box">border-box: exactly 200px</div>

Most developers turn it on for every element, at the top of their stylesheet:

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

4. Seeing Boxes in DevTools

Right-click any element and choose Inspect. The Computed (or Layout) panel draws the element's content, padding, border and margin with their sizes. It's the fastest way to find out where unexpected space is coming from.


Common Beginner Mistakes

1. Boxes wider than expected. Usually padding and border are being added to width. Switch to border-box.

2. Padding vs margin. Want the coloured area bigger? Use padding. Want more space between elements? Use margin.

3. Expecting margin to show the background. Margin is always transparent.


Practice Questions

  1. Name the four layers of the box model, from the inside out.
  2. Which layer is filled by the element's background colour: padding or margin?
  3. width: 300px; padding: 10px; border: 2px solid; with the default box-sizing. What's the total width?
  4. The same box with box-sizing: border-box. What's the total width now?
  5. You want two cards further apart. Padding or margin?
  6. Write the rule that makes every element use border-box.

Practice Answers

  1. Content, padding, border, margin.
  2. Padding.
  3. 300 + 10 + 10 + 2 + 2 = 324px.
  4. 300px
  5. Margin.
  6. *, *::before, *::after { box-sizing: border-box; }

Key Takeaways

  • Every element is a box: content → padding → border → margin.
  • Padding is inside the border and shows the background; margin is outside and transparent.
  • By default width is the content only; border-box makes it include padding and border.
  • Use DevTools → Inspect to see the box of any element.