CSS Multi-column Layout Module Level 1

W3C Candidate Recommendation Snapshot,

This version:
https://www.w3.org/TR/2021/CR-css-multicol-1-20211012/
Latest published version:
https://www.w3.org/TR/css-multicol-1/
Editor's Draft:
https://drafts.csswg.org/css-multicol/
Previous Versions:
Implementation Report:
https://test.csswg.org/harness/results/css-multicol-1_dev/grouped/
Test Suite:
http://test.csswg.org/suites/css-multicol-1_dev/nightly-unstable/
Issue Tracking:
CSSWG Issues Repository
Disposition of Comments
Editors:
Florian Rivoal (On behalf of Bloomberg)
(Google)
Former Editor:
(Opera Software)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This specification describes multi-column layouts in CSS, a style sheet language for the web. Using functionality described in the specification, content can be flowed into multiple columns with a gap and a rule between them.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

This document was published by the CSS Working Group as a Candidate Recommendation Snapshot. Publication as a Candidate Recommendation does not imply endorsement by the W3C Membership. A Candidate Recommendation Snapshot has received wide review and is intended to gather implementation experience. This document is intended to become a W3C Recommendation; it will remain a Candidate Recommendation at least until to gather additional feedback.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-multicol” in the title, like this: “[css-multicol] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document is governed by the 15 September 2020 W3C Process Document.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

1. Introduction

(This section is not normative.)

This module describes multi-column layout in CSS. By using functionality described in this document, style sheets can declare that the content of an element is to be laid out in multiple columns.

Other layout methods in CSS, when applied to a parent element, change the display properties of the direct children. For example if a three column grid layout is created, the direct children of the grid container become grid items and are placed into the column tracks, one element per cell with additional rows created as needed.

The child elements of a multi-column container however continue in normal flow, that flow is arranged into a number of columns. These columns have a flexible inline size, and therefore respond to available space by changing the size or number of columns displayed.

Multi-column layouts are easy to describe in CSS. Here is a simple example:

body { column-width: 12em }

In this example, the body element is set to have columns at least 12em wide. The exact number of columns will depend on the available space.

The number of columns can also be set explicitly in the style sheet:

body { column-count: 2 }

In this case, the number of columns is fixed and the column widths will vary depending on the available width.

The shorthand columns property can be used to set either, or both, properties in one declaration.

In these examples, the number of columns, the width of columns, and both the number and width are set, respectively:
body { columns: 2 }
body { columns: 12em }
body { columns: 2 12em }

Another group of properties introduced in this module describe gaps and rules between columns.

body {
  column-gap: 1em;
  column-rule: thin solid black;
}

The first declaration in the example above sets the gap between two adjacent columns to be 1em. Column gaps are similar to padding areas. In the middle of the gap there will be a rule which is described by the column-rule property.

The values of the column-rule property are similar to those of the CSS border properties. Like border, column-rule is a shorthand property.

In this example, the shorthand column-rule declaration from the above example has been expanded:
body {
  column-gap: 1em;
  column-rule-width: thin;
  column-rule-style: solid;
  column-rule-color: black;
}

The column-fill and column-span properties give style sheets a wider range of visual expressions in multi-column layouts.

In this example, columns are set to be balanced, i.e., to have approximately the same length. Also, h2 elements are set to span across all columns.
div { column-fill: balance }
h2 { column-span: all }

This specification introduces ten new properties, all of which are used in the examples above.

If all column properties have their initial value, the layout of an element will be identical to a multi-column layout with only one column.

Column gaps (diagonal hatching) and column rules are shown in this sample rendition of a multi-column container with padding (cross hatching). The hatched areas are present for illustrational purposes only. In actual implementations these areas will be determined by the background, the second image shows a rendering of a multi-column container with column-rules.
a diagram showing the various parts of multi-column layout key to the conventions used to display invisible parts of diagram
A multi-column layout with the non-visible column-span and padding inside the multicol container highlighted.
a diagram showing the various parts of multi-column layout
The same layout as in the first image, as it would be displayed by an implementation.

1.1. Value Definitions

This specification follows the CSS property definition conventions from [CSS21] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.

2. The Multi-Column Model

An element whose column-width or column-count property is not auto establishes a multi-column container (or multicol container for short), and therefore acts as a container for multi-column layout.

