CSS3 module: Multi-column layout

W3C Working Draft 15 December 2005

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

Abstract

This module describes multi-column layout in CSS. It builds on the 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 under the 24 January 2002 CPP as amended by the W3C Patent Policy Transition Procedure. The Working Group maintains a public list of patent disclosures relevant to this document; 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) with respect to this specification should 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 year, 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 (informative) 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: 15em }

In the above example, the body element is set to have columns at least '15em' 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 15em }

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. 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 the one below:

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 }
h1, h2 { column-break-inside: avoid }

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

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. Content is flowed into column boxes in the block-progression-direction, and column boxes (or "columns", for short) are flowed into content boxes in the inline-progression-direction. All columns of a content box have the same column width and column length. 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 (although they may in the future). The column box becomes the containing block for the block children (explicit or anonymous) of the element on which the columns are defined.

Between adjacent columns of the same element, there can be column gaps and column rules. Column gaps similar to padding areas in the sense that they take up space. Column rules are visually similar to borders, and they are described by the same values as borders. The column rule is placed in the middle of the column gap. Different from borders, column rules do not take up space.

In paged media, the size of columns is constrained by the page area.

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

Content that extend outside the column box (e.g., long words and floats that are higher and/or wider than the column box) is clipped. This only applies to content for which the column box is the containing block (i.e., as if the column box had 'overflow:hidden').

Column boxes act as containing blocks for content in the columns. More specifically, column boxes act like block-level, table cell, and inline-block boxes as per CSS 2.1, section 10.1, item 2 [CSS21].

Or, should they not be containing blocks?

The top margin of elements in the first column collases with preceding margins in much the same way as they would if the element did not use columns. Similarly, the bottom margin of the last column collapses with following margins.

4. The number and width of columns

Finding the number, width and length of columns is fundamental when laying out multi-column content. When the block-progression-direction is unconstrained, these two properties determine the outcome:

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

The following pseudo-algorithm determines the used values for 'column-count' (N) and 'column-width' (W):

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

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

if (!determined-width) then
  available-width := shrink-to-fit-width;
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;
  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;
fi

Describe in more detail these terms: "determined-width", "available-width", "shrink-to-fit-width", and "floor".

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.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 width of columns. The actual column width may be wider (to fill the available space), or narrower (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 width. 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.

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 (on '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 }

If the value is '1', only one column will be generated. I.e., the content will be laid out as if this specification did not exist.

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:

5. Column gaps and rules

Column gaps and rules are placed between columns of the same element. The length of the column gaps and column rules is equal to the length of the columns. The 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 columns or column gaps.

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: block-level 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 UA-specific. A value of '1em' is suggested.

Column gaps cannot be negative.

5.2. 'column-rule-color'

Name: column-rule-color
Value: <color> | transparent
Initial: currentcolor
Applies to: block-level elements
Inherited: no
Percentages: N/A
Media: visual
Computed value: when taken from the 'color' property, the computed value of 'color'; otherwise, as specified

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: block-level 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: block-level 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.

5.5. 'column-rule'

Name: column-rule
Value: <'border-width'> || <'border-style'> || <color>
Initial: see individual properties
Applies to: block-level 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. CSS 2.1 [CSS21] describes rules for breaking content into pages. This specification prescribes the same rules for column breaks, with the following differences:

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

6.3. 'column-break-inside'

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

Values for these properties have the following meanings:

auto
Neither force nor forbid a column break before (after, inside) the generated box.
always
Always force a column break before (after) the generated box.
avoid
Avoid a column break before (after, inside) the generated box.

7. Unresolved issues

This section contains identified, but unresolved issues. Feedback from implementors is encouraged.

7.1. The length of columns

What happens when the 'height' property is non-auto? Are column lengths in any way constrained by the 'height' property? Two options seems possible:

Similarly, the 'max-height' and 'min-height' properties must be described.

Another option is to create 'column-length' property.

7.2. Absolutely positioned elements in columns

One developer writes:

What is the desired behaviour when an element in a column is an absolute container (e.g., relatively positioned)? In particular, do its absolutely positioned children break at column boundaries too? A similar question arises for pages and the fact that we don't currently break abs-pos children across page boundaries is a huge source of complaints, so I strongly push for abs-pos children to break at column boundaries, and I'd like as much guidance as possible as to the preferred behaviour. Do consider what happens if the in-flow content fits in N columns but additional columns would be required to fit abs-pos content.

7.3. Floating elements in columns

One delvoper writes:

When multiple floats occur in a line and they have different heights, an arbitrary subset of the floats may break to the next column while the rest are complete in the current column. Should the different parts of each float have a consistent x-offset within their columns? Should they have a consistent width? My preferred answer to the former is "no", it forces too much white space. I'm not sure what my preferred answer to the latter is, but I think it might simplify things if we can generally assume that all boxes for a given element have the same width. If you allow different parts of the same float to have different widths then it becomes unclear what happens to a child element which is "width:30%".

7.4. Stacking context in columns

One developer writes:

When an element that forms a stacking context (e.g. abs-pos with z-index not 'auto', or 'opacity' not 1) breaks vertically, do the pieces together still form a single stacking context? This could get hard to implement in general.

Another person writes that:

[The specification needs to] state where columns and the column rule are rendered in terms of a delta from CSS2.1 Appendix E.

8. Conformance

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

Acknowledgments

This document is based on several older proposals and comments on older proposals. Contributors include Peter-Paul Koch, Till Halbach, CÚdric Savarese, Robert O'Callahan, Markus Mielke, 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. 25 February 2004. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2004/CR-CSS21-20040225
[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

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-after auto | 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-gap <length> | normal normal block-level elements no N/A visual
column-rule <'border-width'> || <'border-style'> || <color> see individual properties block-level elements no N/A visual
column-rule-color <color> | transparent currentcolor block-level elements no N/A visual
column-rule-style <border-style> none block-level elements no N/A visual
column-rule-width <border-width> medium block-level elements no N/A visual
columns [ [ <integer> | auto] || [ <length> | auto] ] see individual properties block-level elements no N/A visual
column-width <length> | auto auto block-level elements no N/A visual