What is Flexbox?
display: flex to a container element and its direct children automatically become flex items. It is ideal for navigation bars, button groups, aligned cards, and any UI that needs items distributed along one axis.The Two Axes
.container {
display: flex; /* turns on flexbox */
}
/* All direct children are now flex items */Flex Direction
live: flex-direction: row;
rowLeft → right (default)
row-reverseRight → left
columnTop → bottom
column-reverseBottom → top
.container {
display: flex;
flex-direction: row; /* row | row-reverse | column | column-reverse */
}Justify Content
live: justify-content: flex-start;
flex-startPacked to start (default)
flex-endPacked to end
centerCentered
space-betweenMax gap, no outer space
space-aroundEqual space each side
space-evenlyIdentical space everywhere
.container {
display: flex;
justify-content: flex-start;
/* flex-start | flex-end | center
space-between | space-around | space-evenly */
}Align Items
live: align-items: stretch;
stretchFill cross axis (default)
flex-startAligned to start
flex-endAligned to end
centerCentered
baselineText baseline aligned
.container {
display: flex;
align-items: stretch; /* stretch | flex-start | flex-end | center | baseline */
}Flex Wrap
nowrapAll on one line — items overflow (default)
wrapItems wrap to new lines
wrap-reverseItems wrap upward
.container {
display: flex;
flex-wrap: wrap; /* nowrap | wrap | wrap-reverse */
}Align Content
align-content distributes those rows on the cross axis — just like justify-content handles items on the main axis. Has no effect with a single row (flex-wrap: wrap must be set).flex-startRows packed to start
flex-endRows packed to end
centerRows centered
space-betweenMax space between rows
space-aroundEqual space each side of rows
stretchRows stretch to fill (default)
.container {
display: flex;
flex-wrap: wrap; /* required */
align-content: flex-start;
/* flex-start | flex-end | center | space-between | space-around | stretch */
}Gap
gap: 0No space
gap: 8px8px between each item
gap: 20px20px between each item
.container {
display: flex;
gap: 16px; /* row-gap + column-gap in one */
/* row-gap: 8px; only between wrapped rows */
/* column-gap: 16px; only between columns */
}Flex Grow
all flex-grow: 0Items keep natural size; space is left over
all flex-grow: 1Items share remaining space equally
item 2: flex-grow: 2Item 2 takes twice the free space
.item { flex-grow: 0; } /* default — does not grow */
.item-fill { flex-grow: 1; } /* absorbs all free space */
.item-priority { flex-grow: 2; } /* absorbs 2× more than flex-grow:1 siblings */Flex Shrink
all flex-shrink: 1 (default)All items shrink proportionally to fit
item 2: flex-shrink: 0Item 2 holds its size; siblings absorb the squeeze
.item { flex-shrink: 1; } /* default — can shrink */
.no-shrink { flex-shrink: 0; } /* never shrinks below its basis */The flex Shorthand
flex-grow, flex-shrink, and flex-basis into one property. The most common value is flex: 1 which expands to 1 1 0% — items start at zero width and grow equally.flex: 1Equal share — expands to 1 1 0%
flex: 0 0 autoNo grow, no shrink — natural content size
2 on item 2Item 2 is twice as wide as its siblings
/* flex: <grow> <shrink> <basis> */
.equal { flex: 1; } /* → 1 1 0% — equal share of space */
.fixed { flex: 0 0 200px; } /* locked at exactly 200px */
.double { flex: 2; } /* takes 2× the space of flex:1 siblings */
.natural { flex: none; } /* → 0 0 auto — keeps content size */Align Self
flex-startPinned to start
flex-endPinned to end
centerCentered on cross axis
stretchStretch to fill height
.container { display: flex; align-items: center; }
.special { align-self: flex-end; } /* overrides for this item only */Order
default (order: 0)Visual order matches source order: A B C
order: -1 on CC appears first — lower value wins
.item-C { order: -1; } /* appears before all order: 0 items */
.item-A { order: 0; } /* default */
.item-B { order: 1; } /* appears last */
/* ⚠ Screen readers follow DOM order, not visual order */Your certificate is waiting
Complete all 6 challenge levels to unlock your personalized certificate
Progress is saved locally in your browser.