Skip to content
Tutorials Center
My learning

Topics

Lesson 69 of 139 ยท Flexbox

Flexbox Basics

Learn how CSS Flexbox works: flex containers and items, the main and cross axis, and how to build your first flexible row of content step by step.

Before Flexbox, putting a few boxes side by side and lining them up was surprisingly hard. People used floats, inline-block tricks and table displays, and every one of them came with strange side effects. Flexbox (the Flexible Box Layout module) was designed for exactly this job: arranging items in a single row or column, sharing out space, and aligning them without hacks.

Flexbox is one-dimensional. It thinks about one line of items at a time. That makes it perfect for navigation bars, button groups, toolbars, form rows, card footers and the countless small "put these things next to each other" problems in every interface. In this lesson you will create a flex container, learn the two axes that every flex property refers to, and build your first flexible layout.

Making a flex container

Flexbox starts with a single declaration on a parent element:

CSS
.toolbar {
  display: flex;
}

The element with display: flex becomes a flex container. Its direct children become flex items. Grandchildren are not affected; they keep their normal layout unless their own parent is also a flex container.

The moment you add display: flex, three things happen that you can see straight away:

  1. The items sit side by side in a row, even block elements like <div> and <p>.
  2. The items shrink to the width of their content instead of stretching across the page.
  3. The items all become as tall as the tallest one.
Block boxes become a row

The markup in both boxes is identical. Only the parent's display value changed. In the flex version, notice that all three items have the same height even though the last one holds more text. Narrow the preview and the long item wraps its text so everything stays on one line.

flex vs inline-flex. display: flex makes the container itself behave like a block: it starts on a new line and fills the available width. display: inline-flex lays out the children in exactly the same way, but the container sits inline in text, like a button. Choose based on how the container should behave on the outside. The display lesson explains this outer/inner split.

The main axis and the cross axis

Every Flexbox property talks about one of two directions. Learning them is the key to the whole module.

  • The main axis is the direction the items are laid out in. By default it runs horizontally, from left to right in English.
  • The cross axis runs at right angles to the main axis. By default it runs vertically, from top to bottom.

Each axis has a start and an end. Items are placed starting at main-start and flow toward main-end. Cross-axis alignment works between cross-start and cross-end.

The axes are not fixed to "horizontal" and "vertical". If you set flex-direction: column, the main axis turns to run top to bottom and the cross axis becomes horizontal. That is why Flexbox property names avoid the words "left", "right", "top" and "bottom", and why you should think in terms of main and cross:

Property Works along
justify-content the main axis
align-items, align-self the cross axis
flex-grow, flex-shrink, flex-basis the main axis

Axes also follow the writing mode. In a right-to-left language such as Arabic, a default row starts on the right. You learn more about that in Writing Modes and Logical Properties.

Watch the axes turn

In the row, items stretch vertically (the cross axis) to fill the 120px height. In the column, they stretch horizontally instead, because now that is the cross axis. Same rule, different direction.

Spacing items with gap

The cleanest way to put space between flex items is gap, the same property you meet in Grid:

CSS
.toolbar {
  display: flex;
  gap: 12px;
}

gap adds space only between items, never before the first or after the last. You no longer need to add margins to each item and then remove the extra margin from the last one. gap for Flexbox has been supported in every major browser since 2021, so it is safe to use.

Your first flex layout

Let's combine what you have so far into something real: a site header with a logo on the left and links on the right, all centered vertically.

A simple header

Three properties on the header do all the work:

  • display: flex puts the logo and the navigation in a row.
  • justify-content: space-between pushes the first item to the start and the last item to the end of the main axis, with the free space in between.
  • align-items: center centers both items on the cross axis, so the 22px logo and the smaller links line up through their middles.

The <nav> is itself a flex container, so the links inside it are laid out in their own small row. Nesting flex containers like this is completely normal. You will explore every container property in detail in Flex Container Properties.

Items share the space

Flex items are flexible because they can grow into free space and shrink when space runs out. By default:

  • Items do not grow. If there is extra room, it stays empty at the end of the line.
  • Items can shrink down to the width of their longest word (or other unbreakable content) when the container gets too narrow.

You can change this per item. The quickest change is flex: 1, which tells an item to take a share of any free space:

One item fills the rest

The button keeps its natural width and the input stretches to fill everything else. Resize the preview: the input grows and shrinks while the button stays put. Exactly how growing and shrinking are calculated is the subject of Flex Item Properties.

Source order and accessibility

Flexbox can change the visual order of items with flex-direction: row-reverse, column-reverse or the order property. Screen readers and the Tab key still follow the HTML source order, though. If the visual order and the source order disagree, keyboard users see focus jump around unpredictably. Keep the HTML in a sensible reading order and use reordering only for small visual tweaks.

When to choose Flexbox

Flexbox shines when the content should drive the layout: you have a row of things of different sizes and want them to share space and line up. Some good fits:

  • navigation bars and toolbars
  • a label, input and button on one line
  • centering one thing inside another (see Centering Anything)
  • tags or chips that wrap onto several lines
  • pushing a card's footer to the bottom

When you need items in different rows to line up in strict columns, reach for Grid instead. The two work well together: a grid for the page and Flexbox inside each component.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which elements become flex items when you set display: flex on a container?

    Only direct children become flex items. Their own children keep normal layout unless their parent is also a flex container.
  2. 2. With flex-direction: column, which way does the main axis run?

    A column direction turns the main axis to run vertically (top to bottom by default). The cross axis becomes horizontal.
  3. 3. Which property aligns flex items along the cross axis?

    align-items works on the cross axis. justify-content distributes items along the main axis.
  4. 4. By default, what happens to extra space in a flex container that is wider than its items?

    Items do not grow by default, so the free space is left at the main-end of the line. Use flex-grow or justify-content to use it.

Summary

  • display: flex turns an element into a flex container and its direct children into flex items; inline-flex does the same but keeps the container inline.
  • Flexbox is one-dimensional: it lays out items along a single main axis, with alignment on the perpendicular cross axis.
  • flex-direction decides which way the main axis runs, so think "main" and "cross", not "horizontal" and "vertical".
  • gap adds space between items only.
  • Items stretch on the cross axis by default, do not grow on the main axis, and can shrink when space is short.
  • Visual reordering does not change reading or tab order, so keep the HTML in a logical order.

Next: Flex Container Properties, where you take control of direction, wrapping and alignment.