{module 4}
The box model: padding, border and margin
Making it look right with CSS20 min read
What you will be able to do
- Name the four layers of a box, and set the padding, border and margin of an element.
- Work out how wide a box really is, and use
box-sizingso that it is as wide as you said. - Centre a column of text in a wide window, and read the layers of a box in the developer tools.
Why it matters
Tunis Tile Workshop paints tiles by hand in the medina of Tunis, Tunisia. Leila Haddad, who runs it, put her tile patterns on a page, each in a box with a blue border. The text touched the border on every side. The boxes touched each other, so two patterns looked like one. On her large screen, each line of text ran across the whole window, and her customers lost their place from one line to the next.
None of this was about colour or fonts. It was about space: inside a box, between boxes, and around the page. CSS treats every element as a box with layers of space, and once you can name the layers, you can control each one.
The idea
Every element is a box
The browser draws every element as a rectangle, even a heading or a paragraph. The box has four layers, from the inside out:
- The content: the text or picture itself.
- The padding: space between the content and the border. It has the element's background colour.
- The border: a line around the padding.
- The margin: space outside the border. It keeps other elements away, and it is always transparent.
Think of a framed picture on a wall. The picture is the content, the white card around it is the padding, the frame is the border, and the empty wall around the frame is the margin.
Setting the layers
.card {
box-sizing: border-box;
max-width: 20rem;
padding: 1rem;
border: 2px solid #1d4e89;
margin-bottom: 1.5rem;
}padding: 1rem puts 16 pixels of space inside the box, on all four sides.
border takes three values: a width, a style and a colour. The style solid is a plain line. Others are dashed and dotted. A border is a fine line, so its width is in pixels, not rem.
margin-bottom: 1.5rem puts 24 pixels below the card, so the next card starts lower. Each side has its own property: margin-top, margin-right, margin-bottom and margin-left. Padding has the same four.
One value for padding or margin sets all four sides. Two values set the top and bottom first, then the left and right. So padding: 0 1rem means no space above and below, and 1rem on the left and right.
How wide is a box?
max-width sets the largest width the box may have. It may be narrower on a small screen, but never wider.
By default, that width is the width of the content only. The padding and the border are added outside it. A box with a width of 320 pixels, padding of 16 and a border of 2 is really this wide:
320 + 16 + 16 + 2 + 2 = 356 pixels
Two of each, because there is a left side and a right side. This surprises many people, and it makes boxes hard to fit side by side.
The property box-sizing changes the sum. With box-sizing: border-box, the width you give includes the padding and the border. The box is 320 pixels from the outside of one border to the outside of the other. The browser makes the content narrower to fit: 320 minus 36 is 284 pixels. Lesson 4 puts boxes side by side, and this makes their widths easy to plan.
A centred column
Long lines are hard to read. To keep the text in a column of comfortable width, give the body a max-width and set its left and right margins to auto:
max-width: 40rem;
margin: 0 auto;
padding: 0 1rem;margin: 0 auto means no margin at the top and bottom, and auto at the left and right. The value auto tells the browser to share the free space equally between the two sides, so the column sits in the middle. The padding keeps the text away from the edges of a small screen, where there is no free space at all.
Worked example
Make a folder called tunis-tiles with this index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tunis Tile Workshop</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Tunis Tile Workshop</h1>
<p>Painted tiles made by hand in the medina of Tunis. Each tile is 10 cm square.</p>
<section class="card">
<h2>Jasmine pattern</h2>
<p>Blue and white, painted with a brush. $3 a tile.</p>
</section>
<section class="card">
<h2>Olive branch pattern</h2>
<p>Green and yellow on white. $3.50 a tile.</p>
</section>
<p>Orders of more than 50 tiles take two weeks.</p>
</body>
</html>And this styles.css:
/* Text #222222 on white is 15.91 to 1, and blue #1d4e89 on white is 8.39 to 1. */
body {
max-width: 40rem;
margin: 0 auto;
padding: 0 1rem;
background-color: #ffffff;
color: #222222;
font-family: Verdana, Arial, sans-serif;
line-height: 1.5;
}
h2 {
margin-top: 0;
margin-bottom: 0.5rem;
color: #1d4e89;
font-size: 1.25rem;
}
.card {
box-sizing: border-box;
max-width: 20rem;
padding: 1rem;
border: 2px solid #1d4e89;
margin-bottom: 1.5rem;
}Save both and open the page in a wide window. The text sits in a column in the middle. Each pattern is in a blue box, with space between the text and the border and space between the two boxes. A heading has a margin above it by default. The h2 rule removes it, so the heading starts at the top of its card.
Read the layers in the developer tools
Right-click a card's border and choose Inspect, then select the section in the Elements panel. In Chrome and Edge, scroll to the bottom of the Styles tab. There is a drawing of the box: margin outside, then border, then padding, then the content in the middle, each with its sizes. Hover over a layer in the drawing, and the browser shows that layer on the page. In Firefox, the same drawing is in the Layout view.
In a window 1280 pixels wide, the Computed tab gives these values for the card:
width: 320px
padding-left: 16px
border-left-width: 2px
margin-bottom: 24pxThe width is 320 pixels, padding and border included, because of border-box. The paragraph inside the card shows how much room is left for the content:
284pxNow select the body:
width: 640px
padding-left: 16px
margin-left: 304px
margin-right: 304px40rem is 640 pixels. With 16 pixels of padding on each side, the body takes 672 of the 1280 pixels. The browser shared the other 608 equally, so each margin is 304.
On a phone, the column cannot be 640 pixels wide. In the device toolbar from module 1, at a width of 390 pixels:
body width: 358px
card width: 320pxThe body is 390 minus 32 pixels of padding. There is no free space, so the auto margins are zero. That is what max-width is for: the column is never too wide, and it is narrower when the screen is.
Common mistakes
Forgetting box-sizing
.card {
max-width: 20rem;
padding: 1rem;
border: 2px solid #1d4e89;
margin-bottom: 1.5rem;
}This is the width of the paragraph inside the card:
320pxThe content is 320 pixels, so the box from border to border is 356, not the 320 you wrote. Nothing looks broken with one card. When boxes have to fit side by side, the 36 extra pixels push one to the next line. Add box-sizing: border-box to every box that has a width and padding.
Auto margins with nothing to share
body {
margin: 0 auto;
padding: 0 1rem;width: 1248px
margin-left: 0pxWithout a max-width, the body fills the window, so there is no free space to share. The lines run across the whole screen, as on Leila's old page. A box can be centred only when it is narrower than the space around it.
A border without a style
border: 2px #1d4e89;border-top-style: none
border-top-width: 0pxThere is no border at all. The browser starts every border with the style none, and a border with no style is not drawn, whatever its width and colour. Give all three values: 2px solid #1d4e89.
Think it through
Leila wants the note about large orders in a pale blue box, exactly as wide as the cards, with more space inside than the cards have. Tomás, a learner, plans it.
Understand. The note must be 320 pixels wide from border to border, like the cards. Its padding is larger, 1.5rem. It needs a background colour and a thin border.
Break down. A class for the note. Its width. Its padding and border. Its colours, with the contrast measured.
Plan. He adds class="notice" to the paragraph. With border-box, he can write max-width: 20rem again, and the larger padding comes out of the content, not the outside. For the background he picks #eef3fa. Measured against it, the text #222222 is 14.27 to 1.
Build. He writes a .notice rule with box-sizing: border-box, max-width: 20rem, padding: 1.5rem, border: 1px solid #1d4e89 and background-color: #eef3fa, then saves and reloads.
Test. The Computed tab gives a width of 320 pixels, the same as the cards. The padding is 24 pixels. For the odd case he removes box-sizing. The note grows to 370 pixels, 320 plus 48 of padding plus 2 of border, and no longer lines up. He puts the line back.
Reflect. He worked out the width before he looked. When the number matched, he knew why, not only that it looked right.
At work
An estimate that counts only the CSS is too low. Much of a job is not typing code: asking the client for details, waiting for answers, checking on a phone and on a laptop, measuring colours, publishing, and writing to say it is done. Those parts take real time, and if you leave them out, the job takes longer than you said.
Here is the estimate Tomás sent Leila:
Leila, here is how I expect the 3 hours for the new page layout to go.
- Writing the CSS for the boxes and the column: 1 hour 30 minutes
- Checking on a phone and a laptop, and fixing what I find: 45 minutes
- Publishing it and sending you the link: 15 minutes
- Your review and one round of changes: 30 minutes
The last line depends on your reply. If you can look at it on the same day, the whole job takes one day.
Half of the time is not CSS. Writing that down protects the estimate, and shows the client what the time buys.
Your tasks
Task 1: Build and read the box
Build the page and open it in a wide window. Inspect a card, find the drawing of the box in the developer tools, and hover over each layer.
You are done when you have seen each of the four layers shown on the page, and the content area reads 284 pixels wide.
Task 2: Do the sum, then check it
Change the card's padding to 1.5rem and its border to 4px. Before you reload, work out the width of the content with border-box, and the width from border to border without it. Then check both numbers in the developer tools.
You are done when both of your sums match what the browser shows.
Task 3: Centre your own page
Open the Kigali Threads page you styled in lesson 1. Put its text in a centred column, choose a max-width you think is easy to read, and put one part of the page in a box with padding and a border.
You are done when your page is centred in a wide window, the text does not touch the edges in a narrow one, and your box has space inside its border.
Recap
- Every element is a box: content, padding, border and margin, from the inside out.
- Without
box-sizing: border-box, padding and border are added outside the width. With it, they are inside. - A
borderneeds a width, a style and a colour. Without a style it is not drawn. max-widthwithmargin: 0 autocentres a column. Add side padding for small screens.- The drawing of the box in the developer tools shows every layer and its size.
Go deeper
- MDN: The box model explains the four layers and
box-sizing, with examples you can change. - MDN: box-sizing gives every value of the property and how the width is worked out.
- MDN: margin shows what one, two, three and four values mean.
- Chrome for Developers: CSS features reference explains where to find the drawing of the box and the Computed tab.