Flexbox Cheatsheet: Interactive Visual Guide

October 15, 2026  |  Cheatsheet  |  CSS

By Syed · Frontend Engineer & Creator of UITweak

Flexbox is the layout tool for one-dimensional arrangements: rows or columns of items. Every property below has a live demo. Click the values, watch the boxes move, and copy the code when it looks right.

display: flex

Setting display: flex turns the element into a block-level flex container and its children into flex items, so the browser runs the flex layout algorithm instead of normal block flow. Items are sized from their flex-basis, leftover or missing main-axis space is shared with grow and shrink factors, and painting still follows document order.

1
2
3
.container {
  display: flex;
}

/* Tailwind: <div className="flex"> */

flex-direction

flex-direction sets the main axis direction: row runs left to right, row-reverse flips it, and the column values stack vertically. Changing it also flips which edge flex-start refers to and rotates the cross axis, so identical alignment values move items differently, while keyboard and screen-reader order still follow the DOM.

1
2
3
.container {
  display: flex;
  flex-direction: row;
}

/* Tailwind: flex flex-row */

justify-content

justify-content distributes leftover main-axis space after items are sized: space-between and space-evenly split it between, around, or evenly among items, while center groups items in the middle. When items are wider than the container there is no leftover space, so centering pushes the first item past the visible edge and clips it on the left.

1
2
3
.container {
  display: flex;
  justify-content: flex-start;
}

/* Tailwind: flex justify-start */

align-items

align-items positions every item on the cross axis: vertical alignment in a row, horizontal in a column. The default stretch fills the container's cross size, but only when the item has no definite cross size of its own. baseline aligns items by their first text baseline, which is how mixed font sizes share one line.

1
2
3
.container {
  display: flex;
  align-items: stretch;
}

/* Tailwind: flex items-stretch */

flex-wrap

With the default nowrap, items stay on one line and shrink to fit when space runs short. wrap lets the browser split items into multiple flex lines, each sized on the cross axis independently, which is why align-content only matters on wrapped containers. wrap-reverse stacks the extra lines on the opposite side of the cross axis.

1
2
3
4
5
6
7
8
.container {
  display: flex;
  flex-wrap: nowrap;
}

/* Tailwind: flex flex-nowrap */

gap

gap inserts fixed gutters between items and wrapped lines, and the browser subtracts that space before dividing leftover space among flexible items. Unlike margins, gaps never collapse and never add space at the container's outer edges, removing the old negative-margin hack. One value sets both axes, with row-gap and column-gap for when rows and columns differ.

1
2
3
.container {
  display: flex;
  gap: 16px;
}

/* Tailwind: flex gap-[16px] */

flex-grow

flex-grow splits leftover main-axis space after every item reaches its flex-basis: the browser totals all grow factors and hands each item a proportional share. An item with flex-grow: 2 gets twice the extra space of a sibling with 1, starting from each item's basis rather than zero, while the default 0 leaves items at their basis size.

1
2
3
.item {
  flex-grow: 1; /* takes available free space */
}

/* Tailwind: grow */

flex-shrink and flex-basis

flex-basis is the item's hypothetical main size before free space is distributed, overriding width in rows when both are set. When items overflow, flex-shrink factors share the deficit: 1 shrinks everyone proportionally to their basis, 0 locks an item so siblings absorb the squeeze, and flex: 1 means 1 1 0%, which is why two such items always end up equal.

.item {
  flex-shrink: 0;   /* never shrink */
  flex-basis: 200px; /* start at 200px */
}