In the traditional CSS box model, the content of an element is flowed into the content box of the corresponding element. Multi-column layout introduces a fragmentation context formed of anonymous fragmentation containers called column boxes (or columns for short). These column boxes establish an independent block formatting context into which the multi-column container’s content flows, and form the containing block for its non-positioned children.

In this example, the width of the image is set with these rules:
img {
  display: block;
  width: 100%;
}

Given that the column box creates a new block formatting context, the width is calculated relative to the column box. Therefore the image will not overflow the column box:

an image contained inside a column box
The image is constrained by the column box that it is displayed in.
Given that the column box creates a new block formatting context, a top margin set on the first child element of a multicol container will not collapse with the margins of the multicol container.
The first paragraph has a 'margin-top' of <fake-maybe-placeholder bs-autolink-syntax='''1em'''>1em</fake-maybe-placeholder>, which appears before the text.
The margin above the first paragraph has not collapsed, leaving a 1em margin above the first line in the multicol container.

Floats that appear inside multi-column layouts are positioned with regard to the column box where the float appears.

In this example, this CSS fragment describes the presentation of the image:
img {
  display: block;
  float: right;
}

In the HTML, the image appears after the sentence ending, "the leg of a chicken".

an image floated and contained inside a column box
The image is floated inside the column box it appears in.

Content overflowing a column box in the block axis fragments and continues in the next column box.

Note: Column boxes, which are anonymous boxes, do not become the containing block for absolutely positioned boxes. The position property, which establishes a containing block for such boxes, applies to the multicol container, it being the principal box.

In this example, the multi-column container has position: relative thus becoming the containing block. The image is a direct child of the multi-column container and has position: absolute. It takes positioning from the multi-column container and not from the column box.
.container {
  position: relative;
  column-count: 3;
}
img {
  position: absolute;
  top: 20px;
  left: 40px;
}
The absolutely positioned image is positioned by reference to the [=multi-column container=] not the [=column box=].
The figure demonstrates that the absolutely positioned image is positioned by reference to the multicol container and not the column box.

Out-of-flow descendants of a multi-column container do affect column balancing, and the block-size of the multi-column container.

The column boxes are ordered in the inline base direction of the multicol container and arranged into multicol lines. The column width is the length of the column box in the inline direction. The column height is the length of the column box in the block direction. All column boxes in a line have the same column width, and all column boxes in a line have the same column height.

Note: In text set using a vertical writing mode, the block direction runs horizontally. In a vertical writing mode columns are laid out horizontally, and the direction of the flow of blocks may be right to left, or left to right. The column-width property therefore refers to the inline size of the column, and not the physical horizontal width.

The first image shows horizontal text with a LTR inline direction. The second shows vertical text with blocks flowing right to left. The third shows vertical text with blocks flowing left to right.
A diagram showing the different ways columns may be arranged due to writing mode.
From left to right: horizontal-tb, vertical-rl, vertical-lr.

Within each multicol line in the multi-column container, adjacent column boxes are separated by a column gap, which may contain a column rule. All column gaps in the same multi-column container are equal. All column rules in the same multi-column container are also equal, if they appear; column rules only appear between columns that both have content.

In the simplest case a multicol container will contain only one line of columns, and the height of each column will be equivalent to the used height of the multi-column container’s content box. However, fragmentation or spanners can split the content of the multi-column container into multiple multicol lines.

If the multi-column container is paginated, the height of each column is constrained by the page and the content continues in a new line of column boxes on the next page; a column box never splits across pages.

The same effect occurs when a spanning element divides the multi-column container: the columns before the spanning element are balanced and shortened to fit their content. Content after the spanning element then flows into a new, subsequent line of column boxes.

a diagram showing a spanning element causing the shortened columns above the element with text continuing in new columns below
A demonstration of how the spanning element divides the multicol container.

A multi-column container therefore is a regular block container that establishes a new independent formatting context whose contents consist of a series of multicol lines and multicol spanners. Each multi-column line acts as a block-level box that establishes a multi-column formatting context for its column boxes; and each spanner acts as a block-level box that establishes an independent formatting context with its type depending on its display value as usual.

Nested multi-column containers are allowed, but there may be implementation-specific limits.

Note: It is not possible to set properties/values on column boxes. For example, the background of a certain column box cannot be set and a column box has no concept of padding, margin or borders. Future specifications may add additional functionality. For example, columns of different widths and different backgrounds may be supported.

Note: Multicol containers with column heights larger than the viewport may pose accessibility issues. See Accessibility Considerations for more details.

