-- online Search!

Flexbox Fundamentals

by kirupa   |   filed under HTML and CSS

TL;DR

Setting display: flex on a container lines its children up along one direction, called the main axis. justify-content spaces the items along that axis, align-items lines them up across it, gap keeps them apart, flex-wrap moves extra items to a new line, and flex-grow hands out the leftover space in shares.

  • flex-direction picks the main axis. In a column, the axes swap, so justify-content moves items up and down while align-items moves them sideways.
  • The default align-items value of stretch is why every item in a flex row ends up the same height.
  • flex: 1 is short for flex: 1 1 0%, so every item with it gets an equal share of the whole container.
  • Flex items won't shrink below their content by default, and min-width: 0 lets an item with a long URL or code block shrink to fit.

A lot of what we build on the web is a bunch of things sitting in a line. A navigation bar is a row of links. A toolbar is a row of buttons. A sidebar is a column of cards. Lining things up like that sounds like it should be the easiest job in CSS, and for a very long time it was weirdly hard. We floated things left, fought with mysterious gaps between inline-block elements, and reached for tables when nobody was looking.

These days, we have a much better tool for this job. In this article, we'll learn how to arrange a row or a column of items, space them out, line them up, and decide which of them gets any leftover space, and we'll get to play with all of it live.

Onwards!

What Is Flexbox?

Let's start with a container and three items inside it. Here's the HTML we'll use for most of this article:

<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

Out of the box, each div takes up the whole width it's given, so our three items stack on top of each other. What we often want instead is for the container to take charge of its children: line them up in a row, decide how much space goes between them, decide how they line up with each other, and hand out any extra room. CSS has a layout mode that does exactly that, and it's called the flexible box layout, or Flexbox for short. We turn it on with one line on the container:

.container {
  display: flex;
}

