CSS Flexbox

Quick reference for CSS Flexbox: main and cross axes, default alignment, gap, and why flex items default to min-width: auto.

リファレンスページの本文は現在英語です。

A flex container lays out its children along one main axis, then optionally wraps to additional lines. The CSS tutorial uses a short .actions { display: flex; flex-wrap: wrap; gap: 8px; } rule. This page is the property and boundary reference for that model, not a rewrite of the layout chapter or the #layout anchor.

The main axis follows flex-direction (row by default, left to right in a horizontal left-to-right language). The cross axis is perpendicular. justify-content distributes space on the main axis; align-items aligns on the cross axis. gap (and row-gap / column-gap) puts space between items and does not collapse like vertical margins in block layout.

Examples were parsed for syntax and structure. Flex line wrapping and overflow were not measured in a browser here.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Axes, defaults, and gap

Changing flex-direction to column swaps the axes: the main axis becomes vertical, and justify-content then works vertically. flex-wrap: wrap allows additional lines when items do not fit; wrapped lines do not automatically share the same cross-axis track sizing the way a grid column would, which is the tutorial’s Flexbox pitfall.

Syntax or propertyInitial / defaultNotes
display—flex or inline-flex (or the two-value forms block flex and inline flex).
flex-directionrowrow, row-reverse, column, or column-reverse. Sets the main axis.
flex-wrapnowrapnowrap, wrap, or wrap-reverse. nowrap is why a single line may overflow instead of wrapping.
justify-contentflex-startMain-axis distribution: flex-start, flex-end, center, space-between, space-around, space-evenly, and others from CSS Box Alignment.
align-itemsstretchCross-axis alignment of items. stretch, flex-start, flex-end, center, and baseline are the usual flex values. The later alignment keyword normal also stretches flex items in typical cases.
align-contentstretchDistributes extra space between flex lines when the container wraps. It has no effect on a single line.
gap0Shorthand for row-gap and column-gap. Space between items, not an item margin, and not collapsed.
flex0 1 autoShorthand for grow, shrink, and basis. The default is equivalent to flex: initial. flex: auto is 1 1 auto. flex: none is 0 0 auto. flex: 1 is 1 1 0%.
flex-grow0How the item takes leftover main-axis space. 0 means it will not grow unless you change it.
flex-shrink1How the item shrinks when the line is too short. A min-width: auto floor can still stop the shrink; see the next section.
flex-basisautoThe starting main size before grow and shrink. auto usually uses the item’s width or height, or its content size.

A wrapping row with gap

display: flex;
flex-wrap: wrap;
gap: 8px;
justify-content: flex-start;

Default row direction, wrapping, and gap between controls, matching the idea of the tutorial .actions rule.

  • gap: 8px sets both row-gap and column-gap. It does not collapse with the buttons’ margins.
html
<div class="actions">
  <button type="button">Plant</button>
  <button type="button">Water</button>
  <button type="button">Harvest</button>
</div>
<style>
.actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-start; }
.actions button { font: inherit; }
</style>

What this markup means

The container is a row flex container that may wrap. The three buttons start on the main axis with 8px gutters from gap. They will not automatically form equal grid-like columns when they wrap, because Flexbox is one-dimensional.

Related: CSS tutorial: layout, Display, Grid

MDN documentation → · CSS specification →

Default min-width: auto

Flex items (and grid items) have an automatic minimum size. For width, the initial min-width is auto, which in this context is a content-based minimum, not 0. flex-shrink: 1 therefore may not shrink an item below the size of its unbreakable content, such as a long word, a URL, or a wide image.

Setting min-width: 0 (or a smaller definite minimum), or allowing the content to wrap or overflow, lets the item shrink with the flex line. The same idea applies to min-height: auto on a column flex item. This is a frequent reason a flex child overflows a card even when flex-shrink is at its default of 1.

Syntax or propertyInitial / defaultNotes
min-widthautoOn a flex item, auto is a content-based minimum and can block shrinking. 0 (or another definite length) allows the item to shrink below that content size.
min-heightautoThe same automatic minimum, on the height axis, which matters when flex-direction is column.

min-width: auto versus min-width: 0

min-width: auto; /* initial on flex items */
min-width: 0;

The automatic minimum can keep a flex item as wide as its unbreakable content. min-width: 0 allows shrink.

  • The long path is documentation text, not a user file path, and nothing is read from disk. Overflow behaviour was not measured in a browser.
html
<div class="row">
  <p class="grow">A short label</p>
  <p class="item auto">docs/garden/week-01/planting-notes</p>
  <p class="item zero">docs/garden/week-01/planting-notes</p>
</div>
<style>
.row { display: flex; gap: 8px; width: 240px; }
.grow { flex: 1 1 auto; margin: 0; }
.item { flex: 1 1 auto; margin: 0; overflow-wrap: normal; }
.auto { min-width: auto; }
.zero { min-width: 0; overflow-wrap: anywhere; }
</style>

What this markup means

All three children can grow and shrink. The middle item keeps the initial min-width: auto, so the unbreakable path can hold the item at a content minimum. The last item sets min-width: 0 and allows wrapping, so it can shrink with the 240px flex line. This is the usual fix when a flex child appears not to shrink.

Related: Grid, Box model

MDN documentation → · CSS specification →