--- title: Column layouts slug: Web/CSS/Layout_cookbook/Column_layouts translation_of: Web/CSS/Layout_cookbook/Column_layouts ---
{{CSSRef}}
你可能经常需要创建一个包含几个栏目的布局,css提供了几个方法来实现它。使用Grid, Flexbox 或者 Multi-column layout 都取决于你想要实现什么目标,在这篇recipe中我们会探讨这些内容
three different styles of layouts which have two columns in the container.

Requirements

你也许会为你的栏目实现多种设计样式:

The recipes

为了达到你的需要你需要选择不同的布局方式

A continuous thread of content — multi-column layout

If you create columns using multi-column layout your text will remain as a continuous stream filling each column in turn. The columns must all be the same size, and you are unable to target an individual column or the content of an individual column.

如果你使用多列布局来创建列,则文本会以连续流的形式依次填满每个列。这些列必须大小相同,并且你不能够针对单个列或者单个列的内容

You can control the gaps between columns with the {{cssxref("column-gap")}} property, and add a rule between columns using {{cssxref("column-rule")}}.

你可以使用{{cssxref("column-gap")}} 控制列之间的距离,并且使用{{cssxref("column-rule")}}.增加列之间的规则

{{EmbedGHLiveSample("css-examples/css-cookbook/columns-multicol.html", '100%', 720)}}

Download this example

在以下情况使用多列布局:

A single row of items with equal heights — flexbox

Flexbox can be used to break content into columns by setting {{cssxref("flex-direction")}} to row, however flexbox targets the elements inside the flex container and will place each direct child into a new column. This is a different behavior to what you saw with multicol.

Flexbox通过设置 row的{{cssxref("flex-direction")}} ,可以用于将内容分成列, 然而,flexbox的目标是flex容器内的元素,并将把每个直接的子元素放到一个新列中,这是与multicol不同的地方。

There is currently no way to add a rule between flex items, and browser support for the {{cssxref("column-gap")}} and {{cssxref("row-gap")}} properties is limited. Therefore to create gaps between items use a margin.

目前没有办法可以在flex的item中添加规则,而且浏览器对{{cssxref("column-gap")}} 和 {{cssxref("row-gap")}}的支持是有限的,因此使用margin属性来创建item之间的间距

{{EmbedGHLiveSample("css-examples/css-cookbook/columns-flexbox.html", '100%', 720)}}

Download this example

Flexbox can also be used to create layouts where the flex items wrap onto new rows, by setting the {{cssxref("flex-wrap")}} property on the container to wrap. These new flex lines will distribute space along that line only — the items in the new line will not line up with items in the line above, as you'll see in the example below. This is why flexbox is described as one-dimensional. It is designed for controlling layout as a row or a column, but not both at the same time.

Flexbox还可以被用来创建 flex items 自动换行的布局,通过给 flex container 设置  {{cssxref("flex-wrap")}} 属性为wrap. 这些新的flex行只会沿该行分配空间——新行中的项不会与上面行中的项对齐,你可以在下面的例子中看到。这就是为什么flexbox被描述为一维。 他是为了将布局控制为行或列,但不是同时控制行和列。

{{EmbedGHLiveSample("css-examples/css-cookbook/columns-flexbox-wrapping.html", '100%', 720)}}

Download this example

Use flexbox:

Lining items up in rows and columns — grid layout

If what you want is a layout where items line up in rows and columns then you should choose CSS Grid Layout. Grid Layout works on the direct children of the grid container in a similar way to the manner in which flexbox works on the direct children of the flex container, however with CSS Grid you can line your items up in rows and columns — it is described as two-dimensional.

如果你想要items在行和列进行布局,你应该选择Grid Layout. Grid Layout 作用于 grid container 的 直接子元素类似于 flexbox。 但是你可以在行和列两方面来控制他(flex只能在行或者列)。

{{EmbedGHLiveSample("css-examples/css-cookbook/columns-grid.html", '100%', 720)}}

Download this example

Use Grid:

Browser compatibility

The various layout methods have different browser support. See the charts below for details on basic support for the properties used.

column-width

{{Compat("css.properties.column-width")}}

column-rule

{{Compat("css.properties.column-rule")}}

flex

{{Compat("css.properties.flex")}}

flex-wrap

{{Compat("css.properties.flex-wrap")}}

grid-template-columns

{{Compat("css.properties.grid-template-columns")}}

Resources on MDN