The CSS Multi-column Layout standard is a CSS module that adds support for multi-column layouts to CSS. This module makes it easier and more reliable than ever to produce columnar layouts without having to hand-code complicated and fragile structures. Support is included for establishing the number of columns in a layout, as well as how content should flow from column to column, gap sizes, column dividing lines (known as column rules), and so forth.
Multi-column layout basics
The three key properties used to produce the layout of columns in CSS are column-count
, column-width
, and column-gap
. Other properties provide ways to fine-tune details of how things are organized and rendered within the layout structure built using those properties, but the job of figuring out what goes where is generally handled by these four.
Suggesting the number of columns and their widths
The first two properties, column-count
and column-width
, are both the most important and the most potentially misleading. It's important to understand that:
- The
column-count
property specifies a maximum number of columns to use to render the text. The browser will consider the amount of horizontal space available, the value ofcolumn-width
, and the value ofcolumn-gap
, then draw the largest number of columns across that it can fit into the available space. - The
column-width
property specifies a minimum column width, given alength
value. Each column the browser creates will be at least this wide, but may be wider. After determining the number of columns that it will draw, any remaining horizontal space may be divided up by the browser among the columns and added to their widths.
In a way, then, they're more like recommendations than hard and fast rules. And this makes sense once you consider the logic of it; this allows multi-column layouts to be responsive and adaptive to the screen width, while also supporting the overall layout of the page and the intent of the author. As the width of the containing space (or the screen) shrinks, the browser will reduce the number of columns it creates, adjusting the remaining columns' widths to ensure that the overall width is as expected.
The other thing you can control is the distance between the columns, known as the column gap. The column gap can be controlled using the column-gap
property. By convention, the default gap is 1em
, but this may or may not be the case in any given browser, so if it matters to you, explicitly set it. The column gap, unlike the column count and width, doesn't get adjusted by the browser as it tries to fit the content into the available space.
Take a look at the example below, in which you can use the range control at the top to adjust the width of the columns' containing element.
HTML
<p>
Use the slider to change the container width:
</p>
<div class="controls">
<input type="range" id="widthSlider" min="350" max="625">
<span id="widthDisplay"></span>
</div>
<div class="columnbox">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent dignissim feugiat tellus, vitae vehicula nunc
fermentum at. Nulla ut leo tincidunt turpis tincidunt
facilisis vel sit amet nisl. Duis odio mauris, ornare vitae
est eget, molestie aliquet quam. Quisque sed lacus sodales
nulla dignissim semper. Mauris finibus erat ut molestie
sagittis. Maecenas eget augue dapibus eros mollis
consectetur quis a mauris. Ut quis ipsum egestas, molestie
augue id, lobortis neque. Sed malesuada id justo nec
accumsan. Curabitur non tempus ligula, ut congue libero.
Morbi mattis, neque at feugiat rutrum, ipsum massa iaculis
risus, ut rutrum justo purus et urna. Aenean semper ornare
enim, maximus tincidunt ante tincidunt sed. Fusce imperdiet
id tellus molestie laoreet.
</p>
<p>
Sed non fringilla turpis, ac fermentum neque. Nunc tortor
sapien, convallis in odio dictum, maximus hendrerit metus.
Phasellus vitae molestie lacus, sed imperdiet enim. Praesent
tempus ligula eget orci interdum, et tempus nunc rutrum.
Cras vel arcu non ipsum sodales faucibus vestibulum eu mi.
Fusce sed laoreet turpis, eu rutrum leo. Vestibulum sit amet
porta mauris. Curabitur in nisi a neque ultricies iaculis
sit amet vel felis. Suspendisse lobortis iaculis gravida. Ut
bibendum vestibulum lacus eget dignissim.
</p>
</div>
JavaScript
let widthSlider = document.getElementById("widthSlider");
let widthDisplay = document.getElementById("widthDisplay");
let columnBox = document.querySelector(".columnbox");
widthSlider.value = columnBox.offsetWidth;
widthDisplay.innerText = widthSlider.value;
widthSlider.addEventListener("input", function(event) {
columnBox.style.width = widthSlider.value.toString() + "px";
widthDisplay.innerText = widthSlider.value;
}, false);
CSS
body {
font: 16px "Open Sans", "Arial", "sans-serif";
}
.columnbox {
width: 400px;
column-count: 3;
column-width: 12em;
border: 1px dotted black;
}
.columnbox p:first-child {
margin-top: 0;
}
.controls {
font: 16px "Open Sans", "Arial", sans-serif;
}
Result
Text wrapping in multi-column layouts
The column-fill
property also affects layout. By default, the browser will create as many appropriately-sized columns as it can, then balance the contents across them so that each column is approximately the same length. If, however, you prefer to have the browser fill the first column to its maximum height before moving on to the next column, you can set column-fill
to auto
(instead of the default, balance
).
If there is no constraint on the column height, however, there will only be one column created, as it will never reach its maximum height to trigger wrapping to the second column, so be sure to either place your columns inside a container which constrains their height, either by using height
or max-height
.
While the CSS specification defines the break-before
, break-after
, and break-inside
properties to help control wrapping of elements across region, column, or page boundaries, these properties are generally not implemented widely enough to use in real-world code at this time.
Column appearance
You can specify that a column rule—a dividing line drawn in the center of each column gap—be drawn between each column in the rendered output of your layout by using the column-rule-style
, column-rule-width
, and column-rule-color
properties, or the shorthand property column-rule
.
Reference
CSS properties
Guides
- Using CSS multi-column layouts
- Step-by-step tutorial about how to build layouts using several columns.
Specifications
Specification | Status | Comment |
---|---|---|
CSS Multi-column Layout Module | Working Draft | Initial definition |
Browser compatibility
See also
Other CSS layout technologies include:
- CSS Flexible box layout (CSS flexbox)
- CSS Grid layout