Skip to content
Back to all posts
Technology

CSS Flexbox Cheat Sheet: Alignment, Wrapping, and Responsive Rows

A practical CSS Flexbox reference covering display:flex, flex-direction, gap, justify-content, align-items, flex-wrap, and flex shorthand.

3 min read

TechSilo

Technology writer & developer

Flexbox is ideal for arranging items along a single main axis—a row or a column. Unlike CSS Grid, it is designed around the relationship between items on one axis at a time.

Start with a flexible row

css
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

The main axis defaults to horizontal in left-to-right writing modes. justify-content controls distribution along that axis; align-items controls alignment along the cross axis.

Core properties

| Property | Common values | What it changes |

| --- | --- | --- |

| display | flex, inline-flex | Creates a flex container |

| flex-direction | row, column, row-reverse | Main-axis direction |

| justify-content | flex-start, center, space-between | Main-axis distribution |

| align-items | stretch, center, baseline | Cross-axis alignment |

| flex-wrap | nowrap, wrap | Whether items may use multiple lines |

| gap | 0.75rem, 1rem | Space between flex items |

| flex | 1, 0 0 auto | How items grow, shrink, and calculate basis |

Build a responsive navigation row

css
.nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1rem;
}

.nav__search {
  flex: 1 1 16rem;
  min-width: 0;
}

.nav__actions {
  display: flex;
  gap: 0.5rem;
}

The search area can grow and shrink, but its preferred basis is 16rem. The min-width: 0 rule prevents some long inputs or text from forcing unwanted overflow.

Understand flex shorthand

css
.sidebar { flex: 0 0 15rem; }
.content { flex: 1 1 auto; min-width: 0; }

The three parts correspond to flex-grow, flex-shrink, and flex-basis. The sidebar neither grows nor shrinks in this example, while the main content can use remaining space.

Be careful with flex: 1: browsers commonly interpret it with a zero-basis behavior, which differs from flex: 1 1 auto when intrinsic item sizes matter.

Center an element

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

Use min-height: 100dvh where supported if your page needs to account for changing mobile viewport height.

Common mistakes

- Using justify-content: center when you meant cross-axis alignment.

- Forgetting flex-wrap: wrap when controls must fit small screens.

- Letting an item's automatic minimum size create horizontal overflow.

- Assuming order changes keyboard navigation or reading order. It does not.

- Reaching for Flexbox to coordinate both rows and columns when Grid would be clearer.

When to choose Grid

Use Flexbox for navigation, toolbars, button rows, tags, and distributed controls. Use Grid for two-dimensional card layouts, dashboards, or explicit row-and-column placement. See our [CSS Grid Cheat Sheet](/blog/css-grid-cheat-sheet-msl9yl1p) for the complementary approach.

Want to put this into practice?

Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.

Related blog posts