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.
Task: Gather the boxes in the center
See each Flexbox property live on a set of boxes: what values it takes and what changes. Then move on to «Practice».
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; }
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 */
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;
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;
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;
Space in between
gap puts an equal space between the boxes. No fiddling with margin, one line is enough.
gap: 16px;
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;
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; }
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; }
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.
Awesome!
The boxes fell into place.