Column layouts - CSS(层叠样式表) 编辑

你可能经常需要创建一个包含几个栏目的布局,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 column-gap property, and add a rule between columns using column-rule.

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

Download this example

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

  • 你想要你的文本看起来想报纸那种排版布局。
  • 您有一组要分成列的小项目。
  • 你不需要单独为指定的列写样式。

A single row of items with equal heights — flexbox

Flexbox can be used to break content into columns by setting 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的flex-direction ,可以用于将内容分成列, 然而,flexbox的目标是flex容器内的元素,并将把每个直接的子元素放到一个新列中,这是与multicol不同的地方。

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

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

Download this example

Flexbox can also be used to create layouts where the flex items wrap onto new rows, by setting the 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 设置  flex-wrap 属性为wrap. 这些新的flex行只会沿该行分配空间——新行中的项不会与上面行中的项对齐,你可以在下面的例子中看到。这就是为什么flexbox被描述为一维。 他是为了将布局控制为行或列,但不是同时控制行和列。

Download this example

Use flexbox:

  • For single rows or columns of items. 设置一行或者一列中的items
  • When you want to do alignment on the cross axis after laying out your items. 当你想在横轴上对齐您的items
  • When you are happy for wrapped items to share out space along their line only and not line up with items in other lines.

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只能在行或者列)。

Download this example

Use Grid:

  • For multiple rows or columns of items. 
  • When you want to be able to align the items on the block and inline axes.
  • When you want items to line up in rows and columns.

Browser compatibility

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

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

column-width

BCD tables only load in the browser

column-rule

BCD tables only load in the browser

flex

BCD tables only load in the browser

flex-wrap

BCD tables only load in the browser

grid-template-columns

BCD tables only load in the browser

Resources on MDN

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:123 次

字数:10697

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文