Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. CSS
  3. CSS multi-column layout
  4. Using multi-column layouts

Using multi-column layouts

The properties defined in theCSS multi-column layout module extend theblock layout mode, enabling the easy definition of multiple columns of text. People have trouble reading text if lines are too long. If it takes too long for the eyes to move from the end of one line to the beginning of the next, readers can lose track of which line they were on. To provide for a better user experience when reading text making use of a large screen, you should limit the width of text by using columns of text placed side by side, just as newspapers do.

Using columns

Column count and width

Two CSS properties control whether and how many columns will appear:column-count andcolumn-width.

Thecolumn-count property sets the number of columns to a particular number. E.g.,

Example 1

HTML

html
<div>  <p>    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod    tempor incididunt ut labore et dolore magna aliqua.  </p>  <p>    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut    aliquip ex ea commodo consequat.  </p>  <p>    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore    eu fugiat nulla pariatur.  </p>  <p>    Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia    deserunt mollit anim id est laborum.  </p></div>

CSS

css
#col {  column-count: 2;}

Result

The content will be displayed in two columns:

Thecolumn-width property sets the minimum desired column width. Ifcolumn-count is not also set, then the browser will automatically make as many columns as fit in the available width.

Example 2

HTML

html
<div>  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>

CSS

css
#wid {  column-width: 100px;}

Result

In a multi-column block, content automatically flows from one column into the next as needed. All HTML, CSS, and DOM functionality is supported within columns, as are editing and printing.

The columns shorthand

You can use eithercolumn-count orcolumn-width. Because values for these properties do not overlap, it is often convenient to use the shorthandcolumns.

Example 3

In this example, the CSS declarationcolumn-width: 12em is replaced bycolumns: 12em.

HTML

html
<div>  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>

CSS

css
#col_short {  columns: 12em;}

Example 4

In this example, the CSS declarationcolumn-count: 4 is replaced bycolumns: 4.

HTML

html
<div>  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>

CSS

css
#columns_4 {  columns: 4;}

Result

Example 5

The two CSS declarationscolumn-width: 8em andcolumn-count: 12 can be replaced bycolumns: 12 8em. Thecolumn-count portion of the shorthand is the maximum number of columns that will be present. Thecolumn-width is the minimum width each column should be.

HTML

html
<div>  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>

CSS

css
#columns_12 {  columns: 12 8em;}

Result

Assuming a default1em gap between columns, if the container is wider than103ems (12 columns *8em width each + 71em gaps), there will be 12 columns, each with a width of8ems or more. If the container is less than103ems wide, there will be fewer than 12 columns. If the container is less than17ems wide (8em column +8em column +1em gap), the content will be displayed as a single column with no column gap.

Height balancing

CSS columns require that the column heights must be balanced: that is, the browser automatically sets the maximum column height so that the heights of the content in each column are approximately equal. Firefox does this.

However, in some situations it is also useful to set the maximum height of the columns explicitly, and then lay out content starting at the first column and creating as many columns as necessary, possibly overflowing to the right. Therefore, if the height is constrained, by setting the CSSheight ormax-height properties on a multi-column block, each column is allowed to grow to that height and no further before adding new column. This mode is also much more efficient for layout.

Column Gaps

There is a gap between columns. The recommended default is1em. This size can be changed by applying thecolumn-gap property to the multi-column block:

Example 6

HTML

html
<div>  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>

CSS

css
#column_gap {  column-count: 5;  column-gap: 2em;}

Result

Conclusion

CSS columns are a layout primitive that can help make large blocks of text easier to read when responsive content is viewed on wide viewports. Imaginative developers may find many uses for them, especially in conjunction withcontainer queries and with the automatic height balancing feature.

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp