Lesson 1 · Unit 03
Flexbox
One line of items, sized by sharing out space
What you practise
- Main and cross axis,
justify-contentandalign-items,flex-grow,shrinkandbasisworked as arithmetic, wrapping andgap
Try a question from this lesson
Pick one
A <div> is a block: each one normally starts on a new line. With display: flex on their parent, how do these three sit?
<style>
.row { display: flex; }
</style>
<div class="row">
<div>Tea</div>
<div>Milk</div>
<div>Soup</div>
</div>Correct answer
Side by side in one row
Why
A flex container lays its children out along one line, a row by default, each as wide as its content. The rule goes on the parent and changes how the children are arranged. They stop behaving as blocks.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Two axes
A flex container has a main axis, the direction its items run in, and a cross axis at right angles to it.
justify-content spreads the items along the main axis. align-items places them on the cross axis.
flex-direction: column turns the main axis downwards, and the two properties swap directions with it.
.row {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}Concept
Grow, shrink, basis
Each item starts at its flex-basis, or at its width. Space left over is shared out in proportion to flex-grow.
If the items do not fit, each gives up space in proportion to flex-shrink, which is 1 unless you change it.
flex: 1 sets all three: items forget their content size and share the row equally.
.item { flex: 1; }
/* the same as:
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0; */10 more exercises in the app
- 6× type the answer
- 1× fill the blanks
- 2× multiple choice
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.