On this page9 sections
Lecture 005: The Box Model
| Category | 2: The Box Model |
| Lecture | 005 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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-boxto make sizes predictable - Inspect boxes with the browser's developer tools
1. The Four Layers
+--------------------- margin ----------------------+
| +------------------ border -------------------+ |
| | +--------------- padding --------------+ | |
| | | | | |
| | | content (text, images) | | |
| | | | | |
| | +--------------------------------------+ | |
| +---------------------------------------------+ |
+---------------------------------------------------+| Layer | What it is |
|---|---|
| Content | The text or image itself |
| Padding | Space between the content and the border. The background fills it |
| Border | A line around the padding |
| Margin | Space outside the border, pushing other elements away |
Example
<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:
width: 200px
padding: 20px → + 20 left + 20 right
border: 5px → + 5 left + 5 right
---------------------------------------
total width = 250pxExample: both boxes say width: 200px, but they're not the same size:
<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
<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:
*, *::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
- Name the four layers of the box model, from the inside out.
- Which layer is filled by the element's background colour: padding or margin?
width: 300px; padding: 10px; border: 2px solid;with the default box-sizing. What's the total width?- The same box with
box-sizing: border-box. What's the total width now? - You want two cards further apart. Padding or margin?
- Write the rule that makes every element use
border-box.
Practice Answers
- Content, padding, border, margin.
- Padding.
- 300 + 10 + 10 + 2 + 2 = 324px.
- 300px
- Margin.
*, *::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
widthis the content only;border-boxmakes it include padding and border. - Use DevTools → Inspect to see the box of any element.