CSS · Unit 3: Layout with flexbox and grid · Lesson 1 of 6
Flexbox: rows and gap
display: flex on a container lines its children up in a row.
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
display: flex on a container lines its children up in a row. The children are called flex items.
gap puts space between the items without adding margins to each one.
flex-direction: column stacks them instead. Flexbox lays things out in one direction: a row or a column.
Worked example
CSS
.row {
display: flex;
gap: 12px;
}
.box {
padding: 12px;
background-color: #e0f2f1;
}
The HTML it styles
<div class="row">
<div class="box">One</div>
<div class="box">Two</div>
<div class="box">Three</div>
</div>
Key terms
- Flexbox
- display: flex lines children up in a row or column; gap spaces them.
Practise it in Code Academy
It has 4 exercises (multiple choice, fill in the blank, fix the broken code 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.