Flexbox
Change a control, or edit the CSS. The preview uses that same CSS.
Numeric changes ease so you can watch them.
Direction flipped the row into a column, justify-content moved the spare space along that axis, and the item you selected grew while its siblings kept their own flex-grow. The container was one flex parent the whole time.
Three mechanisms you already have can place this note. Switch them and watch which ones match.
.parent {
display: flex;
justify-content: center;
align-items: center;
}The parent centers the note on both axes.
+Go deeper
The main axis
display flex, from the display lesson, makes the children flex items. then sets the axis. flex-direction row is horizontal. column is vertical. row-reverse and column-reverse flip the start and the end. The of each item still includes its padding and border.
Free space on the main axis
justify-content distributes leftover space on the main axis: flex-start, flex-end, center, space-between, space-around, or space-evenly. Gap is a fixed space between items and is not the same as space-between, which only appears when there is free space.
The cross axis
align-items aligns each item on the cross axis. stretch is the initial value, and it needs the item's cross size to be auto. align-content does the same job for extra space when there is more than one line. With nowrap and a single line, align-content has no second line to move.
| Property | Applies to | What it controls |
|---|---|---|
| flex-direction | Container | row, column, or either one reversed |
| flex-wrap | Container | Whether items wrap onto another line |
| justify-content | Container | Free space on the main axis: start, end, center, or the space-between family |
| align-items | Container | Alignment on the cross axis, including stretch and baseline |
| align-content | Container | How wrapped lines are spaced. It needs more than one line |
| gap | Container | Space between items, without margin on each item |
| flex-grow | Item | How much of the extra space this item takes |
| flex-shrink | Item | How much this item gives back when space is tight |
| flex-basis | Item | The starting size before grow and shrink |
One item can differ
flex-grow takes a share of extra space. flex-shrink gives space back when the items overflow. flex-basis is the starting size along the main axis. Select A, B, or C and change only that item. The others keep their own values, which is the point of a per-item control. The switch above the prediction centers one note three ways you can already explain. Flexbox uses justify-content and align-items on the parent. Margin auto centers horizontally, and only if the note has a width. The position lesson is the third way: a relative parent, an absolute note, and a transform that pulls it back by half its own size. None of the three is the only correct one. Flexbox is the usual choice when the note should stay in the layout.
A second line
flex-wrap wrap allows another line when the items no longer fit the width of the container. nowrap keeps one line and overflows. The playground container is 320px wide so wrap is visible when the bases are wide.
Guided challenge
Flexbox
The row style says flex-direction row. The script logs column. Read the direction from the style attribute.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
flex row 1 B
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.