That one line changes two things. The container becomes a flex container, and each of its direct children becomes a flex item. Our three items now sit side by side in a row, each one only as wide as its content needs. Notice the word direct here. Only the children of the flex container become flex items. Their own children (the container's grandchildren, if you will) keep laying out the way they normally would, unless we make them a flex container too.

What Are the Main Axis and the Cross Axis?

Every flex container has a direction that its items flow in. That direction is called the main axis, and the direction that runs across it is called the cross axis. The flex-direction property picks the main axis, and its default value is row. Here's what our items look like in a row and in a column:

Two flex containers with three items each. With flex-direction: row, the items line up left to right along a horizontal main axis, and the cross axis runs top to bottom. With flex-direction: column, the items stack top to bottom, so the main axis runs down and the cross axis runs across.

With row, the main axis runs from left to right, and the cross axis runs from top to bottom. With column, the axes swap places. The main axis now runs from top to bottom, and the cross axis runs across. This swap is the single most important idea in Flexbox, because almost every other property is defined in terms of these two axes instead of left, right, top, and bottom:

.container {
  display: flex;
  flex-direction: column;
}

There are also row-reverse and column-reverse, which flip the order the items flow in. One more detail to keep in mind is that row follows the direction of the text. On a page in Arabic or Hebrew, where text runs from right to left, a row also runs from right to left. That's by design, and it's the same idea behind CSS Logical Properties.

How Does justify-content Space Things Out?

Our three items rarely fill the whole container. The space left over is called free space, and the justify-content property decides what to do with it along the main axis. Here are the six values we'll use the most, each with the same three items in the same container:

Six flex containers, each holding three items. flex-start packs them at the start, center packs them in the middle, and flex-end packs them at the end. space-between puts the first and last items at the edges with equal space between, space-around gives each item equal space on both sides, and space-evenly makes every gap, including the ones at the edges, the same size.

The first three values pack the items together and move the whole group. flex-start is the default and puts them at the start of the row, center puts them in the middle, and flex-end puts them at the end. The last three values spread the items out instead. With space-between, the first and last items touch the edges, and the free space goes evenly between them. With space-around, each item gets the same amount of space on both of its sides, which is why the gaps at the edges are half the size of the gaps in the middle. With space-evenly, every gap is the same size, including the ones at the edges:

.container {
  display: flex;
  justify-content: space-between;
}

When all we want is a consistent amount of space between our items, there's an even simpler option. The gap property puts space between neighboring items, and only between them, so we don't get any extra space on the outside edges:

.container {
  display: flex;
  gap: 10px;
}

Before gap worked with Flexbox, we would add a margin to every item and then remove it from the last one. Every major browser has supported gap in flex containers since 2021, so we can leave that trick in the past.

How Does align-items Line Things Up?

The align-items property works on the other axis. It decides where each item sits along the cross axis, which in a row means how items of different heights line up with each other:

Four flex rows holding three items of different heights. With stretch, every item grows to fill the container's height. With flex-start, they line up along the top. With center, their middles line up. With flex-end, they line up along the bottom.

The default value is stretch, and it explains something that surprises a lot of people the first time they use Flexbox: items in a row all end up the same height, even when their content is different. Each item stretches to fill the container's cross axis, as long as we haven't given it a fixed height. The other values keep each item at its natural height. flex-start lines the items up along the top, center lines up their middles, and flex-end lines them up along the bottom. There's also baseline, which lines items up by the bottom of their first line of text. It's handy when items have different font sizes.

When one item needs to break from the group, we can set align-self on just that item:

.container {
  display: flex;
  align-items: flex-start;
}

.item:last-child {
  align-self: flex-end;
}

Putting the two alignment properties together gives us one of the most famous tricks in CSS. Setting both justify-content and align-items to center puts an item right in the middle of its container, and our Centering Vertically and Horizontally Using Flexbox tutorial walks through it in detail.

What Happens When Items Don't Fit?

By default, a flex container keeps all of its items on one line, no matter how many there are. When the items want more room than the container has, they shrink to fit. Sometimes that's what we want. Other times, we'd rather the extra items move down to a new line, and that's what flex-wrap controls. Here are five items that each want to be 120px wide, in a container that's only 360px wide:

Two 360px flex containers, each with 5 items that want to be 120px wide. With nowrap, they all stay on one line and shrink to 72px each. With wrap, they keep their 120px width, so three fit on the first line and the other two move to a second line.

With the default value of nowrap, all five items squeeze onto one line, and each one ends up 72px wide. With wrap, each item keeps its 120px width. Three of them fit on the first line, and the other two move to a second line:

.container {
  display: flex;
  flex-wrap: wrap;
}

Once there's more than one line, the align-content property decides how the lines themselves are spaced along the cross axis. It takes the same values as justify-content, and it only does something when the container is taller than its lines need.

How Do Items Share the Leftover Space?

So far, our items have kept their own sizes, and justify-content has moved them around inside the free space. Flexbox can also hand that free space to the items themselves so they grow to fill it. That's the job of three properties we set on the items: flex-basis, flex-grow, and flex-shrink.

The flex-basis property is an item's starting size along the main axis, before any growing or shrinking happens. The flex-grow property says how many shares of the free space an item gets. Its default is 0, which means no share at all. Here's a 600px container with three items that start at 100px each, before and after we give them some flex-grow values:

A 600px flex container with three items that each start at 100px, leaving 300px of free space. With flex-grow values of 1, 2, and 0, the first item gets one share (100px), the second gets two shares (200px), and the third gets none, so they end up 200px, 300px, and 100px wide.

Our three items start at 100px each, so they use 300px of the container, and 300px is left over. The first item has a flex-grow of 1, the second has 2, and the third has 0. That's three shares in total, so each share is worth 100px. The first item gets one share and grows to 200px. The second item gets two shares and grows to 300px. The third item gets nothing and stays at 100px. Here's the CSS behind that picture:

.item {
  flex-basis: 100px;
}

.item:nth-child(1) {
  flex-grow: 1;
}

.item:nth-child(2) {
  flex-grow: 2;
}

The flex-shrink property is the opposite. When the items are too big for the container, it decides how much each one gives up. Its default is 1, which is why our items in the nowrap example squeezed down to 72px each.

Most of the time, we set all three at once with the flex shorthand. The one we'll see everywhere is flex: 1, which is short for flex: 1 1 0%. Because it sets the starting size to 0, every item with flex: 1 gets the same share of the whole container, so they all end up the same size no matter what's inside them:

.item {
  flex: 1;
}

The other detail to keep in mind, and this one trips up everybody at some point, is that a flex item won't shrink below the size of its content by default. A long URL or a wide code block inside an item can push it wider than we asked for and overflow the container. When that happens, setting min-width: 0 on the item lets it shrink past its content, and then the content can wrap or scroll inside it.

Let's Play With Flexbox

By now, we've seen every property that matters for everyday Flexbox. Here's a playground with five items of different sizes, where you can change all of them and see the matching CSS:

Try it

  1. Set justify-content to space-between, then align-items to center
  2. Set flex-direction to column and flex-wrap to wrap
  3. Drag Item 2's flex-grow up

When the direction is a column, notice how justify-content now moves the items up and down while align-items moves them sideways. That's because both properties follow the axes, and in a column, the main axis runs up and down.

A Real Example: A Navigation Bar

Let's put a few of these properties together and build a navigation bar with a logo on one side and some links on the other side. Here's the HTML:

<nav class="navbar">
  <a class="logo" href="/">Waffle Hub</a>
  <a href="/menu">Menu</a>
  <a href="/hours">Hours</a>
  <a href="/visit">Visit</a>
</nav>

And here's the CSS that turns it into a navigation bar:

.navbar {
  display: flex;
  align-items: center;
  gap: 16px;
}

.navbar .logo {
  margin-inline-end: auto;
}

The align-items: center line keeps the logo and the links lined up through their middles, even if the logo uses a bigger font. The gap line puts 16px between every item. The interesting line is the last one. An auto margin in a flex container soaks up all of the free space on that side of the item. Because the logo's margin after it takes all of the free space, the links get pushed all the way to the other end of the bar. (We use margin-inline-end instead of margin-right so the bar also flips correctly for right-to-left languages.)

There's also an order property that changes the order items are displayed in. Use it carefully. It only changes what we see, not the order of the HTML, so keyboard users tabbing through our links and screen reader users still go through them in the original order. When the visual order and the HTML order disagree, people get lost, so it's usually better to change the HTML itself.

When Should We Use Flexbox Instead of Grid?

Flexbox works along one axis at a time. Even when items wrap onto several lines, each line is laid out on its own, so the items on one line don't line up with the items on the next line. That makes Flexbox great for things that are a row or a column at heart, where the content decides how big each piece is: navigation bars, toolbars, button groups, tags, and the inside of a card.

When we need rows and columns to line up at the same time, like a page layout or a gallery of cards, CSS Grid is the better fit. We cover it next in CSS Grid Fundamentals. The two aren't rivals, though. Most real pages use Grid for the big picture and Flexbox for the small pieces inside it.

Frequently Asked Questions

What Is the Difference Between justify-content and align-items?

justify-content spaces items along the main axis, and align-items lines them up along the cross axis. In a row, that means justify-content works left to right and align-items works top to bottom. Switch flex-direction to column, and the two trade directions because the axes swap.

Why Are All My Flex Items the Same Height?

The default value of align-items is stretch, which makes every item fill the container’s cross axis. In a row, that means every item becomes as tall as the container. Setting align-items to flex-start, or giving the items a fixed height, keeps them at their natural height.

What Does flex: 1 Mean?

It’s short for flex: 1 1 0%, which sets flex-grow to 1, flex-shrink to 1, and flex-basis to 0%. Because every item starts from a size of zero, the items with flex: 1 split the whole container into equal shares and end up the same size, no matter what’s inside them.

Why Does My Flex Item Refuse to Shrink?

By default, a flex item won’t shrink below the size of its content, so a long URL or a wide code block can push it wider than the container. Setting min-width: 0 on the item lets it shrink past its content, and the content can then wrap or scroll inside it.

Should I Use Flexbox or CSS Grid?

Use Flexbox for a row or a column of things that should size themselves, like a navigation bar or a group of buttons. Use Grid when things need to line up in rows and columns at the same time, like a page layout or a gallery. Most pages use both, with Grid for the big picture and Flexbox for the pieces inside it.

Conclusion

Flexbox takes a job that used to involve floats, clearfixes, and a fair amount of muttering, and turns it into a handful of properties on a container. We pick a direction with flex-direction, and that gives us a main axis and a cross axis. Then justify-content spaces the items along the main axis, align-items lines them up along the cross axis, gap keeps them apart, and flex-wrap decides what happens when they run out of room.

The part that takes the longest to click is how flex-grow, flex-shrink, and flex-basis share space, so don't worry if it still feels a bit fuzzy. Spend a few minutes with the playground, and the shares will start to feel natural.

Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--