Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 11 / 16

Flexbox

Change a control, or edit the CSS. The preview uses that same CSS.

flex-direction
flex-wrap
justify-content
align-items
align-content
Item

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.

Flex properties
PropertyApplies toWhat it controls
flex-directionContainerrow, column, or either one reversed
flex-wrapContainerWhether items wrap onto another line
justify-contentContainerFree space on the main axis: start, end, center, or the space-between family
align-itemsContainerAlignment on the cross axis, including stretch and baseline
align-contentContainerHow wrapped lines are spaced. It needs more than one line
gapContainerSpace between items, without margin on each item
flex-growItemHow much of the extra space this item takes
flex-shrinkItemHow much this item gives back when space is tight
flex-basisItemThe 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.

04

Guided challenge

Flexbox

The row style says flex-direction row. The script logs column. Read the direction from the style attribute.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

flex
row
1
B

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which property distributes free space on the main axis?
    02What does flex-grow on one item do?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson