Opens in a new tab

How to Create Columns Using CSS Flexbox?

Website layout on a laptop and phone showing content arranged in equal columns with CSS flexbox

Columns are one of the first layout problems every web developer runs into. A pricing section with three plans, a features block with four boxes, a footer with link groups: they all need content sitting side by side. Years ago we used floats and clearfix hacks for this. Today, flexbox does it with a couple of lines of CSS, and it handles the awkward parts (equal heights, spacing, wrapping on small screens) far better.

In this tutorial we will build columns using CSS flexbox step by step, starting with the simplest version and finishing with a responsive layout you can drop into a real project. We will also clear up one very common misunderstanding that trips up beginners.

The starting HTML

Every example below uses the same simple markup: a wrapper with a few children. The wrapper becomes the flex container, and each child becomes a flex item, which is our column.

<div class="columns">
  <div class="col">Column one</div>
  <div class="col">Column two</div>
  <div class="col">Column three</div>
</div>

Step 1: Turn the wrapper into a flex container

.columns {
  display: flex;
}

That single line already puts the three children next to each other in a row. This is the part that confuses many people, so let us be very clear about it:

Columns come from a row. The default direction of a flex container is flex-direction: row, which places items side by side, and that is exactly what a column layout is. Setting flex-direction: column does the opposite: it stacks the items on top of each other. You will use column later for mobile screens, not for creating columns.

At this point each column is only as wide as its content, so the layout probably looks uneven. Let us fix that.

Step 2: Make the columns equal width

.col {
  flex: 1;
}

flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. In plain words: start every column from zero width, then share all the available space equally. Three columns get a third each, four columns get a quarter each, and you never have to do the maths yourself.

Step 3: Add space between the columns

The old way was to add margins to each column and then remove the margin from the last one. Modern browsers support gap on flex containers, which is much cleaner:

.columns {
  display: flex;
  gap: 24px;
}

gap only adds space between items, never on the outside edges, so your columns stay perfectly aligned with the content above and below.

Equal height columns for free

Try putting a long paragraph in one column and a single line in another. Notice that both columns are the same height. That happens because flex containers use align-items: stretch by default, so every item stretches to match the tallest one in the row.

This is perfect for cards with backgrounds or borders. If you ever want columns to keep their natural height instead, set align-items: flex-start on the container.

Columns with different widths

Real layouts are not always equal. Here are three patterns we use all the time.

One wide column and one narrow column

.main  { flex: 2; }  /* takes two shares */
.aside { flex: 1; }  /* takes one share */

The space is split into three shares: the main column gets two, the side column gets one. That is roughly a 66/33 split.

A fixed column next to a flexible one

.sidebar {
  flex: 0 0 280px;  /* don't grow, don't shrink, stay 280px */
}
.content {
  flex: 1;          /* take everything that is left */
}

This is the classic sidebar layout. The three values in flex: 0 0 280px mean grow 0, shrink 0, basis 280px, so the sidebar always stays exactly 280 pixels wide.

Exact percentage columns

If you need precise widths like 25% and 75%, remember to account for the gap, or the columns will overflow the container:

.columns { display: flex; gap: 24px; }
.quarter      { flex: 0 0 calc(25% - 12px); }
.three-quarter { flex: 0 0 calc(75% - 12px); }

With one gap of 24px shared by two columns, each column gives up 12px. In most cases, though, using flex: 1 and flex: 3 gives the same result without any calc().

Making columns using CSS flexbox responsive

Three columns side by side look great on a laptop and terrible on a phone. There are two good ways to handle this.

Option 1: Stack the columns on small screens

.columns {
  display: flex;
  gap: 24px;
}
.col {
  flex: 1;
}

@media (max-width: 768px) {
  .columns {
    flex-direction: column;  /* now we stack them */
  }
}

Here is where flex-direction: column belongs. Below 768px, the columns stack into a single list, and gap keeps working as vertical spacing.

Option 2: Let the columns wrap by themselves

This is our favourite, because it needs no media query at all:

.columns {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}
.col {
  flex: 1 1 250px;  /* grow, shrink, but aim for at least 250px */
}

Each column tries to be at least 250px wide. When the container is too narrow to fit them all on one line, the extra columns wrap onto the next line and stretch to fill it. On a wide screen you might see four columns, on a tablet two, and on a phone one, all from the same few lines of CSS.

Reordering columns

Sometimes the order you want on mobile is different from the desktop order. Maybe the image should come after the text on phones. The order property moves items visually without touching the HTML:

@media (max-width: 768px) {
  .columns { flex-direction: column; }
  .col-image { order: 2; }
  .col-text  { order: 1; }
}

Use this carefully. Screen readers and keyboard users still follow the HTML order, so do not use order in a way that makes the content confusing to read.

Common mistakes and how to fix them

Problem Why it happens Fix
Using flex-direction: column to make columns It stacks items vertically instead Leave the direction as row
A long URL or code block pushes a column wider than it should be Flex items have min-width: auto by default and refuse to shrink below their content Add min-width: 0 to the column
Percentage columns overflow the container The gap is added on top of the percentages Subtract the gap with calc(), or use flex values instead
Images overflow their column Images keep their natural size Add img { max-width: 100%; height: auto; }
Cards are not the same height align-items was changed, or the card is inside a wrapper Keep the default stretch and give the card height: 100%

The min-width: 0 fix deserves extra attention. It is the single most common flexbox bug we see when reviewing client sites, and it can take hours to find if you do not know about it.

A complete example of columns using CSS flexbox

<section class="features">
  <div class="feature"><h3>Fast</h3><p>Pages load in under a second.</p></div>
  <div class="feature"><h3>Secure</h3><p>Daily backups and updates.</p></div>
  <div class="feature"><h3>Simple</h3><p>Edit everything yourself.</p></div>
</section>

.features {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}
.feature {
  flex: 1 1 240px;
  min-width: 0;
  padding: 24px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
}

This gives you equal height cards, even spacing, and automatic wrapping on smaller screens with no media queries.

Should you use flexbox or CSS grid for columns?

Both work. A simple rule of thumb:

  • Flexbox is best when the content decides the layout: a row of buttons, a navigation bar, cards that wrap naturally, or a sidebar next to content.
  • Grid is best when you want a strict structure in two directions, such as a photo gallery where every row and column must line up exactly.

For most column layouts on business websites, flexbox is all you need. If you are curious how flexbox handles a full page layout with a sidebar on each side, read our follow up tutorial on building a two sidebar layout with flexbox.

Quick summary: columns using CSS flexbox

  • display: flex on the parent puts children side by side.
  • flex: 1 on the children makes them equal width.
  • gap adds clean spacing between columns.
  • flex-wrap: wrap plus a flex-basis makes them responsive without media queries.
  • flex-direction: column is for stacking, which you usually only want on mobile.
  • Add min-width: 0 when long content breaks your columns.

Open your code editor and try the complete example above. Change the numbers, resize the browser, and watch what happens. Ten minutes of playing with it will teach you more than any amount of reading. For every property in detail, the MDN guide to flexbox is the best free reference.