Flexbox Basics: Flex Containers, Axes, flex-direction and flex-wrap
What display: flex does to a container and its children, the main and cross axes and how writing modes change them, flex-direction, flex-wrap and flex-flow, what counts as a flex item, and when to choose flexbox over grid.
kvi5hnuCode Sprout team member8 min readOn this page
Flexbox lays out items in one direction, a row or a column, and distributes space between them. It's the tool for toolbars, navigation bars, button groups, cards whose footer sits at the bottom, centered content and form rows: anything that's a line of things. Grid (Part 10) handles two directions at once.
This article covers the container, the axes and wrapping. Article 62 covers how items grow and shrink, and article 63 covers alignment.
Flex containers and flex items
.toolbar { display: flex; } /* a block-level flex container */
.badge { display: inline-flex; } /* an inline-level one, sitting in a line of text */
display: flex makes the element a flex container, and its children flex items:
- Each in-flow child becomes a flex item, including text that isn't wrapped in an element, which becomes an anonymous item, and
::beforeand::after. - Items are blockified: a
<span>or<a>that's a flex item getsdisplay: block, andinline-blockbecomesblock. Tested in Chrome: a span child computes toblock, and so does the container's::before. - Only children are items. Grandchildren lay out normally inside their item, unless that item is a flex container too.
floatandcleardon't apply to flex items, andvertical-alignhas no effect on them.- Absolutely positioned children aren't flex items: they're taken out of flow, and their static position is where they'd go as the container's only item: the start corner, unless the container's alignment moves it.
- Margins between flex items never collapse, and the container's margins don't collapse with its items' margins.
The container is like a block to the outside: display: flex is the two-keyword display: block flex, and inline-flex is inline flex (article 46).
The two axes
Flexbox doesn't think in terms of left, right, top and bottom. It uses two axes:
- The main axis is the direction items are placed in, one after another. Its size is the main size, and it runs from main-start to main-end.
- The cross axis is perpendicular to it, running from cross-start to cross-end, with the cross size.

With the default flex-direction: row in English, the main axis is horizontal, left to right, and the cross axis is vertical, top to bottom. Every flex property is defined in terms of these axes, so the same property does different things in a row and in a column: justify-content works along the main axis, align-items along the cross axis.
flex-direction
| Value | Main axis |
|---|---|
row (initial) | the inline direction: left to right in English, right to left in Arabic or Hebrew |
row-reverse | the inline direction, reversed |
column | the block direction: top to bottom in horizontal writing |
column-reverse | the block direction, reversed |
row follows the writing mode (article 47): in an dir="rtl" page, a flex row starts at the right; in writing-mode: vertical-rl, a row runs top to bottom. That's why the axis names are logical.
The reversed directions swap main-start and main-end, so items are placed from the other end, and justify-content: flex-start packs them at that end too. They only change the visual order: tab order and screen readers still follow the HTML. Article 64 explains why that matters and when reversing is safe.

row-reverse and the right-to-left row look the same here, for different reasons: one reverses the main axis, the other uses a main axis that already runs right to left.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.controls { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.75rem; }
select { font: inherit; }
.box {
display: flex;
flex-direction: var(--dir, row);
flex-wrap: var(--wrap, nowrap);
gap: 0.4rem;
padding: 0.4rem;
block-size: 12rem;
border: 2px dashed #8a3b12; border-radius: 8px;
}
.box div {
inline-size: 4.5rem; block-size: 2.5rem;
display: grid; place-items: center;
background: #f4efe6; border: 1px solid #8a3b12; border-radius: 6px;
}
.box div:first-child { background: #8a3b12; color: white; }
Try row with nowrap: the six items are squeezed onto one line, narrower than their 4.5rem width, because flex items shrink by default (article 62). With wrap, they keep their width and move to more lines. With column, the 12rem-tall container holds them in a column, and column with wrap starts a second column.
flex-wrap
| Value | Effect |
|---|---|
nowrap (initial) | all items on one line; they shrink to fit, and overflow if they can't |
wrap | items that don't fit move to a new flex line, below in a row |
wrap-reverse | new lines go the other way: above the first one in a row |

The measured widths in the first panel are the default flex-shrink: 1 at work: on one line, the items give up width equally to fit.
Items shrink before anything else happens, but only down to their minimum size. By default, a flex item's minimum is min-width: auto, which means about the width of its longest word or widest unbreakable content. Tested in Chrome: in a 100px nowrap container, a span with a 154px unbreakable word stays 154px wide and overflows. Article 62 shows how min-width: 0 changes that.
With wrap, each line is laid out on its own: items on a line grow and shrink only with the other items on that line, and they don't line up in columns with items on other lines. If you want columns, that's a grid.
flex-flow
The shorthand sets direction and wrap together, in either order:
.tags { display: flex; flex-flow: row wrap; gap: 0.5rem; }
.stack { display: flex; flex-flow: column; gap: 1rem; }
gap
gap (article 63) puts space between items, and between lines when they wrap, without adding space at the edges. It replaced margin tricks and works in all browsers for flexbox since 2021:
.toolbar { display: flex; gap: 0.5rem; } /* between items */
.tags { display: flex; flex-wrap: wrap; gap: 0.5rem 0.75rem; } /* row-gap column-gap */
A first example
A site header with a logo on one side and links on the other is one of the most common flex layouts:
<header class="site-header">
<a class="logo" href="/">Shelf</a>
<nav><a href="/new">New</a> <a href="/events">Events</a> <a href="/basket">Basket</a></nav>
</header>
.site-header { display: flex; align-items: center; gap: 1rem; }
.site-header nav { display: flex; gap: 0.75rem; margin-inline-start: auto; } /* pushes the nav to the end */
The header is a row of two items, and the nav is a flex container of its own. margin-inline-start: auto on the nav takes all the free space before it, pushing it to the far end; article 63 explains auto margins.
Flexbox or grid?
| Use flexbox when | Use grid when |
|---|---|
| the content is one line of items, which may wrap | you need rows and columns that line up |
| each item's size should come from its content | the layout defines the sizes and items fill them |
| items should share leftover space | items should be placed in specific areas |
Both often appear together: a grid for the page and card layout, flexbox inside each card for its header row or button group. Neither is "better"; they answer different questions.
What's current
- Flexbox works in all browsers, and the old
display: boxanddisplay: flexboxsyntaxes and the-webkit-prefixes are long gone. gapfor flexbox has worked everywhere since 2021.display: block flexandinline flex, the two-keyword forms, are Baseline since 2023.reading-flow(article 64) makes keyboard order follow the visual order of reversed or reordered flex items, in Chrome.
Common questions
Why do my flex items have the same height?
align-items defaults to stretch, so items stretch across the cross axis to the height of the line. Use align-items: start to stop it (article 63).
Why don't my items wrap?
flex-wrap defaults to nowrap. Add flex-wrap: wrap.
Why does a long word or URL push my flex item wider than the container?
A flex item's minimum width is auto, about its longest unbreakable content. Set min-width: 0 on the item, and allow breaking with overflow-wrap: anywhere (article 62).
Do I need flex-direction: row?
No, it's the default. Only set flex-direction for columns or reversed directions.
Can I make only some children flex items? No: every in-flow child of a flex container is a flex item. Put the ones you want grouped in a wrapper.
Summary
display: flex(orinline-flex) makes a flex container; its in-flow children, including text and pseudo-elements, become blockified flex items.- The main axis is set by
flex-direction; the cross axis is perpendicular.rowfollows the writing mode. - Reversed directions change only the visual order, not tab order or reading order.
flex-wrap: wrapmoves items onto new lines; withnowrap, items shrink down to their minimum size, then overflow.flex-flowsets both;gapspaces items and lines.- Use flexbox for one-dimensional, content-sized lines of items, grid for two-dimensional layouts.
Practice questions
Write CSS for .tags that lays its children out in a row, wrapping onto new lines, with 0.5rem between them in both directions.
Answer
.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; }
Or flex-flow: row wrap instead of flex-wrap.
In the HTML below, how many flex items does .row have?
<div class="row">Hello <span>world</span><p style="position: absolute">note</p></div>
Answer
Two: an anonymous item for the text "Hello", and the span. The absolutely positioned paragraph is out of flow, so it isn't a flex item.
A container has the rule below, in an English page. Where is main-start, and what does justify-content: flex-start do?
.list { display: flex; flex-direction: column-reverse; }
Answer
Main-start is at the bottom, because the main axis runs bottom to top. flex-start packs the items at the bottom, with the first item in the HTML at the very bottom.
Six 6rem items are in a 20rem-wide flex container with the default flex-wrap. What happens, and how do you keep them at 6rem?
Answer
They stay on one line and shrink to fit, as far as their minimum content size allows. Add flex-wrap: wrap so they move to new lines, or flex-shrink: 0 if they should keep their width and overflow.
Previous: Popovers, Dialogs and the Top Layer: Styling, ::backdrop and Entry Animations
Next: Flex Sizing: flex-basis, flex-grow, flex-shrink and the flex Shorthand

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.