Lessons + 20 levels

Arrange the boxes with Flexbox

Write display: flex, justify-content and align-items on the container to put the boxes in place. What you type shows up live instantly.

Start
Live20 levelsAnimated
LIVE DEMO

Task: Gather the boxes in the center

123
justify-content: flex-start;
Correct! Just like that.

See each Flexbox property live on a set of boxes: what values it takes and what changes. Then move on to «Practice».

1
2
3
display: flex
display: flex

It all starts here

When you give the container display: flex, the boxes (flex items) line up side by side in a single row. By default they were stacked on top of each other.

.maydon {
  display: flex;
}
1
2
3
main axis →cross
main / cross axis

Understand the two axes

Flexbox works with two axes: the main axis, i.e. the direction the boxes line up along (usually horizontal), and the cross axis, perpendicular to it. justify-content positions along the main axis, align-items along the cross axis.

/* main axis ->  justify-content */
/* cross axis v  align-items */
1
2
3
justify-content

Along the main axis

justify-content positions the boxes along the main axis (horizontal): flex-start (to the left), center (to the center), flex-end (to the right), space-between / space-around / space-evenly (space in between).

justify-content: center;
justify-content: space-between;
1
2
3
align-items

Along the cross axis

align-items positions the boxes along the cross axis (vertical): flex-start (to the top), center (to the middle), flex-end (to the bottom), stretch (stretches to the full height).

align-items: center;
align-items: flex-end;
1
2
3
flex-direction

Change the direction

flex-direction sets the direction of the main axis: row (a row, the default), column (a column), and their -reverse reversed variants. Important: in column the main axis becomes vertical!

flex-direction: column;
flex-direction: row-reverse;
1
2
3
gap

Space in between

gap puts an equal space between the boxes. No fiddling with margin, one line is enough.

gap: 16px;
1
2
3
4
5
6
flex-wrap

If it doesn't fit, next line

By default the boxes are squeezed into one row. When you give flex-wrap: wrap, whatever doesn't fit moves to the next line. That is how a responsive grid is built.

flex-wrap: wrap;
1
2
3
flex-grow

Take up the free space

flex-grow is a property of the box (the item). If its value is 1, it expands to fill the free space. The others stay at their own size.

.quti2 {
  flex-grow: 1;
}
1
2
3
order

Change the order

order is a property of the box. Without changing the HTML, it controls its visual order. A smaller value comes first, a larger value comes last (default 0).

.quti1 {
  order: 2;
}
1
2
3
align-self

An exception for one box

align-self is a property of the box. It overrides the container's align-items rule for that box only. Use it when you want to place just one of them differently.

.quti2 {
  align-self: flex-end;
}

Ready? 🎯

Now put your knowledge to the test: across 20 practice levels you write code to put the boxes in their homes.

Field display: flex

Awesome!

The boxes fell into place.

1Box (flex item) Home (to its own box)
Task
.maydon CSSLive
Explanation
Need help?