W3C

CSS3 module: Multi-column layout

W3C Working Draft 6 June 2007

This version:
http://www.w3.org/TR/2007/WD-css3-multicol-20070606
Latest version:
http://www.w3.org/TR/css3-multicol
Previous version:
http://www.w3.org/TR/2005/WD-css3-multicol-20051215
Editors:
Håkon Wium Lie, Opera Software, howcome@opera.com

Abstract

This module describes multi-column layout in CSS. It builds on the CSS3 Box model module and adds functionality to flow the content of an element into multiple columns.

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 http://www.w3.org/TR/.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-multicol” in the subject, preferably like this: “[css3-multicol] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 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.

This document has been a Working Draft in the CSS Working Group for several years. Multi-column layouts are traditionally used in print. On screen, multi-column layouts have been considered experimental, and implementation and use experience was deemed necessary in order to proceed. Several implementations have occurred over the past years, and this draft incorporates useful feedback from implementors as well as authors and users.

Table of contents

1. Dependencies on other modules

This CSS3 module depends on the following other CSS3 modules:

It has non-normative references to the following other CSS3 modules:

2. 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.

On the Web, tables have also been used to describe multi-column layouts. The main benefit of using CSS-based columns is flexibility; content can flow from one column to another, and the number of columns can vary depending on the size of the viewport. By removing presentational table markup from documents they can more easily be presented on various output devices, including speech synthesizers and small mobile devices.

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 }

The shorthand ‘columns’ property can be used to set both properties in one declaration. The rule in the example below sets the same property/value pairs as the rules in the two examples above:

body { columns: 2 12em }

Another group of properties introduced in this module describe gaps and rules between columns. Here is a simple example:

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 also a compound property. The example above is therefore identical to this:

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

A third group of properties indicate where column breaks should occur:

h1 { column-break-before: always }
h2 { column-break-after: avoid }

Finally, ‘column-fill’ and ‘column-span’ give style sheets a wider range of visual expressions in multi-column layouts:

div { column-fill: balance }
h2 { column-span: all }

This specification introduces twelve 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.

3. The multi-column model

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 new type of container between the content box and the content, namely the column box. The content of an element with more than one column is flowed into column boxes in the block direction, and column boxes (or columns, for short) are flowed into the content box of the multi-column element in the inline-progression-direction.

An element that has, or possibly has, more than one column is called a multi-column element (or simply multicol). That is, multi-column elements have ‘column-count’ or ‘column-width’ different from ‘auto’. All columns of a multi-column element have the same column width, column height, and the same gap between them. A multi-column element establishes a new block formatting context, as per CSS 2.1 section 9.4.1.

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.

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.

Between adjacent columns of the same element, there can be column gaps and column rules.

Column gaps (yellow) and column rules (green) are shown in this sample rendition of a multi-column element with padding (blue). The blue and yellow is present for illustrational purposes only. In actual implementations these ares will be determined by the background.

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs

tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.

M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
Pqr stu vw xyz.

To illustrate the effects of the various properties described in this specification, variations on a sample document will be used. Here is the source code of the sample document:

<html>
<style>
div {
  column-width: 15em;
  column-gap: 2em;   /* shown in yellow */            
  column-rule: 4px solid green;
  padding: 5px;      /* shown in blue */
}
img { display: none }
</style>
<body>
<div>
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
<img src=...>
def g hij
...
</div>
</body>
</html>

The nonsensical text in the example is the English alphabet which shows how text flows from one column to another. To simplify the visualization, the textual content of the different examples vary slightly.

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 }

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g
hij klm
nopq
rs tuv
wxy x
yz. Ab
cde fgh i jkl. Mno
pqr stu vw xyz. A bc

def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw

x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.

The black box represents the image.

Column boxes act like block-level, table cell, and inline-block boxes as per CSS 2.1, section 10.1, item 2 [CSS21]. As a result, floats are positioned with regard to the column box. However, the column box does not establish a contaning block for elements with ‘position: fixed’ or ‘position: absolute’.

