Skip to content
Tutorials Center
My learning

Topics

Lesson 64 of 139 ยท Layout Fundamentals

Multi-column Layout

Flow content into newspaper-style columns with CSS columns, add gaps and rules, keep items from splitting with break-inside, and span headings across columns.

Newspapers split long text into narrow columns because short lines are easier to read. CSS can do the same with multi-column layout. You tell a container how many columns you want, or how wide they should be, and the browser pours the content into them like water into glasses: it fills the first column, then continues in the next, and balances the heights for you.

Multi-column layout is different from flexbox and grid. Those place separate boxes into rows and columns. Multi-column takes one stream of content and breaks it across columns. That makes it great for lists of links, tag clouds, footnotes, glossaries and "masonry-style" card collections, and occasionally for actual text.

Creating columns

Two properties start a multi-column container:

  • column-count sets the number of columns.
  • column-width sets the ideal minimum width of each column. The browser fits as many columns of at least that width as the container allows, then stretches them to fill the space.

The columns shorthand accepts either or both:

CSS
.list { columns: 3; }          /* exactly three columns */
.list { columns: 14rem; }      /* as many 14rem+ columns as fit */
.list { columns: 14rem 4; }    /* as many as fit, but never more than 4 */

column-width is the more flexible choice, because it is responsive without media queries: a wide screen gets four columns, a phone gets one. With column-count alone, three columns on a phone can end up uncomfortably narrow.

Responsive columns with column-width

The list reads down the first column and then continues at the top of the next. That is the key difference from a grid, which fills across each row first. For an alphabetical list, reading down is exactly what you want.

Gaps and rules

Between columns you can set the space and draw a line:

  • column-gap sets the space between columns. The default is normal, which is 1em in multi-column layout. It is the same gap family you will use in flexbox and grid, so gap works too.
  • column-rule draws a vertical line in the middle of each gap. It takes the same values as a border: width, style and color, with the longhands column-rule-width, column-rule-style and column-rule-color.
CSS
.article {
  columns: 18rem;
  column-gap: 3rem;
  column-rule: 1px solid #cbd5e1;
}

The rule takes up no space. If it is wider than the gap, it simply overlaps the columns, so keep the gap generous.

Controlling breaks

Because the content flows from column to column, an element can be split between the bottom of one column and the top of the next. For text paragraphs that is fine. For a card, a figure or a heading separated from its first paragraph, it looks broken.

The break properties fix that:

  • break-inside: avoid on an element asks the browser not to split it.
  • break-after: avoid on a heading asks the browser not to put a column break right after it, so it stays with the text below. (Browser support for break-after: avoid in columns is patchy, so do not rely on it alone.)
  • break-before: column forces an element to start a new column, and break-after: column forces the next one to. These are mainly useful in print and in Chromium and Safari today.

break-inside: avoid is the one you will use constantly. Here it keeps cards whole, which gives you a simple masonry layout: cards of different heights packed into columns without gaps.

Masonry-style cards with break-inside

Remove break-inside: avoid and some cards get cut in half, with the top part at the bottom of one column and the rest at the top of the next.

Masonry order. With columns, the cards read down each column, so the first column holds items 1, 2 and 3. If the order matters, such as newest items first, people scanning left to right may get confused. CSS is gaining a dedicated masonry layout in grid that keeps the row order, but it is still experimental, so columns remain the practical choice for now.

Spanning across columns

column-span: all makes an element break out of the columns and stretch across all of them. Content before it is balanced into columns above, and content after it starts new columns below. It is ideal for headings and pull quotes in a long article. The only values are none (the default) and all; you cannot span just two columns.

A heading that spans every column

The heading cuts across the whole article. The text above it is balanced into columns, then a fresh set of columns begins underneath.

Column fill and height

By default, column-fill: balance makes all columns roughly the same height. If the container has a fixed height, you can set column-fill: auto to fill each column completely before moving to the next. When the content does not fit in a fixed-height container, the extra columns overflow sideways, past the container's right edge. That is rarely what you want on screen, so avoid fixed heights on column containers unless you deliberately want horizontally scrolling pages.

When to use columns (and when not)

Good fits:

  • Long lists of short items: links in a footer, tags, a glossary, an index.
  • Collections of cards with varying heights.
  • Short passages of text on wide screens.

Poor fits:

  • Long articles on screen. If a column is taller than the viewport, readers must scroll down to the bottom of column one, then back up to the top of column two. Keep column text short, or use columns only when the whole block fits on screen.
  • Arranging separate components in rows, or anything that needs alignment across columns. Use flexbox or grid.

Columns also behave well in print styles, where pages have fixed heights and the reader cannot scroll.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does columns: 12rem do?

    A width value sets the ideal minimum column width. The browser fits as many as it can and stretches them to fill the container.
  2. 2. In which order does content flow in a multi-column container?

    Multi-column layout breaks one stream of content into columns, so it reads down each column in turn.
  3. 3. Which declaration stops a card from being split between two columns?

    break-inside: avoid asks the browser not to break inside that element.
  4. 4. What does column-span: all do on a heading inside a column container?

    The heading breaks out of the columns. Content before it is balanced above, and content after it starts new columns below.

Summary

  • column-count, column-width and the columns shorthand turn a container into a multi-column layout; widths make it responsive without media queries.
  • Content flows down one column and continues in the next.
  • column-gap sets the space between columns and column-rule draws a line in it.
  • break-inside: avoid keeps items whole, which also gives a simple masonry layout.
  • column-span: all lets headings stretch across every column.
  • Columns suit lists, cards and short text; use flexbox and grid to arrange separate components.

Next: Flexbox Basics โ€” the one-dimensional layout model that handles rows, columns, alignment and spacing.