Bitpath

Lesson 1 · Unit 03

Flexbox

One line of items, sized by sharing out space

13 exercisesabout 3 min

What you practise

  • Main and cross axis, justify-content and align-items, flex-grow, shrink and basis worked as arithmetic, wrapping and gap

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?

HTML
<style>
  .row { display: flex; }
</style>
<div class="row">
  <div>Tea</div>
  <div>Milk</div>
  <div>Soup</div>
</div>

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.

CSS
.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.

CSS
.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.

One email when Bitpath is ready to try. Nothing else. Privacy