In this example, the width of the image is set with these rules:

img { display: block; width: 100% }

Given that the width is calculated relative to the column box, the image will be as wide as the column box:

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc






def g hij klm nopqrs
tuv wxy z. Abc de fg

hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz.

The top margin of the first element and the bottom margin of the last element will collapse with the margins of the multi-column element as per the normal rules for collapsing.

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

4. The number and width of columns

Finding the number and width of columns is fundamental when laying out multi-column content. When the block direction is unconstrained and no column breaks are added through style sheets, these two properties determine the outcome:

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

4.1. column-width

Name: column-width
Value: <length> | auto
Initial: auto
Applies to: block-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: the absolute length

The column-width property 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).

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.

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, all length values (in horizontal text these are: ‘width’, ‘column-width’, column-gap', and ‘column-rule-width’) must be specified.

4.2. column-count

Name: column-count
Value: <integer> | auto
Initial: auto
Applies to: block-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: specified value

The column-count property describes the ideal number of columns into which the content of the element will be flowed.

An integer value sets the ideal number of columns into which the content of the element will be flowed.

Example:

body { column-count: 3 }

4.3. columns

Name: columns
Value: [ [ <integer> | auto] || [ <length> | auto] ]
Initial: see individual properties
Applies to: block-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: see individual properties

This is a shorthand property for setting ‘column-width’ and ‘column-count’ at the same place in the style sheet. If two legal values (one for ‘column-count’ and one for ‘column-width’) are specified, both properties are set to their respective specified value. If only one value is specified, the following applies:

4.4. Pseudo-algorithm

The pseudo-algorithm below determines the used values for ‘column-count’ (N) and ‘column-width’ (W). There are two other variables in the pseudo-algorithm:

Two assumptions are being made by the pseudo-algorithm:

The floor() function rounds a number to the next smaller integer.

if ((column-width = auto) and (column-count = auto)) or
   ((available-width = unknown) and (column-count = auto)) then
  exit; /* no columns */
fi

if (available-width = unknown) and (column-count != auto) and (column-width != auto) then
  N := column-count;
  W := column-width;
  exit;
fi

if (available-width = unknown) then
  available-width := shrink-to-fit;
fi

if (column-width = auto) then
  if ((column-count - 1) * column-gap < available-width) then
    N := column-count;
    W := (available-width - ((N - 1) * column-gap))/N;
  elsif (column-gap >= available-width) then
    N := 1;
    W := available-width;
  else
    N := floor(available-width/column-gap);
    W := (available-width - ((N - 1) * column-gap))/N;
  fi
elsif (column-count = auto) then
  if (column-width >= available-width) then
    N := 1
    W := column-width;
  else
    N := floor((available-width + column-gap) / (column-width + column-gap));
    W := ((available-width + column-gap) / N) - column-gap;
  fi
elsif ((column-count * column-width) + ((column-count - 1 ) * column-gap) <= available-width) then
  N := column-count;
  W := column-width;
elsif (column-width >= available-width) then
  N := 1
  W := column-width;
else
  N := floor((available-width + column-gap) / (column-width + column-gap));
  W := ((available-width + column-gap) / N) - column-gap;
fi

Note that, in most cases, only one of ‘column-width’ and ‘column-count’ affect the layout. If ‘column-width’ has a value other than ‘auto’, ‘column-count’ will be ignored. The only case when both ‘column-width’ and ‘column-count’ can affect the layout is for element where the width of the element has not been determined. This can, e.g., be the case for table cells and floats.

4.5. Stacking context

All column boxes in a multi-column element are in same stacking context. If content from two columns overlaps while in same z-order they are drawn in tree order. No new behavior is introduced over CSS2.1 Appendix E.

5. Column gaps and rules

Column gaps and rules are placed between columns of the same multi-column element. The length of the column gaps and column rules is equal to the length of the columns. Column gaps take up space. That is, column gaps will push apart content in adjacent columns (within the same multi-column element). A column rule is placed in the middle of the column gap. 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 column rule will overlap adjacent column boxes.

Column rules are only drawn between columns that have content in the normal flow.

5.1. column-gap

Name: column-gap
Value: <length> | normal
Initial: normal
Applies to: multi-column elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: specified value

The column-gap property sets the gap between columns. If there is a column rule between columns, it will appear in the middle of the gap.

The ‘normal’ value is UA-specific. A value of ‘1em’ is suggested.

Column gaps cannot be negative.

5.2. column-rule-color

Name: column-rule-color
Value: <color>
Initial: currentColor
Applies to: multi-column elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: the same as the computed value for the ‘color’ property [CSS3COLOR]

This property sets the color of the column rule. The <color> values are defined in [CSS3COLOR].

Conforming user agents are only required to support the subset of color values defined in [CSS21].

5.3. column-rule-style

Name: column-rule-style
Value: <border-style>
Initial: none
Applies to: multi-column elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: specified value

The ‘column-rule-style’ property sets the style of the rule between columns of an element. The <border-style> values are defined in [CSS21].

The ‘inset’ keyword value is shown like the ‘ridge’ value. The ‘outset’ value is shown like ‘groove’.

Note that the ‘none’ value forces the computed value of column-rule-width to be ‘0’.

5.4. column-rule-width

Name: column-rule-width
Value: <border-width>
Initial: medium
Applies to: multi-column elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: absolute length; ‘0’ if the column rule style is ‘none’ or ‘hidden

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

5.5. column-rule

Name: column-rule
Value: <‘border-width’> || <‘border-style’> || [ <color> | transparent ]
Initial: see individual properties
Applies to: multi-column elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: see individual properties

This property is a shorthand property for setting ‘column-rule-width’, ‘column-rule-style’, and ‘column-rule-color’ at the same place in the style sheet.

6. 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. CSS3 Paged Media [CSS3PAGE] describes rules for breaking content into pages. This specification prescribes the same rules for column breaks, with the following differences:

Because the rules for column breaks are similar to rules for page breaks, margins will be set to zero where column breaks occur.

6.1. column-break-before

Name: column-break-before
Value: auto | always | avoid
Initial: auto
Applies to: block-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: as specified

6.2. column-break-after

Name: column-break-after
Value: auto | always | avoid
Initial: auto
Applies to: block-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: as specified

7. Spanning columns

The ‘column-span’ property makes it possible for an element to span across several columns.

7.1. column-span

Name: column-span
Value: 1 | all
Initial: 1
Applies to: static, non-floating elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: as specified

This property describes how many columns an element spans across.

1
The element does not span multiple columns.
all
The element spans across all columns. Content in the normal flow that appear before the element is automatically balanced across all columns before the element appears.

An element that spans more than one column is called a spanning element.

In this example, an h2 element has been added to the sample document after the first sentence in the fourth alphabet (i.e., after the word "jkl."). This styling applies:

h2 { column-span: all; background: silver } 
img { display: none }

By setting ‘column-span’ to ‘all’, all content that appear before the h2 element is shown before the h2 element.

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc


M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno. Pqrstu vw

def g hij klm nopqrs
tuv wxy z. Abc de fg


x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg

hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.


hi jklmno. Pqrstu vw
x yz. Abc def ghi jkl.
M nop qrst uv wx yz.
Ab cde fgh i jkl. Mno
pqr stu vw xyz.

An H2 element

8. Filling columns

There are two strategies for filling columns: columns can either be balanced, or not. If columns are balanced, UAs will try to balance content between different columns. Otherwise, columns are filled sequentially. In any case, the user agent should try to honor the ‘widows’ and ‘orphans’ properties.

Name: column-fill
Value: auto | balance
Initial: balance
Applies to: multi-column elements
Inherited: no
Percentages: N/A
Media: paged
Computed value: as specified

The values are:

balance
Balance content equally between columns, if possible.
auto
Fills columns sequentially.

In continous media, this property will only be consulted if the length of columns has been constrained. Otherwise, columns will will automatically be balanced.

9. Overflow and multi-column elements

Content that extend into column gaps (e.g., long words and floats that are higher and/or wider than the column box) is clipped in the middle of the column gap. Content that extend outside column boxes at the edges of the multi-column element is clipped according to the ‘overflow’ property.

However, some content (e.g., floats) may intrude into other columns.

A multi-column element can have more columns than it has room for due to:

In paged media, columns for which there is not room within the page are are moved to the next page.

In this example, explicit column breaks are generated after paragraphs:

p { 
  column-break-after: always;
}

As a result, the number of columns is increased and another page is generated. This would appear on the first page:

Ab cde fgh i jkl. Mno
pqr stu vw xyz.

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z.

Ab cde fgh i jkl.

This would appear on the second page:

Ab cde fgh i jkl. Mno
pqr stu vw xyz. A bc
def g hij klm nopqrs
tuv wxy z. Abc de fg
hi jklmno.

10. Conformance

Conforming UAs must flow the content of an element into several columns according to this specification.

User agents that support CSS3 Backgrounds and Borders [[!CSS3BACKGROUND]] must support ‘column-gap-image’; for other UAs this property is optional.

For the ‘column-rule-color’ property, conforming user agents are only required to support the subset of color values defined in [CSS21].

Acknowledgments

This document is based on several older proposals and comments on older proposals. Contributors include Peter-Paul Koch, Till Halbach, CÚdric Savarese, Andy Clarke, Robert O'Callahan, Markus Mielke, Alex Mogilevsky, Sergey Genkin, Michael Day, Melinda Grant, Kevin Lawver, David Baron, Bert Bos, Dave Raggett, Chris Wilson, Robert Stevahn, Peter Linss, Chris Lilley, Steve Zilles, Tantek Çelik, Daniel Glazman and Ian Hickson.

References

Normative references

[CSS21]
Bert Bos; et al. Cascading Style Sheets, level 2 revision 1. 11 April 2006. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2006/WD-CSS21-20060411
[CSS3BOX]
Bert Bos. CSS3 module: The box model. 24 October 2002. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2002/WD-css3-box-20021024
[CSS3COLOR]
Tantek Çelik; Chris Lilley. CSS3 Color Module. 14 May 2003. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2003/CR-css3-color-20030514
[CSS3GCPM]
Håkon Wium Lie. CSS Generated Content for Paged Media Module. 19 September 2006. W3C Working Draft. (Work in progress) URL: http://www.w3.org/TR/2006/WD-css3-gcpm-20060919
[CSS3PAGE]
Håkon Wium Lie; Melinda Grant. CSS3 Paged Media Module. 25 February 2004. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2004/CR-css3-page-20040225

Other references

Index

Property index

Property Values Initial Applies to Inh. Percentages Media
column-break-after auto | always | avoid auto block-level elements no N/A visual
column-break-before auto | always | avoid auto block-level elements no N/A visual
column-count <integer> | auto auto block-level elements no N/A visual
column-fill auto | balance balance multi-column elements no N/A paged
column-gap <length> | normal normal multi-column elements no N/A visual
column-rule <‘border-width’> || <‘border-style’> || [ <color> | transparent ] see individual properties multi-column elements no N/A visual
column-rule-color <color> currentColor multi-column elements no N/A visual
column-rule-style <border-style> none multi-column elements no N/A visual
column-rule-width <border-width> medium multi-column elements no N/A visual
columns [ [ <integer> | auto] || [ <length> | auto] ] see individual properties block-level elements no N/A visual
column-span 1 | all 1 static, non-floating elements no N/A visual
column-width <length> | auto auto block-level elements no N/A visual