CSS Flexbox Cheatsheet - Flexbox Layout Reference

For developers who have fought with floats or inline-block for centering and equal-width rows. Flexbox is the modern way to lay out a single row or column quickly. The recurring confusion is that Flexbox is one-dimensional and that justify-content and align-items act on different axes. By the end you can center anything, build a toolbar and a wrapping card row, and control item sizing with flex-grow/flex-shrink/flex-basis without trial and error.

Web Services·44 commands·Last updated 2026-07-21
cssflexboxLayoutFrontend

Typical Use Case

For front-end developers doing layout work who would rather not fight float or absolute positioning again. Almost all of the difficulty in Flexbox lives in one distinction: flex-direction sets the main axis, align-items acts on the cross axis, so flip the main axis and alignment flips with it. Container properties, main-axis distribution, cross-axis alignment and item-level flex shorthand sit in separate layers, followed by ready-made combinations for centring, sticky footers and responsive card grids, and why flex: 1 is not 1 1 auto.

Container Properties 8

display: flex
Enable flex layout (block-level)
display: inline-flex
Enable flex layout (inline-level)
flex-direction: row
Main axis = row, horizontal (default)
flex-direction: column
Main axis = column, vertical
flex-direction: row-reverse
Row, reversed order
flex-direction: column-reverse
Column, reversed order
flex-wrap: wrap
Allow wrap (default nowrap)
flex-flow: row wrap
Shorthand for direction + wrap

Gap & Main-axis Alignment 8

gap: 16px
Gap between items (row & column)
row-gap: 16px
Row gap only
column-gap: 16px
Column gap only
justify-content: flex-start
Start of main axis (default)
justify-content: center
Center on main axis
justify-content: flex-end
End of main axis
justify-content: space-between
Space between, ends flush
justify-content: space-around
Equal space around each item

Cross-axis Alignment 7

justify-content: space-evenly
All gaps exactly equal
align-items: stretch
Stretch to fill cross axis (default)
align-items: flex-start
Start of cross axis
align-items: center
Center on cross axis
align-items: flex-end
End of cross axis
align-items: baseline
Align on baseline
align-content: center
Cross-axis alignment of wrapped lines

Item Properties 10

flex-grow: 1
Grow factor, 0 = no grow (default)
flex-shrink: 1
Shrink factor, 0 = no shrink (default 1)
flex-basis: 200px
Initial size (default auto)
flex: 1
Shorthand: 1 1 0%, share free space
flex: 0 0 200px
Shorthand: no grow/shrink, fixed 200px
flex: auto
Shorthand: 1 1 auto
flex: none
Shorthand: 0 0 auto, no grow/shrink
align-self: flex-end
Override align-items for one item
order: 1
Display order, default 0, can be negative
order: -1
Move to the front

Common Patterns 6

justify-content: center; align-items: center
Horizontal & vertical center
justify-content: space-between
Space-between (navbar left/right)
flex: 1
Share remaining space
margin-left: auto
Push item to end of main axis
min-height: 100vh
Sticky footer with column (child flex:1)
flex: 1 0 auto
Fixed base, no shrink (sidebar)

Responsive Tips 5

flex-wrap: wrap
Auto wrap when window narrows
flex-basis: 200px; flex-grow: 1
Responsive card grid
flex-wrap: wrap-reverse
Reverse wrap layout
@media (max-width: 768px) { flex-direction: column }
Switch to column on mobile
flex: 1 1 100%
Fill full row, force wrap

Parameter matrix

