Every element on a page is drawn as a rectangle, and that rectangle has four layers: the content, the padding around it, the border, and the margin outside. Together they make up the CSS box model. Understanding it explains most spacing and sizing puzzles, such as why a 200px box ends up 250px wide.
In this lesson you will learn
- The four layers of the box model
- How width is calculated, and how
box-sizingchanges it - Shorthand for margin and padding
- Centring a block with
margin: auto - The
displayvalues block, inline and inline-block
Content, Padding, Border and Margin
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Box model</title>
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid #00695c;
margin: 15px;
background: #e0f2f1;
}
.border-box { box-sizing: border-box; }
</style>
</head>
<body>
<div class="box">content-box: 200 + 40 + 10 = 250px wide</div>
<div class="box border-box">border-box: exactly 200px wide</div>
</body>
</html>
Result
- Content: the text or image itself, sized by
widthandheight. - Padding: space inside the border. The background colour fills it.
- Border: the line around the padding.
- Margin: transparent space outside the border that pushes other elements away.
- By default (
box-sizing: content-box),widthsets only the content, so the first box is 200 + 20 + 20 + 5 + 5 = 250px wide. - With
box-sizing: border-box, padding and border fit inside the width, so the second box is exactly 200px. Many developers apply it to everything:*, *::before, *::after { box-sizing: border-box; }.
Shorthand, Centring and Display
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Margins</title>
<style>
.card {
max-width: 260px;
margin: 10px auto; /* top/bottom 10px, centred horizontally */
padding: 8px 16px; /* top/bottom 8px, left/right 16px */
border: 2px dashed #6a1b9a;
border-radius: 10px;
}
.inline { display: inline-block; width: 90px; margin: 4px; padding: 4px; background: #fff3cd; }
</style>
</head>
<body>
<div class="card">A centred card using margin: auto</div>
<span class="inline">inline-block</span><span class="inline">inline-block</span>
</body>
</html>
Result
| Shorthand | Meaning |
|---|---|
margin: 10px; | All four sides |
margin: 10px 20px; | Top/bottom 10px, left/right 20px |
margin: 5px 10px 15px; | Top 5, left/right 10, bottom 15 |
margin: 1px 2px 3px 4px; | Top, right, bottom, left (clockwise) |
margin: 10px autowith amax-widthcentres a block horizontally.border-radiusrounds the corners.display: inline-blocklets elements sit side by side like text while still accepting width, height and vertical padding. Plain inline elements ignorewidthandheight.
Margin Collapsing
When two block elements are stacked, their vertical margins do not add up: the larger one wins. A box with margin-bottom: 20px above one with margin-top: 30px gives a 30px gap, not 50px. Horizontal margins, and margins inside flex or grid containers, do not collapse.
Common Mistakes
- Forgetting that padding and border add to the width under the default
content-box. - Using margin when you meant padding: backgrounds and click areas cover padding but not margin.
- Trying to centre with
margin: autowithout a width. A full-width block has nothing to centre. - Setting a fixed
heighton text boxes, which makes text overflow on small screens.
Practice Exercises
Exercise 1. A box has width: 300px; padding: 10px; border: 2px solid; and the default box-sizing. How wide is it on screen?
Show answer
300 + 10 + 10 + 2 + 2 = 324px.
Exercise 2. Centre a 600px-wide content area with 16px padding on each side.
Show answer
.container {
max-width: 600px;
margin: 0 auto;
padding: 0 16px;
box-sizing: border-box;
}
Frequently Asked Questions
How can I see an element’s box model?
Right-click it and choose Inspect. Browser developer tools draw a box-model diagram with the exact content, padding, border and margin sizes.
What is the difference between outline and border?
An outline is drawn outside the border and takes up no space, so it never shifts the layout. It is used for focus indicators.
Summary
- Every element is a box: content, padding, border, margin.
- Default width excludes padding and border;
border-boxincludes them. - Shorthand goes top, right, bottom, left.
margin: 0 autoplus a width centres a block; vertical margins collapse.
Next: CSS Colours, Units and Fonts.
