Skip to content

Intermediate · CSS layout and responsive design · topic 24 of 64 · 30 min

Flexbox

Explanation

Flexbox lays out items in one direction, a row or a column. You turn it on with display: flex on the parent, then control spacing with gap and alignment with justify-content and align-items. It is perfect for navbars, button rows and centring things.

Parent and children

Flexbox works on two levels. The parent with display: flex is the flex container. Its direct children are flex items, and they line up in a row by default.

Think of it like this: Think of people standing in a queue at a ticket counter. The queue (the container) decides the direction, how close people stand (gap) and whether they bunch at the front or spread out. Each person (an item) can also ask for more or less space.

Two axes

The main axis is the direction items flow: left to right for a row, top to bottom for a column. The cross axis runs across it. justify-content arranges items along the main axis; align-items aligns them along the cross axis.

css
.navbar {
  display: flex;
  flex-direction: row;            /* default; column stacks them */
  justify-content: space-between; /* main axis */
  align-items: center;            /* cross axis */
  gap: 16px;                      /* space between items */
}
A navbar: logo on the left, links on the right, vertically centred
justify-contentResult
flex-startItems packed at the start
centerItems in the middle
space-betweenFirst and last at the edges, equal space between
space-around / space-evenlySpace around every item

Wrapping

By default items stay on one line and shrink. flex-wrap: wrap lets them move to the next line when there is no room, which is useful for tags, filter chips or a row of cards on a phone.

Growing and shrinking

css
.search-bar { display: flex; gap: 8px; }
.search-bar input  { flex: 1; }      /* take all the free space */
.search-bar button { flex: none; }   /* keep its natural size */
flex: 1 means "grow to fill the space that is left"

flex is a shorthand for three values: flex-grow (how much to grow), flex-shrink (how much to shrink) and flex-basis (the starting size). flex: 1 is the most common setting.

Perfect centring

css
.center-me {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100dvh;
}
Centre anything horizontally and vertically
Tip: Use gap for spacing between flex items instead of margins on each item. It is cleaner and never leaves extra space at the ends.
Careful: The order property and flex-direction: row-reverse change the visual order but not the order for keyboard and screen readers. Keep the HTML in a logical order instead.

Watch the lesson

Flexbox · 1/6

Flexbox = one direction

Flexbox lines items up in one direction, a row or a column.

Prefer a video by a teacher? Find free videos on “css flexbox tutorial for beginners” on YouTube ↗

In real life: A food delivery app header

The top bar of a food app shows the location on the left, a search box that fills the middle and a profile icon on the right. One display: flex with align-items: center, gap: 12px and flex: 1 on the search box does the whole layout, and it still fits on a small phone.

Practice (0/2 done)

Task 1 · Code · HTML

Make the .nav a flex container with justify-content: space-between, align-items: center and a gap of 16px.

Task 2 · Code · HTML

Build a search bar: .search is a flex container with gap 8px, the input inside it has flex: 1 so it fills the space, and the button keeps its size.

Quick quiz

Question 1

1.Where do you write display: flex?

Question 2

2.In a row, which property centres items vertically?

Question 3

3.What does flex: 1 on one item do?

Question 4

4.Items overflow on a small phone. Which property lets them move to a new line?

Question 5

5.What is the best way to add equal space between flex items?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next: CSS Grid →

← Previous: Arrays and array methods