ParameterEffectExample
display: flex启用弹性布局,容器变为块级display: flex
display: inline-flex启用弹性布局,容器按行内级参与排版display: inline-flex
flex-direction: row主轴为水平行方向(默认值)flex-direction: row
flex-direction: column主轴为垂直方向,此时交叉轴变为水平flex-direction: column
flex-direction: row-reverse主轴水平但排列方向反转flex-direction: row-reverse
flex-direction: column-reverse主轴垂直但排列方向反转flex-direction: column-reverse
flex-wrap: wrap允许项目换行,默认 nowrap 不换行flex-wrap: wrap
flex-wrap: wrap-reverse换行时交叉轴方向反转flex-wrap: wrap-reverse
flex-flow: row wrapflex-direction 与 flex-wrap 的简写flex-flow: row wrap
gap / row-gap / column-gap项目间距,可分别控制两个方向gap: 16px
justify-content: flex-start主轴起始对齐(默认)justify-content: flex-start
justify-content: center主轴居中对齐justify-content: center
justify-content: flex-end主轴末尾对齐justify-content: flex-end
justify-content: space-between两端对齐,项目间空隙均分justify-content: space-between
justify-content: space-around每个项目两侧等距,端部空隙较小justify-content: space-around
justify-content: space-evenly所有空隙完全相等justify-content: space-evenly
align-items: stretch交叉轴拉伸填满(默认)align-items: stretch
align-items: center交叉轴居中对齐align-items: center
align-items: baseline交叉轴按文本基线对齐align-items: baseline
align-content: center多行内容整体在交叉轴上的对齐,需配合换行align-content: center
flex-grow放大比例,0 表示不放大flex-grow: 1
flex-shrink缩小比例,默认 1 表示允许压缩flex-shrink: 1
flex-shrink: 0禁止被压缩,保持基础尺寸flex: 0 0 200px
flex-basis: 200px分配剩余空间前的初始主轴尺寸flex-basis: 200px
flex: 1简写 1 1 0%,等分剩余空间flex: 1
flex: 0 0 200px简写:不放大不压缩,固定 200pxflex: 0 0 200px
flex: auto简写 1 1 auto,按内容起始再分配flex: auto
flex: none简写 0 0 auto,完全不伸缩flex: none
flex: 1 1 100%占满整行,强制换到下一行flex: 1 1 100%
flex: 1 0 auto固定基础尺寸且不压缩,常用于侧边栏flex: 1 0 auto
align-self覆盖父容器的 align-items,单独控制某一项align-self: flex-end
order视觉排列顺序,默认 0,可为负值order: -1
margin-left: auto用外边距把项目推到主轴末尾margin-left: auto
min-height: 100vh配合 column 容器与 flex: 1 实现粘性页脚min-height: 100vh
@media (max-width: 768px) { flex-direction: column }窄屏时把横向布局切为纵向@media (max-width: 768px) { flex-direction: column }

Common pitfalls

Symptom父容器写了 flex: 1 子项却被压扁,内容溢出或高度塌陷。

Causeflex: 1 等价于 flex: 1 1 0%,shrink 分量为 1 表示允许被压缩到内容尺寸以下;垂直方向的主内容区没显式给基准尺寸就会被挤没。

Fix需要不被压缩时改用 flex: 1 0 auto,或在纵向撑开场景给子项写 flex-grow: 1 并给父容器 min-height: 100vh。

Symptomalign-items 明明写在容器上,却看不出任何水平居中的效果。

Cause容器的 flex-direction 是 column,此时主轴变成垂直方向,交叉轴随之变为水平,align-items 控制的正是水平方向;而开发者想控制的是垂直方向。

Fix在 column 容器里要垂直居中应改用 justify-content: center;反之在 row 容器里垂直居中用 align-items: center。

Symptom多行 flex 容器里各行之间的分布不受 justify-content 控制,行整体贴在一起。

Causejustify-content 只作用于主轴上的项目分布,多行整体在交叉轴上的分布由 align-content 控制。

Fix给容器加 align-content: center 之类的取值来分配交叉轴空间,同时确认 flex-wrap: wrap 确实生效。

Symptom给项目加了 flex-direction / gap 却完全没反应。

Cause这些属性作用在容器上,加在了子项元素上;子项只能用 flex、flex-basis、align-self、order 这类项目属性。

