1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
---
title: Column layouts
slug: Web/CSS/Layout_cookbook/Column_layouts
translation_of: Web/CSS/Layout_cookbook/Column_layouts
---
<div>{{CSSRef}}</div>
<div>你可能经常需要创建一个包含几个栏目的布局,css提供了几个方法来实现它。使用Grid, Flexbox 或者 Multi-column layout 都取决于你想要实现什么目标,在这篇recipe中我们会探讨这些内容</div>
<div><img alt="three different styles of layouts which have two columns in the container." src="https://mdn.mozillademos.org/files/16183/cookbook-multiple-columns.png" style="height: 406px; width: 1541px;"></div>
<h2 id="Requirements">Requirements</h2>
<p>你也许会为你的栏目实现多种设计样式:</p>
<ul>
<li>连续的内容线,分成报纸式的专栏</li>
<li>被划分成几个高度相等的栏目的一行</li>
<li>按行和列排列的多行列</li>
</ul>
<h2 id="The_recipes">The recipes</h2>
<p>为了达到你的需要你需要选择不同的布局方式</p>
<h3 id="A_continuous_thread_of_content_—_multi-column_layout">A continuous thread of content — multi-column layout</h3>
<p>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.</p>
<p>如果你使用多列布局来创建列,则文本会以连续流的形式依次填满每个列。这些列必须大小相同,并且你不能够针对单个列或者单个列的内容</p>
<p>You can control the gaps between columns with the {{cssxref("column-gap")}} property, and add a rule between columns using {{cssxref("column-rule")}}.</p>
<p>你可以使用{{cssxref("column-gap")}} 控制列之间的距离,并且使用{{cssxref("column-rule")}}.增加列之间的规则</p>
<p>{{EmbedGHLiveSample("css-examples/css-cookbook/columns-multicol.html", '100%', 720)}}</p>
<div class="note">
<p><a href="https://github.com/mdn/css-examples/blob/master/css-cookbook/columns-multicol--download.html">Download this example</a></p>
</div>
<p>在以下情况使用多列布局:</p>
<ul>
<li>你想要你的文本看起来想报纸那种排版布局。</li>
<li>您有一组要分成列的小项目。</li>
<li>你不需要单独为指定的列写样式。</li>
</ul>
<h3 id="A_single_row_of_items_with_equal_heights_—_flexbox">A single row of items with equal heights — flexbox</h3>
<p>Flexbox can be used to break content into columns by setting {{cssxref("flex-direction")}} to <code>row</code>, 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.</p>
<p>Flexbox通过设置 row的{{cssxref("flex-direction")}} ,可以用于将内容分成列, 然而,flexbox的目标是flex容器内的元素,并将把每个直接的子元素放到一个新列中,这是与multicol不同的地方。</p>
<p>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.</p>
<p>目前没有办法可以在flex的item中添加规则,而且浏览器对{{cssxref("column-gap")}} 和 {{cssxref("row-gap")}}的支持是有限的,因此使用margin属性来创建item之间的间距</p>
<p>{{EmbedGHLiveSample("css-examples/css-cookbook/columns-flexbox.html", '100%', 720)}}</p>
<div class="note">
<p><a href="https://github.com/mdn/css-examples/blob/master/css-cookbook/columns-flexbox--download.html">Download this example</a></p>
</div>
<p>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 <code>wrap</code>. 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.</p>
<p>Flexbox还可以被用来创建 flex items 自动换行的布局,通过给 flex container 设置 {{cssxref("flex-wrap")}} 属性为wrap. 这些新的flex行只会沿该行分配空间——新行中的项不会与上面行中的项对齐,你可以在下面的例子中看到。这就是为什么flexbox被描述为一维。 他是为了将布局控制为行或列,但不是同时控制行和列。</p>
<p>{{EmbedGHLiveSample("css-examples/css-cookbook/columns-flexbox-wrapping.html", '100%', 720)}}</p>
<div class="note">
<p><a href="https://github.com/mdn/css-examples/blob/master/css-cookbook/columns-flexbox-wrapping--download.html">Download this example</a></p>
</div>
<p>Use flexbox:</p>
<ul>
<li>For single rows or columns of items. 设置一行或者一列中的items</li>
<li>When you want to do alignment on the cross axis after laying out your items. 当你想在横轴上对齐您的items</li>
<li>When you are happy for wrapped items to share out space along their line only and not line up with items in other lines.</li>
</ul>
<h3 id="Lining_items_up_in_rows_and_columns_—_grid_layout">Lining items up in rows and columns — grid layout</h3>
<p>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.</p>
<p>如果你想要items在行和列进行布局,你应该选择Grid Layout. Grid Layout 作用于 grid container 的 直接子元素类似于 flexbox。 但是你可以在行和列两方面来控制他(flex只能在行或者列)。</p>
<p>{{EmbedGHLiveSample("css-examples/css-cookbook/columns-grid.html", '100%', 720)}}</p>
<div class="note">
<p><a href="https://github.com/mdn/css-examples/blob/master/css-cookbook/columns-grid--download.html">Download this example</a></p>
</div>
<p>Use Grid:</p>
<ul>
<li>For multiple rows or columns of items. </li>
<li>When you want to be able to align the items on the block and inline axes.</li>
<li>When you want items to line up in rows and columns.</li>
</ul>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>The various layout methods have different browser support. See the charts below for details on basic support for the properties used.</p>
<h4 id="column-width">column-width</h4>
<p>{{Compat("css.properties.column-width")}}</p>
<h4 id="column-rule">column-rule</h4>
<p>{{Compat("css.properties.column-rule")}}</p>
<h4 id="flex">flex</h4>
<p>{{Compat("css.properties.flex")}}</p>
<h4 id="flex-wrap">flex-wrap</h4>
<p>{{Compat("css.properties.flex-wrap")}}</p>
<h4 id="grid-template-columns">grid-template-columns</h4>
<p>{{Compat("css.properties.grid-template-columns")}}</p>
<h2 id="Resources_on_MDN">Resources on MDN</h2>
<ul>
<li><a href="/en-US/docs/Web/CSS/CSS_Columns">Guide to Multi-column Layout</a></li>
<li><a href="/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/">Guide to Flexbox</a></li>
<li><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout">Guide to CSS Grid Layout</a></li>
</ul>
|