Contents

Frontend Development › CSS

Flexbox

One-dimensional layout along a row or a column.

Also known as: CSS Flexbox, flex layout, display: flex, flex container

Flexbox lays items out along one axis, as a row or a column, and gives you control over spacing, alignment and how items grow or shrink. It’s the go-to tool for toolbars, button groups, centering and card rows.

.toolbar {
  display: flex;
  justify-content: space-between;   /* along the main axis */
  align-items: center;              /* across the other axis */
  gap: 12px;
}
<div class="toolbar"><h1>Orders</h1><button>New</button></div>

Setting display: flex on a parent makes its children flex items.

The two axes

  • Main axis: the direction items flow (flex-direction: row by default, or column).
  • Cross axis: perpendicular to it.

justify-content positions items along the main axis; align-items positions them along the cross axis. People mix these up until it clicks. If you switch to flex-direction: column, the two swap roles.

Properties you’ll use most

On the containerDoes
flex-directionrow or column
justify-contentflex-start, center, space-between, space-around…
align-itemsstretch, center, flex-start…
flex-wrapAllow items to wrap onto new lines
gapSpace between items
On an itemDoes
flex: 1Grow to share leftover space equally
flex-shrink: 0Don’t shrink
align-selfOverride alignment for one item

Centering something

.parent { display: flex; justify-content: center; align-items: center; min-height: 100vh; }

Tips

  • Long text may overflow a flex child. Add min-width: 0 to the item so it can shrink.
  • Flexbox is one-dimensional. For rows and columns that line up, use CSS Grid.
  • Use DevTools: many browsers show a flex overlay and let you toggle the properties.