Fix把 flex-direction、flex-wrap、gap、justify-content 移到父容器声明,子项只保留 flex 与 align-self。

Symptom使用 order 改变顺序后,Tab 键的焦点顺序与视觉顺序不一致。

Causeorder 只改变视觉排列,不改变 DOM 与焦点顺序,屏幕阅读器和键盘导航仍按文档顺序走。

Fix尽量在 DOM 结构上直接排序,不用 order 制造视觉差异;必须用时同时检查键盘可达性。

Symptomflex-wrap: wrap 配了 flex-basis 却没有换行,窗口缩窄时卡片被压扁而不是换行。

Cause子项的 min-width 默认是 auto,内容较长时不允许被压缩到内容宽度以下,容器不会认为需要换行。

Fix给卡片加 min-width: 0(或按内容类型给合适的 min-width),让 flex-basis 成为真实生效的换行依据。

Symptom页脚没有吸底,内容少时整页高度塌缩。

Cause只给页脚设了 margin-top: auto,但父容器不是 flex 容器,或没有指定 min-height。

Fix父容器写 display: flex; flex-direction: column; min-height: 100vh,中间主内容区写 flex: 1 撑开剩余空间。

Troubleshooting

  1. 1确认容器确实进入了弹性布局

    display: flex

    若子项仍按普通块级排列,说明这条声明没落到实际父容器上,检查选择器是否命中。

  2. 2用 justify-content 确认主轴方向的居中

    justify-content: center; align-items: center

    两条同时写可实现水平垂直居中,但要先确定当前 flex-direction 到底是 row 还是 column。

  3. 3确认换行是否生效

    flex-wrap: wrap; flex-basis: 200px; flex-grow: 1

    这组值构成响应式卡片网格:基础宽 200px、有余量就放大、放不下就换行。

  4. 4实现粘性页脚的完整容器写法

    display: flex; flex-direction: column; min-height: 100vh

    主内容区再写 flex: 1,内容不足一屏时页脚也会被推到最底部。

  5. 5窄屏切列布局

    @media (max-width: 768px) { flex-direction: column }

    视口宽度不超过 768px 时主轴转为垂直,横向排列改为纵向堆叠。

  6. 6单项覆盖容器的对齐方式

    align-self: flex-end

    用于在 align-items: center 的容器里把某个按钮单独推到末尾。

Command Examples

Center on both axes

.box { display: flex; justify-content: center; align-items: center; height: 240px; }

justify-content works along the main axis and align-items along the cross axis. With only display: flex everything piles into the top-left corner, which reads as the stylesheet not loading at all.

Two-ended navbar with one item pushed to the end

.nav { display: flex; align-items: center; justify-content: space-between; } .nav .logout { margin-left: auto; }

space-between behaves like flex-end with exactly two children, but redistributes as soon as the child count changes. Use gap or margin-left: auto so the spacing is not hostage to the DOM structure.

Responsive card grid: wrap with a basis and grow

.grid { display: flex; flex-wrap: wrap; gap: 16px; } .grid > .card { flex: 1 1 200px; } @media (max-width: 768px) { .grid { flex-direction: column; } }

flex: 1 1 200px uses 200px as the basis and allows growth and shrink, wrapping once the container runs out of width — replacing most hand-written percentage breakpoints.

Sticky footer: the content area grows with flex: 1

body { display: flex; flex-direction: column; min-height: 100vh; } main { flex: 1; }

flex: 1 really means 1 1 0%: the item is computed at zero and then given all leftover space, which is what keeps the footer pinned to the viewport bottom instead of floating mid-page under short content.

Reorder visually with order and lay out wrapped lines

.grid { display: flex; flex-wrap: wrap; align-content: center; gap: 8px; } .grid .featured { order: -1; }

order changes visual sequence only, not DOM or tab sequence. Treating it as the accessibility fix leaves keyboard and screen-reader users reading in the wrong order.

