CSSLayoutbeginner

CSS Flexbox Guide with Examples

A practical flexbox guide with every key property, four real layouts, fixes and a cheat sheet.

Published
TL;DR – Quick Answer

CSS flexbox is a layout system that arranges items in a row or a column. Set display: flex on the parent, use justify-content for the main axis and align-items for the cross axis. Use grid when items must line up in rows and columns together.

On This Page

CSS flexbox is a layout system that arranges items in one line, either a row or a column. You set display: flex on a parent, and its direct children become flex items that you can align, space, shrink and grow.

This guide explains every property you need, gives a cheat sheet and builds four real layouts. You should already know selectors and the box model. If not, start with the CSS tutorial.

How CSS flexbox works

Flexbox has two parts: the container (the parent) and the items (its direct children). It also has two directions.

  • The main axis is the direction items flow. By default it runs left to right.
  • The cross axis is at a right angle to it. By default it runs top to bottom.
<div class="row">
  <div>One</div>
  <div>Two</div>
  <div>Three</div>
</div>
.row {
  display: flex;
  gap: 16px;
}

The three boxes now sit side by side with 16 pixels between them. Without flexbox, each div would take its own line.

If you change to flex-direction: column, the axes swap. The main axis now runs downwards, and the cross axis runs sideways. Most flexbox confusion comes from missing this swap.

Pro tip: Remember one rule. justify-content works along the main axis and align-items works along the cross axis. Check the direction first, then choose the property.

CSS flexbox properties for the container

Put these on the parent.

Property What it does Common values
display Turns flexbox on flex, inline-flex
flex-direction Sets the main axis row, column, row-reverse, column-reverse
flex-wrap Lets items move to a new line nowrap, wrap
flex-flow Short form of the two above row wrap
justify-content Places items on the main axis flex-start, center, flex-end, space-between, space-around, space-evenly
align-items Places items on the cross axis stretch, center, flex-start, flex-end, baseline
align-content Places wrapped lines on the cross axis normal, center, space-between
gap Adds space between items 16px, 1rem 2rem

The three space values

Say a row has free space left over after placing its items.

  • space-between puts the first and last items at the edges and shares the space between the rest.
  • space-around gives each item equal space on both sides, so the edges get half of a middle gap.
  • space-evenly makes every gap equal, including the edges.

align-items and align-content are different

align-items places items inside their line. align-content places the lines inside the container. align-content needs a container that allows wrapping and has spare height, so it often seems to do nothing.

CSS flexbox properties for the items

Put these on a child.

Property What it does Default
flex-grow Share of free space the item takes 0
flex-shrink How much the item gives up when space is short 1
flex-basis Starting size before growing or shrinking auto
flex Short form of the three above 0 1 auto
align-self Overrides align-items for one item auto
order Changes the visual order 0

What flex: 1 means

flex: 1 is short for flex: 1 1 0%. The item starts at zero size, then grows. If three items all have flex: 1, they get equal widths.

A worked sum makes this clear. Take a 900px container with no gap and three items:

.a { flex: 1; }
.b { flex: 1; }
.c { flex: 2; }

There are 4 shares in total, so one share is 900 / 4 = 225px. Items .a and .b get 225px each and .c gets 450px.

flex: auto is different. It means 1 1 auto, so each item starts from its content size and then shares what is left. Long text gives a wider item.

Common mistake: Using order to fix reading order. It changes only what people see. Keyboard and screen reader users still follow the HTML order, so fix the HTML instead.

CSS flexbox examples you will use at work

These four CSS flexbox examples cover most daily needs.

1. Center anything

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

The child sits in the middle of the screen, both ways. The min-height matters. Without extra height there is no vertical space to center in.

2. Navigation bar

<nav class="nav">
  <a class="logo" href="#">Logo</a>
  <a href="#">Courses</a>
  <a href="#">Login</a>
</nav>
.nav {
  display: flex;
  align-items: center;
  gap: 24px;
}
.logo {
  margin-right: auto;
}

margin-right: auto takes all the free space after the logo. So the logo stays left and the links move right.

3. Cards that wrap

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card {
  flex: 1 1 240px;
}

Each card wants 240px. When a row is full, the next card wraps. Cards then grow to fill their row, so a lone card on the last row stretches across it. Use grid if you want it to keep the column width.

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

On a short page, main grows and pushes the footer to the bottom of the screen. On a long page, the footer follows the content.

CSS flexbox vs grid

CSS flexbox and grid are both modern layout tools, and they work well together. Flexbox handles one direction at a time. Grid handles rows and columns at the same time.

Question Flexbox Grid
Directions One, row or column Two, rows and columns
Layout led by The size of the content The tracks you define
Wrapped items line up in columns No Yes
Good for Nav bars, buttons, form rows, centering Page layouts, galleries, dashboards
Overlapping items Hard Easy, place both in one cell

Use a simple test. If you only care about one line of items, choose flexbox. If items must line up both across and down, choose grid.

The wrapping cards above look like this in grid:

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}

Here a card on the last row keeps the same width as the cards above it. A common pattern is grid for the page and flexbox inside each card.

Common flexbox problems and fixes

Problem Reason Fix
justify-content does nothing Items already fill the main axis Remove flex: 1 or make the container larger
Vertical centering fails Container is only as tall as its content Add min-height
Long text breaks out of the box Items have min-width: auto and will not go below content size Add min-width: 0 to the item
Image looks stretched align-items is stretch by default Set align-items: flex-start
Items refuse to wrap flex-wrap is nowrap by default Add flex-wrap: wrap
Property has no effect on a grandchild Only direct children become items Make the middle element a flex container too

When stuck, open browser DevTools and click the small "flex" badge beside the element. The browser draws the axes, gaps and free space.

Interview note: "How do you center a div?" is still asked often. Give the flexbox answer, then add that display: grid; place-items: center; does it in two lines. More questions are in CSS interview questions.

CSS flexbox cheat sheet

Keep this CSS flexbox cheat sheet near you for the first few projects.

Goal Code
Items in a row display: flex
Items in a column flex-direction: column
Center on main axis justify-content: center
Center on cross axis align-items: center
Push to both ends justify-content: space-between
Equal widths flex: 1 on each item
Fixed sidebar flex: 0 0 250px
Fill what is left flex: 1 on one item
Push one item away margin-left: auto
Align one item alone align-self: flex-end
Wrap to next line flex-wrap: wrap
Space between items gap: 16px

How to practise

Build three things without copying: a nav bar, a pricing section with three cards and a login form centered on the page. Resize the window after each one, and fix whatever breaks.

Then rebuild the pricing section with grid and compare. Write your HTML with proper tags first, as shown in the HTML tutorial. After layouts feel easy, move to JavaScript fundamentals and then the React JS tutorial, where you will use the same flexbox code inside components.

Frequently Asked Questions

What is CSS flexbox used for?
It arranges items in one direction. Common uses are navigation bars, button groups, form rows, cards and centering content.
Should I learn flexbox or grid first?
Learn flexbox first, because it solves small daily layouts. Learn grid next for page layouts where rows and columns must line up.
What does flex: 1 mean?
It is short for flex: 1 1 0%. The item starts at zero size and takes one share of the free space, so items with the same value get equal widths.
Why is justify-content not working?
There may be no free space because the items already fill the main axis. Also check that display: flex is on the direct parent and that you have the right axis.
How do I center a div with flexbox?
On the parent, set display: flex, justify-content: center and align-items: center. Give the parent a height, such as min-height: 100vh, to center vertically on the screen.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 3 December 2026 LinkedIn
Chat with us