CSS · Unit 3: Layout with flexbox and grid · Lesson 2 of 6
Flexbox alignment
justify-content spreads items along the row: flex-start, center, flex-end, space-between (first and last item pushed to the edges) or space-around.
Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.
The idea
justify-content spreads items along the row: flex-start, center, flex-end, space-between (first and last item pushed to the edges) or space-around.
align-items lines items up across the row. center keeps them vertically centered, the classic way to line up an icon with its text.
To center something both ways, give its container display: flex, justify-content: center and align-items: center.
Worked example
CSS
.bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
background-color: #f3f4f6;
}
The HTML it styles
<header class="bar">
<strong>Logo</strong>
<a href="https://example.com/login">Log in</a>
</header>
Key terms
- justify-content
- Spreads flex items along the row: center, space-between and more.
- align-items
- Lines flex items up across the row, such as centering them vertically.
Practise it in Code Academy
It has 4 exercises (multiple choice, fill in the blank and write code from a prompt) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.
Every lesson is free: no ads, no lives and no paywall.