A newspaper article is never printed as one long line from the left edge of the page to the right edge. The text is split into several narrow columns side by side, because short lines are easier to read.
In CSS, text in columns like a newspaper is called multi-column. You only say how many columns you want. The browser then cuts the text and continues it from one column into the next.
Text in several columns
This rule splits the text inside .text into three columns:
.text {
column-count: 3;
column-gap: 20px;
column-rule: 2px solid #d1d5db;
}
column-count: 3splits the content of.textinto three columns. The browser decides where the text is cut.column-gap: 20pxputs 20px of space between the columns.column-rule: 2px solid #d1d5dbdraws a grey line in the middle of that space. You write it like aborder: width, style, then colour. You can leave the line out.
The result: the text fills the first column from top to bottom, then continues at the top of the second column, then the third. So the order is down first, then right. For example, with four paragraphs, the second paragraph usually appears below the first, not next to it.
A heading across every column
In this example, .article is the element with columns, and there is an h2 heading inside it:
.article h2 {
column-span: all;
}
column-span: all stretches the h2 heading across all the columns. The text above and below the heading stays in columns.
Keep a card in one piece
The browser cuts the text at the end of a column, the way a book cuts text at the end of a page: when one column is full, the rest moves to the next column. So one paragraph or card can be split: half at the bottom of the left column, the other half at the top of the right column. break-inside: avoid on that element keeps it whole in one column:
.text p {
break-inside: avoid;
}
⚠️ When multi-column causes trouble
- Using it for a page layout. A sidebar and the content are two separate areas, not one flow of text. Use Grid or Flexbox for that.
- Cards that must be in order from left to right. In multi-column, the second card usually appears below the first. If the order has to go from left to right, use Grid.
- A fixed column count on phones. Three columns on a narrow screen are very narrow. Use
column-width(see below), or change the number of columns in a media query (a CSS rule for certain screen widths).
💡 Let the browser calculate the column count.
column-width: 250pxmeans: each column is about 250px wide or more. The browser fits as many columns as there is room for: one column on a phone, more on a wide screen.
TL;DR: Multi-column splits text into several columns, like a newspaper. column-count sets the number of columns, column-gap the space between them, and column-rule a line in the middle of that space. The text flows down first, then moves to the column on its right. column-span: all makes a heading cross every column, and break-inside: avoid keeps a card or paragraph from being split between two columns. Use multi-column for long text, not for page layouts.
⚡ Text in three columns
Write:
.text {
column-count: 3;
column-gap: 20px;
column-rule: 2px solid #d1d5db;
}
Guess first: the text has four paragraphs and is split into three columns. Where does the second paragraph appear?
Usually below the first one, not next to it. The text fills one column from top to bottom first, then moves to the column on its right.
Meaning: column-count: 3 splits the text inside .text into three columns. column-gap is the space between the columns, and column-rule is a line in the middle of that space, written like a border (width, style, colour).
⚡ Keep it in one piece
Write:
.text p {
break-inside: avoid;
}
Meaning: The browser cuts the text when a column is full, the way a book cuts text at the end of a page. So one paragraph or card can be split: half at the bottom of the left column, half at the top of the right column. break-inside: avoid keeps it whole in one column.
⚡ A heading across every column
Write:
.article h2 {
column-span: all;
}
Meaning: .article is the element with columns, and h2 is a heading inside it. column-span: all stretches that heading across all the columns. The text above and below the heading stays in columns.