/* Shorthands */
.flex-1    { flex: 1 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-none { flex: none; }

order

order regroups items into ordinal groups before layout and painting, so order: -1 renders ahead of all default order: 0 items regardless of HTML position. The browser processes items group by group, then by document order inside each group. Focus order, tab order, and screen readers still follow the DOM, so visual reordering with an illogical DOM creates an accessibility mismatch.

1
2
3
.first-visual {
  order: -1; /* shows first, DOM unchanged */
}

/* Tailwind: order-first, order-1, order-last */

align-self

align-self overrides the container's align-items for one item only, accepting the same values plus auto, which falls back to the container's setting. It is how a single button sits at the bottom of a card while siblings stay at the top, and nothing else in the layout changes.

The middle box overrides the container alignment:

1
2
3
.special-item {
  align-self: center; /* overrides align-items */
}

/* Tailwind: self-center */

Common Production Bugs & Fixes

Bug 1: Flex children overflow instead of shrinking, and text refuses to truncate

What goes wrong: a text label inside a flex item pushes past the container edge or squeezes its siblings, and text-overflow: ellipsis never kicks in. Why: flex items default to min-width: auto, which sets the automatic minimum size to the content's minimum size, so the browser will not shrink the item below its longest unbreakable word. Fix: give the item min-width: 0, then apply the truncation trio:

.card-title {
  min-width: 0;          /* let the flex item shrink below content size */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

Apply min-width: 0 to every flex item in the chain, because one ancestor with min-width: auto reintroduces the overflow.

Bug 2: `flex: 1` and `flex: auto` do not divide space the same way

What goes wrong: flex: 1 produces equal columns while flex: auto produces content-sized columns that only then grow. Why: flex: 1 expands to flex: 1 1 0%, so every item starts from a zero basis and all free space is divided purely by grow factors. flex: auto expands to flex: 1 1 auto, so each item starts from its natural content size and only the remaining space is shared. Fix:

.equal-columns > * { flex: 1; }    /* 1 1 0% : identical widths, content ignored */
.natural-share > * { flex: auto; } /* 1 1 auto: content-sized first, then share the rest */
.fixed-item       { flex: none; }  /* 0 0 auto: never grow, never shrink */

Bug 3: `margin: auto` absorbs the space that `justify-content` would distribute

What goes wrong: adding margin-left: auto to one item pins it to the far edge while justify-content seems to stop working, or a lone margin: auto centers an item and ignores the container's alignment. Why: free space is absorbed by auto margins before justify-content distributes anything, so an auto margin on one side consumes all free space on that side. Fix: use auto margins deliberately:

.navbar { display: flex; align-items: center; }
.navbar .login { margin-left: auto; } /* pins only this item to the right edge */

/* center one item on both axes without touching the container */
.dialog { display: flex; }
.dialog .box { margin: auto; } /* absorbs free space on all four sides */

margin-top: auto does the same on the cross axis, pushing an item to the bottom of a row container: the standard trick for pinning a card button to the bottom.

Real-World Recipes

Recipe 1: Perfect centering with unknown content size

Use when a modal, empty state, or hero message must sit exactly in the middle of its parent no matter how tall the content is. Make the parent a flex container, center on both axes, and give it a minimum height so there is something to center within.

.center-wrap {
  display: flex;
  justify-content: center; /* main axis */
  align-items: center;     /* cross axis */
  min-height: 100vh;       /* or 100dvh on mobile */
  padding: 1rem;           /* breathing room on small screens */
}

Recipe 2: Navbar with logo left and links right

Use when a header has a brand on one edge and navigation on the other, without floats or absolute positioning. Keep everything in one flex row and let an auto margin on the nav group push it to the far edge, so adding or removing links never breaks the alignment.

.site-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
}
.site-header nav {
  margin-left: auto; /* absorbs free space, pins nav right */
  display: flex;
  gap: 1rem;
}

Recipe 3: Equal-height cards with buttons pinned to the bottom

Use when a row of cards has uneven text but every button must line up at the same baseline. Make each card a vertical flex container and give the button margin-top: auto, which consumes all free space above it and pins it to the card's bottom edge.

.card-row { display: flex; gap: 1rem; }
.card {
  display: flex;
  flex-direction: column;
  flex: 1;            /* equal widths */
}
.card .btn {
  margin-top: auto;   /* pins the button to the bottom */
}

FAQ

Why does justify-content: center clip my content on narrow screens?

Centering only positions items inside leftover space. When items are wider than the container there is none, so the group overflows symmetrically and the start edge scrolls out of view. Wrap the items in an inner container with margin: 0 auto instead, or allow wrapping.

Does flex-direction: row automatically flip in right-to-left languages?

Yes: in an RTL context row starts at the right edge, so flex-start means the right side. Use row-reverse only to reverse against the writing direction, and prefer flex-start over physical directions so the layout mirrors on its own.

Can flex-grow take decimal values like 1.5?

Yes. Grow and shrink factors accept any non-negative number, and the browser treats them as pure ratios. flex-grow: 1.5 against a sibling's 1 splits leftover space 3:2, handy for fine proportional tuning without extra wrappers.

Why is my width: 300px ignored on a flex item?

On the main axis, flex-basis wins over width (or height in a column). With the default flex: 0 1 auto the basis is auto, which falls back to the width; but flex: 1 sets the basis to 0% and the width is ignored. Set flex-basis: 300px, or use flex: 0 0 300px, for a fixed main size.

Share this guide