3. The Number and Width of Columns

Finding the number and width of columns is fundamental when laying out multi-column content. These properties are used to set the number and width of columns:

A third property, columns, is a shorthand property which sets both column-width and column-count.

Other factors, such as explicit column breaks, content, and height constraints, may influence the actual number and width of columns.

3.1. The Inline Size of Columns: the column-width property

Name: column-width
Value: auto | <length [0,∞]>
Initial: auto
Applies to: block containers except table wrapper boxes
Inherited: no
Percentages: N/A
Computed value: the keyword auto or an absolute length
Canonical order: per grammar
Animation type: by computed value type

This property describes the width of columns in multicol containers.

auto
means that the column width will be determined by other properties (e.g., column-count, if it has a non-auto value).
<length [0,∞]>
describes the optimal column width. The actual column width may be wider (to fill the available space), or narrower (only if the available space is smaller than the specified column width). Negative values are not allowed. Used values will be clamped to a minimum of 1px.
For example, consider this style sheet:
div {
  width: 100px;
  column-width: 45px;
  column-gap: 0;
  column-rule: none;
}

There is room for two 45px wide columns inside the 100px wide element. In order to fill the available space the actual column width will be increased to 50px.

Also, consider this style sheet:
div {
  width: 40px;
  column-width: 45px;
  column-gap: 0;
  column-rule: none;
}

The available space is smaller than the specified column width and the actual column width will therefore be decreased.

To ensure that column-width can be used with vertical text, column width means the length of the line boxes inside the columns.

Note: The reason for making column-width somewhat flexible is to achieve scalable designs that can fit many screen sizes. To set an exact column width, the column gap and the width of the multicol container (assuming horizontal text) must also be specified.

3.2. The Number of Columns: the column-count property

Name: column-count
Value: auto | <integer [1,∞]>
Initial: auto
Applies to: block containers except table wrapper boxes
Inherited: no
Percentages: N/A
Computed value: specified value
Canonical order: per grammar
Animatable: by computed value

This property describes the number of columns of a multicol container.

auto
means that the number of columns will be determined by other properties (e.g., column-width, if it has a non-auto value).
<integer [1,∞]>
describes the optimal number of columns into which the content of the element will be flowed. Values must be greater than 0. If both column-width and column-count have non-auto values, the integer value describes the maximum number of columns.
Example:
body { column-count: 3 }

3.3. The column-width and column-count Shorthand: The columns Property

Name: columns
Value: <'column-width'> || <'column-count'>
Initial: see individual properties
Applies to: see individual properties
Inherited: see individual properties
Percentages: see individual properties
Computed value: see individual properties
Animation type: see individual properties
Canonical order: per grammar

This is a shorthand property for setting column-width and column-count. Omitted values are set to their initial values.

Here are some valid declarations using the columns property:
columns: 12em;      /* column-width: 12em; column-count: auto */
columns: auto 12em; /* column-width: 12em; column-count: auto */
columns: 2;         /* column-width: auto; column-count: 2 */
columns: 2 auto;    /* column-width: auto; column-count: 2 */
columns: auto;      /* column-width: auto; column-count: auto */
columns: auto auto; /* column-width: auto; column-count: auto */

3.4. The Pseudo-algorithm

The pseudo-algorithm below determines the used values for column-count (N) and column-width (W). There is one other variable in the pseudo-algorithm: U is the used width of the multi-column container.

Note: The used width U of the multi-column container can depend on the element’s contents, in which case it also depends on the computed values of the column-count and column-width properties. This specification does not define how U is calculated. Another module (probably the Basic Box Model [CSS3BOX] or the Intrinsic & Extrinsic Sizing Module [CSS3-SIZING]) is expected to define this.

The floor(X) function returns the largest integer Y ≤ X.

(01)  if ((column-width = auto) and (column-count = auto)) then
(02)      exit; /* not a multicol container */
(03)  if column-width = auto then
(04)      N := column-count
(05)  else if column-count = auto then
(06)      N := max(1,
(07)        floor((U + column-gap)/(column-width + column-gap)))
(08)  else
(09)      N := min(column-count, max(1,
(10)        floor((U + column-gap)/(column-width + column-gap))))

And:

(11)  W := max(0, ((U + column-gap)/N - column-gap))

For the purpose of finding the number of auto-repeated columns, the UA must floor the column size to a UA-specified value to avoid division by zero. It is suggested that this floor be 1px or less.

