4 / 9 · Basic
The box model
Give a box width: 200px, 16px of padding and a 2px border, and by default it takes up 236 pixels across the screen, not 200.
Jump to the 11 practice questionsYou will learn
- Name the four layers: content, padding, border, margin
- Work out total width with content-box and border-box
- Expect collapsing margins and inline boxes
Before this: Colour and units
1Explain
Every element is a box
The CSS box model describes every element as a box made of four layers: content, padding, border and margin. To lay out a page, the browser turns every element into a rectangle. Each box has four layers, from the inside out. The content area holds the text or image.
Content, padding, border and margin. With the default content-box sizing, width sets only the content: 200 + 16 + 16 + 2 + 2 = 236 pixels wide.
2Explore
Padding, border and box-sizing
With content-box the box grows when padding and border grow. With border-box it stays 160px and the content shrinks.
Tap a choice: the code and the preview change together.
<style>
.box { width: 160px; padding: 10px; border: 4px solid #1a5fb4; margin: 20px; background: #fff4d6; box-sizing: content-box; }
</style>
<div class="box">Rye loaf</div>Nothing is drawn: there is no visible content.
3Problem
From the inside out
Order the layers from the inside out.
- content
- margin
- padding
- border
Hint
The background shows in the content and the padding.
Solution
Answer:
- content
- padding
- border
- margin
Each layer wraps the one inside it: padding around the content, border around the padding, margin around the border.
4Problem
Four values, clockwise
padding, margin and border-width take one to four values, going clockwise from the top. padding: 16px sets all four sides. padding: 8px 16px sets top and bottom to 8 and left and right to 16. padding: 4px 8px 12px 16px sets top, right, bottom and left in that order. Each side can also be set on its own, as in margin-top. border: 2px solid #333 sets width, style and colour together; without a style such as solid or dashed no border is drawn at all.
padding: 1px 2px 3px 4px: how much is on the left?
px
Hint
Top, right, bottom, left.
Solution
Answer: 4 px
Clockwise from the top: top 1, right 2, bottom 3, left 4.
5Explain
box-sizing
By default, width and height set the size of the content area only, and padding and border are added on top. That is the content-box model, and it makes layouts awkward: two boxes of width: 50% with any padding no longer fit side by side. With box-sizing: border-box, the width includes the padding and border, and the content area shrinks to make room.
| box-sizing | Width on screen | Content width |
|---|---|---|
| content-box | 244px | 200px |
| border-box | 200px | 156px |
6Problem
content-box total width
content-box: width 300px, padding 20px, border 5px. Total width?
px
Hint
Add padding and border on both sides.
Solution
Answer: 350 px
content-box adds padding and border on both sides to the width: 300 + 20 + 20 + 5 + 5 = 350.
7Problem
border-box content width
border-box: width 300px, padding 20px, border 5px. How wide is the content area?
px
Hint
Take padding and border away from 300.
Solution
Answer: 250 px
300 minus 2 times 20 minus 2 times 5 = 250.
8Explain
Margins that collapse
One rule surprises nearly everyone. When the bottom margin of one block meets the top margin of the next, they do not add up; the larger one wins. A paragraph with margin-bottom: 20px followed by a heading with margin-top: 30px ends up 30 pixels away, not 50. This is called margin collapsing, and it only happens to vertical margins between blocks in normal flow, not inside flex or grid containers.
<style>
p { margin: 0; background: #e8eefa; }
.a { margin-bottom: 20px; }
.b { margin-top: 30px; }
</style>
<p class="a">Bottom margin 20px</p>
<p class="b">Top margin 30px: the gap is 30px, not 50px</p>9Problem
20px meets 30px
A margin-bottom of 20px meets a margin-top of 30px. How big is the gap?
px
Hint
The larger margin wins.
Solution
Answer: 30 px
Vertical margins collapse: the larger one, 30, wins.
10Practice
A card exactly 240px wide
Make .card exactly 240px wide in total: box-sizing: border-box, width: 240px, padding: 16px and a 2px solid border. Centre it with margin: 0 auto.
- border-box
- width 240px
- padding 16px
- 2px border
- solid border
<style>
.card {
background: #fff4d6;
}
</style>
<div class="card">
<h2>Rye loaf</h2>
<p>Dark, dense and baked every morning.</p>
</div>Hint
box-sizing: border-box keeps padding and border inside the 240px.
A solution
<style>
.card {
background: #fff4d6;
box-sizing: border-box;
width: 240px;
padding: 16px;
border: 2px solid #333;
margin: 0 auto;
}
</style>
<div class="card">
<h2>Rye loaf</h2>
<p>Dark, dense and baked every morning.</p>
</div>11Practice
Tags with real height
The tags are inline, so vertical padding does not give them real height. Make them inline-block with padding: 4px 8px.
- inline-block
- padding top 4px
- padding left 8px
<style>
.tag {
background: #e8eefa;
border-radius: 4px;
}
</style>
<p>Filed under <span class="tag">bread</span> <span class="tag">rye</span> <span class="tag">recipes</span></p>Hint
display: inline-block keeps them in the line but gives them a real box.
A solution
<style>
.tag {
background: #e8eefa;
border-radius: 4px;
display: inline-block;
padding: 4px 8px;
}
</style>
<p>Filed under <span class="tag">bread</span> <span class="tag">rye</span> <span class="tag">recipes</span></p>12Explain
Common mistakes
<style>
.card { border: 2px #333; padding: 8px; }
</style>
<div class="card">Rye</div><style>
.card { border: 2px solid #333; padding: 8px; }
</style>
<div class="card">Rye</div>A border needs a style. Without solid (or dashed, dotted …) the style is none and nothing is drawn.
13
Summary
- Content, padding, border, margin.
- content-box adds padding and border to width; border-box includes them.
- Shorthand goes clockwise from the top; touching vertical margins collapse.