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:

  1. Content: the text or image itself. Its size is set by width and height, or by what's inside it.
  2. Padding: space inside the box, between the content and the border. It takes the box's background color.
  3. Border: a line around the padding. Can be any thickness, style and color.
  4. 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-sizing
margin 16
border 4
padding 16
160px

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.

CSS
.box {
  box-sizing: content-box;
  width: 160px;
  padding: 16px;
  border: 4px solid;
  margin: 16px;
}
Question

Quick check

A box has width: 100px; padding: 10px; border: 5px solid; margin: 20px; and the default box-sizing: content-box. How wide is it from the left edge of its border to the right edge?
Pick an answer.

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:

PropertyWhat it doesCommon values
flex-directionWhich way the main axis runsrow, column
justify-contentSpreads items along the main axisflex-start, center, space-between, space-around
align-itemsLines items up across the main axisstretch, center, flex-start, flex-end
gapSpace between items8px, 1rem
flex-wrapLets items move onto a new line when they run out of roomnowrap, 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.

flex-direction
justify-content (main axis)
align-items (cross axis)
flex-wrap

Main axis runs left to right. Items have different text offsets, so baseline lines up their numbers.

CSS
.container {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: stretch;
  flex-wrap: nowrap;
  gap: 8px;
}
Question

Quick check

You want one button dead center inside a 300px-tall box. Which CSS on the box does it?
Pick an answer.

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:

  1. Make the links' list (nav ul) a flex container so the links sit in a row, with some space between them (try gap).
  2. 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-content on the header.
  3. 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 width sets only the content; box-sizing: border-box makes it include padding and border.
  • Block boxes stack and fill the width; inline boxes flow in a line of text.
  • display: flex on a parent lines up its children; justify-content works along the main axis, align-items across it, gap spaces them.

Finished this lesson?

Mark it complete to track your progress on this device.