In fragmented contexts such as in paged media, user agents may perform this calculation on a per-fragment basis.

The used value for column-count is calculated without regard for explicit column breaks or constrained column heights, while the actual value takes these into consideration.

In this example, the actual column-count is higher than the used column-count due to explicit column breaks:
div {
  width: 40em;
  columns: 20em;
  column-gap: 0;
}

p {
  break-after: column;
}
<div>
  <p>one
  <p>two
  <p>three
</div>
Two columns drawn inside the container, one outside
The computed column-count is auto, the used column-count is 2 and the actual column-count is 3.
The actual column-count may be lower than the used column-count. Consider this example:
div {
  width: 80em;
  height: 10em;
  columns: 20em;
  column-gap: 0;
  column-fill: auto;
}
<div>foo</div>

The computed column-count is auto, the used column-count is 4, and the actual column-count is 1.

3.5. Stacking Context

All column boxes in a multi-column container are in the same stacking context and the drawing order of their contents is as specified in CSS 2.1. Column boxes do not establish new stacking contexts.

4. Column Gaps and Rules

Column gaps and rules are placed between columns in the same multicol container. The length of the column gaps and column rules is equal to the column height. Column gaps take up space. That is, column gaps will push apart content in adjacent columns (within the same multicol container).

A column rule is drawn in the middle of the column gap with the endpoints at opposing content edges of the multicol container. Column rules do not take up space. That is, the presence or thickness of a column rule will not alter the placement of anything else. If a column rule is wider than its gap, the adjacent column boxes will overlap the rule, and the rule may possibly extend outside the box of the multicol container. Column rules are painted just above the border of the multicol container. For scrollable multicol containers, note that while the border and background of the multicol container obviously aren’t scrolled, the rules need to scroll along with the columns. Column rules are only drawn between two columns that both have content.

4.1. Gutters Between Columns: the column-gap property

The column-gap property is defined in [CSS3-ALIGN].

In a multi-column formatting context the used value of normal for the column-gap property is 1em. This ensures columns are readable when the initial values are used. If there is a column rule between columns, it will appear in the middle of the gap.

4.2. The Color of Column Rules: the column-rule-color property

Name: column-rule-color
Value: <color>
Initial: currentcolor
Applies to: multicol containers
Inherited: no
Percentages: N/A
Computed value: computed color
Canonical order: per grammar
Animation type: by computed value type
<color>
Specifies the color of the column rule.

4.3. The Style Of Column Rules: the column-rule-style property

Name: column-rule-style
Value: <line-style>
Initial: none
Applies to: multicol containers
Inherited: no
Percentages: N/A
Computed value: specified keyword
Canonical order: per grammar
Animation type: discrete

The column-rule-style property sets the style of the rule between columns of an element. The <line-style> values are interpreted as in the collapsing border model.

The none value forces the computed value of column-rule-width to be 0.

4.4. The Width Of Column Rules: the column-rule-width property

Name: column-rule-width
Value: <line-width>
Initial: medium
Applies to: multicol containers
Inherited: no
Percentages: N/A
Computed value: absolute length; 0 if the column rule style is none or hidden
Canonical order: per grammar
Animation type: by computed value type

This property sets the width of the rule between columns. Negative values are not allowed.

4.5. Column Rule Shorthand: the column-rule property

Name: column-rule
Value: <'column-rule-width'> || <'column-rule-style'> || <'column-rule-color'>
Initial: see individual properties
Applies to: see individual properties
Inherited: see individual properties
Percentages: see individual properties
Computed value: see individual properties
Animation type: see individual properties
Canonical order: per grammar

This property is a shorthand for setting column-rule-width, column-rule-style, and column-rule-color at the same place in the style sheet. Omitted values are set to their initial values.

In this example, the column rule and the column gap have the same width. Therefore, they will occupy exactly the same space.
body {
  column-gap: 35px;
  column-rule-width: 35px;
  column-rule-style: solid;
  column-rule-color: black;
}
The rule completely covers any gap.
The column rule and column gap occupy the same space.

5. Column Breaks

When content is laid out in multiple columns, the user agent must determine where column breaks are placed. The problem of breaking content into columns is similar to breaking content into pages, which is described in CSS 2.1, section 13.3.3 [CSS21].

Three new properties are introduced to allow column breaks to be described in the same properties as page breaks: break-before, break-after, and