Lesson 3 · Websites from scratch
Boxes and flexbox
Understand the box model and lay out a navigation bar with flexbox.
About 25 minutes
Tab indents. Press Escape, then Tab, to leave the editor.
Here's the secret that makes CSS layout click: every element is a rectangle. Headings, paragraphs, links, buttons, images, all boxes. Once you can see the boxes, you can control them. In this lesson you'll learn how a box's size is worked out (the box model), then use flexbox to line boxes up into a navigation bar you'd actually put on a site.
The box model
Every box has four layers, from the inside out:
- Content: the text or image itself. Its size is set by
widthandheight, or by what's inside it. - Padding: space inside the box, between the content and the border. It takes the box's background color.
- Border: a line around the padding. Can be any thickness, style and color.
- Margin: space outside the border that pushes other boxes away. Always transparent.
.card {
width: 200px;
padding: 16px;
border: 4px solid #264653;
margin: 12px;
}
So how wide is .card on screen? By default, width only sets the content. Padding and border get added on top: 200 + 16 + 16 + 4 + 4 = 240px of visible box, plus 12px of margin on each side that keeps neighbours away. That surprises everyone the first time.
The fix most developers use is box-sizing: border-box, which makes width include the padding and border. Now width: 200px means the box is 200px, full stop, and the content area shrinks to fit. Plenty of sites start their CSS with this:
*, *::before, *::after {
box-sizing: border-box;
}
Play with the sliders and flip box-sizing to see the arithmetic change.
Lab
The box model
Every element is a box: content, wrapped in padding, then a border, then margin.
box width = 160 + 2×16 + 2×4 = 200px
space taken = 200 + 2×16 = 232px
width sets the content only, so padding and border make the box wider.
.box {
box-sizing: content-box;
width: 160px;
padding: 16px;
border: 4px solid;
margin: 16px;
}Block and inline
Boxes behave differently depending on their display value:
- Block boxes (
div,p,h1,section,li…) start on a new line and stretch to fill the width available. - Inline boxes (
a,span,strong…) sit inside a line of text, side by side, and only take the space their content needs. Width and height don't apply to them.
That's why a list of links in <li>s stacks into a column: each li is a block. To put them in a row with control over spacing and alignment, you want flexbox.
Flexbox: boxes in a row (or column)
Set display: flex on a parent, and its direct children become flex items that line up along the main axis. By default that's a row, left to right.
.toolbar {
display: flex;
justify-content: space-between; /* spacing along the main axis */
align-items: center; /* alignment across it */
gap: 12px; /* space between items */
}
The properties you'll use most, all set on the parent:
| Property | What it does | Common values |
|---|---|---|
flex-direction | Which way the main axis runs | row, column |
justify-content | Spreads items along the main axis | flex-start, center, space-between, space-around |
align-items | Lines items up across the main axis | stretch, center, flex-start, flex-end |
gap | Space between items | 8px, 1rem |
flex-wrap | Lets items move onto a new line when they run out of room | nowrap, wrap |
One thing trips people up: with flex-direction: column, the axes swap. justify-content now works vertically and align-items horizontally. Try every combination here until you can predict what each one does before you click it.
Lab
Flexbox lab
Flexbox lines items up along a main axis. Change the settings and watch the boxes move.
Main axis runs left to right. Items have different text offsets, so baseline lines up their numbers.
.container {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
flex-wrap: nowrap;
gap: 8px;
}Build it: a navigation bar
Time to use it. This header has a logo and four links, but everything is stacked. Turn it into a proper nav bar.
Challenge
Build a flexbox nav bar
In the CSS tab:
- Make the links' list (
nav ul) a flex container so the links sit in a row, with some space between them (trygap). - Make the header (
.site-header) a flex container too, so the logo stays on the left and the links move to the right. Hint:justify-contenton the header. - Bonus: line the logo and links up vertically with
align-items: center.
- Your links' list (or nav) is a flex container
- The links sit in a row
- There's space between the links
- The logo is on the left and the links are pushed to the right
Recap
- Every element is a box: content, padding, border, margin, from the inside out.
- By default
widthsets only the content;box-sizing: border-boxmakes it include padding and border. - Block boxes stack and fill the width; inline boxes flow in a line of text.
display: flexon a parent lines up its children;justify-contentworks along the main axis,align-itemsacross it,gapspaces them.
Finished this lesson?
Mark it complete to track your progress on this device.