Skip to content
Innovialabs

CSS Flexbox Guide: Properties and Layout Examples

Practice3 min

In short

Flexbox lays elements out along one axis: a container with display: flex controls direction, alignment and gaps, while items control how they grow and shrink.

What Flexbox is and when to use it

Flexbox is a CSS layout mode that arranges elements in one line: a row or a column. It's a good fit for navigation, rows of buttons, centering and equal-height cards.

You turn it on with one property on the parent:

.container {
  display: flex;
}

The parent becomes a flex container, and its direct children become flex items. Flexbox has two axes: the main axis (where items flow) and the cross axis (perpendicular to it).

Container properties

PropertyWhat it doesCommon values
flex-directionDirection of the main axisrow, column, row-reverse, column-reverse
flex-wrapWrapping onto new linesnowrap, wrap
flex-flowShorthand for direction + wraprow wrap
justify-contentAlignment on the main axisflex-start, center, space-between, space-around, space-evenly
align-itemsAlignment on the cross axisstretch, center, flex-start, flex-end, baseline
align-contentDistribution of wrapped linesflex-start, center, space-between
gapSpace between items16px, 1rem 2rem

Note: with flex-direction: column the axes swap, so justify-content starts working vertically.

Item properties

PropertyWhat it does
flex-growShare of free space the item takes (default 0)
flex-shrinkHow much it shrinks when space runs out (default 1)
flex-basisStarting size before space is distributed
flexShorthand: grow, shrink, basis
align-selfIts own alignment on the cross axis
orderDisplay order

The most useful shorthands:

  • flex: 1 — the item grows and shares space equally with siblings;
  • flex: none — it neither grows nor shrinks;
  • flex: 0 0 200px — a fixed 200px width.

Common layouts

<header class="nav">
  <a href="/" class="logo">Logo</a>
  <nav class="menu">
    <a href="/about">About</a>
    <a href="/contacts">Contacts</a>
  </nav>
</header>
.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
}
.menu {
  display: flex;
  gap: 24px;
}

Centering a block

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

The block sits exactly in the middle on both axes.

A row of cards

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.card {
  flex: 1 1 260px;
}

Each card aims for 260px, grows to fill the row and wraps when space runs out. Cards in the same row get equal height thanks to the default align-items: stretch.

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

main takes all the free space, so the footer stays at the bottom even on a short page.

Common mistakes

  • Properties on the wrong element. justify-content goes on the container, flex: 1 goes on the item.
  • Long text breaks the layout. Flex items default to min-width: auto, so a long word or a table stops the item from shrinking. min-width: 0 fixes it.
  • Margins instead of gap. gap doesn't add stray space at the outer edges.
  • Flexbox for a 2D grid. If you need alignment by rows and columns at once, CSS Grid is easier.
  • Using order to change meaning. It changes only the visual order; keyboard and screen reader users follow the HTML order.

The full property reference is on MDN.

FAQ

Flexbox works in one dimension: a row or a column. Grid works in two at once. Navigation and button rows usually use Flexbox; page layouts and galleries use Grid. They're often combined.

We can help with this

If you'd rather get the result than figure it all out yourself — this is what we do every day.

Discuss a project

Read next

Have an idea or a task? Tell us about it

We'll reply within one business day and propose a solution. The consultation is free.