CSS Flexbox layout - flex containers and items, direction, wrapping, alignment, sizing with flex-grow/shrink/basis, gap, and common patterns.
Flexbox
Flexbox is a one-dimensional layout system. It arranges items in a single row or column and gives you precise control over alignment, spacing, and sizing. Before Flexbox, tasks like centring an element or creating equal-height columns required awkward workarounds. Flexbox makes these trivial.
Flex container and flex items
Flexbox has two roles:
- Flex container - the parent element with
display: flex - Flex items - the direct children of the container
The moment you set display: flex, the three child divs stop stacking vertically and line up side by side in a row.
That is the default behaviour.
Note: Only direct children become flex items. Grandchildren and deeper descendants are not affected by the flex container.
Interactive demo
Change direction, alignment, wrapping, and growth -- the items update immediately:
flex-direction
Controls the main axis - the direction items flow:
The main axis is the direction items flow. The cross axis is perpendicular to it. For flex-direction: row,
the main axis is horizontal and the cross axis is vertical. For flex-direction: column, they swap.
flex-wrap
By default, flex items try to fit on a single line, shrinking if necessary. flex-wrap lets items wrap to new
lines:
Cards that do not fit on one line wrap to the next row.
flex-flow shorthand
Combines flex-direction and flex-wrap:
Alignment
Flexbox has four alignment properties. Two control the main axis, two control the cross axis.
justify-content (main axis)
Distributes space along the main axis:
align-items (cross axis)
Aligns items along the cross axis:
align-self (cross axis, per item)
Overrides align-items for a single flex item:
align-content (multi-line cross axis)
Controls spacing between wrapped lines. Only applies when flex-wrap: wrap is set and there are multiple lines:
Same values as justify-content: flex-start, flex-end, center, space-between, space-around, space-evenly,
stretch.
The gap property
gap adds space between flex items without adding space at the edges:
You can set row and column gaps separately:
Tip: Before
gapexisted for flexbox, developers used margins on items and negative margins on containers. That hack is no longer necessary.gapis supported in all modern browsers.
Flex item sizing
Three properties control how flex items grow and shrink.
flex-grow
Determines how much an item grows to fill available space. The default is 0 (do not grow).
If all items have flex-grow: 1, they share extra space equally. If one item has flex-grow: 2, it gets twice as much
extra space as items with flex-grow: 1.
flex-shrink
Determines how much an item shrinks when there is not enough space. The default is 1 (shrink proportionally).
Setting flex-shrink: 0 prevents an item from shrinking below its natural size. This is useful for fixed-width
sidebars.
flex-basis
Sets the initial size of an item before growing or shrinking. It replaces width (for rows) or height (for
columns) in a flex context:
The flex shorthand
Combines all three into one declaration:
Common shorthands:
Tip: Use the
flexshorthand instead of settingflex-grow,flex-shrink, andflex-basisseparately. It is more readable and sets sensible defaults.
order
By default, flex items appear in their HTML source order. The order property overrides this:
Lower values appear first. The default is 0. Use order sparingly - it can make the visual order inconsistent with
the DOM order, which hurts accessibility.
Common Flexbox patterns
Centring anything
The most famous use case. Centre an element both horizontally and vertically:
Navigation bar
Card row
flex: 1 1 280px means: start at 280px, grow to fill space, wrap when there is not enough room. This creates a
responsive card layout without media queries.
Sidebar + main content
The sidebar stays at 260px. The main content takes up all remaining space.
Footer pushed to the bottom
A common problem: make the footer stick to the bottom of the page even when content is short.
flex: 1 on main makes it grow to fill all available vertical space, pushing the footer down.
Flexbox vs older techniques
What you learned
- Set
display: flexon a container to create a flex context flex-directioncontrols the main axis (row or column)justify-contentaligns items along the main axis;align-itemsalong the cross axisgapadds space between items without edge spacingflex-grow,flex-shrink, andflex-basis(or theflexshorthand) control item sizing- Common patterns: centring, nav bars, card grids, sidebars, and sticky footers
Next step
Flexbox handles one-dimensional layouts. The next chapter introduces CSS Grid - a two-dimensional layout system for rows and columns simultaneously.