Show contents
Chapter 4 of 7 · 20 min
The box model and Flexbox
By the end of this chapter, you'll be able to
- Describe the four layers of the box model: content, padding, border, margin
- Use box-sizing: border-box for predictable sizes
- Align and distribute items on a line with Flexbox
- Make items wrap onto new lines with flex-wrap and gap
Every element is a box
For the browser, each element is a rectangle made of four layers, from the inside out:
- the content (text, image), whose size is given by
widthandheight; - the padding: the inner space, between the content and the border;
- the border;
- the margin: the outer space, which keeps the box away from its neighbours.
.card {
width: 300px;
padding: 16px;
border: 5px solid #2a9fd8;
margin: 12px;
}
By default, width only counts the content. This card therefore takes up 300 + 2 × 16 + 2 × 5 = 342 pixels of width, plus its margins. That's the surprise from the introduction.
The rule box-sizing: border-box changes the calculation: width then includes the padding and the border. The card measures exactly 300 pixels, and the content shares what's left. Almost every site applies it to every element, once and for all:
*, *::before, *::after {
box-sizing: border-box;
}
With border-box, width counts everything up to the border: the box is 300 pixels, the content keeps 258.
Text description of the animation
- With the default value (content-box), width only refers to the content.
- The padding is added on both sides.
- Then the border, on both sides too.
- The box overflows by 42 pixels: in a 300-pixel column, it no longer fits.
- With border-box, width counts everything up to the border: the box is 300 pixels, the content keeps 258.
Flexbox: aligning on a line
display: flex on a container lines up its direct children and provides tools to align them:
.bar {
display: flex;
justify-content: space-between; /* distribution on the main axis (horizontal) */
align-items: center; /* alignment on the cross axis (vertical) */
gap: 12px; /* space between the items */
}
<div class="bar">
<a href="/">Kalanblon</a>
<nav aria-label="Menu"><a href="/courses">Courses</a></nav>
<button type="button">Sign in</button>
</div>
justify-contentdistributes the items on the main axis:flex-start,center,flex-end,space-between(equal spaces between the items);align-itemsaligns them on the cross axis:centercentres vertically, which was a headache for a long time;gapputs a regular space between the items, with no margins to calculate;flex-direction: columnmakes the main axis vertical: the items stack up.
Sharing the space and wrapping
On the children, the flex property says how to share the remaining space. On the container, flex-wrap: wrap allows wrapping onto a new line when there's no room left:
.cards {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.cards > article {
flex: 1 1 220px; /* grow: yes; shrink: yes; base size: 220px */
}
Each card aims for 220 pixels and grows to fill the line. On a 360-pixel phone, there's one card per line; on a wide screen, as many as fit. All this without a single special rule for mobile.
Your turn
Align this navigation barHTML · CSS
Preview
The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: the product card
A product card contains an image, a title, a price and an "Add" button. Write the CSS so that: the card is 280 pixels wide including the border, with 16 pixels of padding and a 1-pixel border; its items are stacked vertically 8 pixels apart; and the button always sticks to the bottom of the card, even if the title is short.
Hint 1
display: flex; flex-direction: column; gap: 8px; on the card.Hint 2
margin-top: auto on the button absorbs all the free space above it and pushes it down. Give the card a minimum height too.Show the solution and common mistakes
.product {
box-sizing: border-box;
width: 280px;
min-height: 360px;
padding: 16px;
border: 1px solid #c9d6e3;
display: flex;
flex-direction: column;
gap: 8px;
}
.product img {
width: 100%;
height: auto;
}
.product button {
margin-top: auto;
}
<article class="product">
<img src="wax.jpg" alt="Wax fabric with orange and blue patterns" width="248" height="160">
<h2>Wax fabric</h2>
<p>12,000 CFA francs</p>
<button type="button">Add</button>
</article>
Common mistakes:
- Forgetting
box-sizing: border-box: the card measures 314 pixels (280 + 32 + 2) and no longer fits in its column. - Pushing the button down with a big fixed margin (
margin-top: 120px): it breaks as soon as the title takes two lines.
Summary
- 1A box is the content, the padding, the border and the margin.
- 2box-sizing: border-box makes width include the padding and the border.
- 3display: flex aligns the direct children: justify-content on the main axis, align-items on the other.
- 4gap spaces the items out, flex-wrap: wrap makes them wrap onto new lines.
- 5flex: 1 1 220px shares the remaining space between items of at least 220 pixels.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.