Common Pitfalls

  • Flex items default to min-width: auto, so a long URL, a nowrap code block, or a table can blow out the container and refuse to shrink — an inexplicable horizontal scrollbar. Add min-width: 0 to let inner text ellipsize or wrap.
  • flex: 1 means 1 1 0%, not "split the content width". It lays out differently from flex: 1 1 auto whenever a content basis exists, so switching auto to 0% visibly shifts elements that used to line up.
  • After flex-wrap: wrap, the default align-content: stretch spreads the lines across the container height, which looks like margin being ignored. Set align-content: flex-start for tight row spacing.
  • Mixing margin and gap on the same axis stacks two mechanisms, and margin does not participate in leftover-space distribution — hence a mysterious extra gap after the last item. Pick one per axis.
  • A flex container establishes a new formatting context, so position: absolute children are contained by the container, not the viewport. Dropdowns and popovers that rely on viewport positioning jump once moved inside.
  • overflow: hidden on the flex container only clips what spills out; the container still sizes to its content minimum, so the parent layout is still pushed. Constrain the items with min-width/min-height instead.

Tips

  • flex: 1 is shorthand for flex: 1 1 0% — it divides the remaining space equally.
  • align-items aligns on the cross axis; with flex-direction: column the cross axis becomes horizontal.
  • The gap property replaces the old margin trick, setting item spacing more cleanly.
  • flex-wrap: wrap combined with flex-basis enables responsive grid-like layouts.
  • Prefer the flex shorthand; setting flex-grow/shrink/basis separately easily overrides intended behavior.

FAQ

What does flex: 1 actually expand to, and why does it eat up the leftover space?

flex: 1 is shorthand for flex: 1 1 0%, the three numbers being flex-grow, flex-shrink and flex-basis. Because flex-basis is 0%, the item starts at zero size, so the container treats the entire main-axis space as free space and hands it out in the grow ratio 1:1 — hence the equal-width look. Compare flex: auto (1 1 auto), which keeps the content intrinsic size so short items look narrower, and flex: 0 0 200px, which opts out of growing and shrinking entirely and pins the item at 200px.

Why does my container collapse to content height after I set align-items: center?

Because align-items controls the cross axis, and which axis is cross is decided by flex-direction. With the default flex-direction: row the cross axis is vertical, so align-items: center sizes each item to its content and centers it, collapsing the container to the tallest child. The default align-items: stretch is what makes items fill the container height. A related trap: under flex-direction: column the cross axis is horizontal, so align-items: center centers horizontally and vertical centering requires justify-content: center.

How do I make a card list reflow into a responsive grid without any media query?

The trick is flex-wrap: wrap plus flex-basis. Set the container to display: flex; flex-wrap: wrap; gap: 16px and give each card flex: 1 1 200px — flex-basis: 200px supplies the ideal width, flex-grow: 1 lets the cards on a line stretch to fill the row, and flex-shrink: 1 allows them to compress. On a wide viewport they fill one stretched row; once the viewport can no longer fit 200px, they wrap to the next line, giving you a responsive grid with zero @media rules.

How do space-between, space-around and space-evenly differ visually?

All three are justify-content values distributing leftover main-axis space, but they divide it differently. space-between pins the first and last item to the edges and splits the rest evenly — the classic navbar distribution. space-around gives every item equal spacing on both sides, so the outer margins are half the interior gaps. space-evenly makes every gap identical, including the gaps at the container edges. When a card row or button group looks like its edge padding does not match the inner spacing, switching among these three is the fix.

How do I build a sticky footer that sits at the bottom only when content is short?

Give the outer wrapper display: flex; flex-direction: column; min-height: 100vh so the main axis becomes vertical, then set the main content area to flex: 1 — its flex-grow consumes all leftover height and pushes the footer to the bottom. When content exceeds one viewport the wrapper simply grows and the footer follows the content instead of overlapping it. If the footer gets squashed because flex-shrink applies, give the content area flex: 1 0 auto so it keeps its base size and is not compressed.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us