Spanning and Balancing Columns - CSS: Cascading Style Sheets 编辑

In this guide we look at how to make elements span across columns inside the multicol container and how to control how the columns are filled.

Note the spanning and balancing functionality covered in this guide is not as well supported across browsers as the functionality covered in the previous two sections in this guide.

Spanning the Columns

To cause an item to span across columns use the property column-span with a value of all. This will cause the element to span all of the columns.

Any descendant element of the multicol container may become a spanner including both direct and indirect children. For example, a heading nested directly inside the container could become a spanner, as could a heading nested inside a section nested inside the multicol container.

In the example below, the h2 element is set to column-span: all and spans all of the columns.

In this second example, the heading is inside an <article> element, yet still spans the content as expected.

When a spanner is introduced, it breaks the flow of columns and columns restart after the spanner, effectively creating a new set of column boxes. The content does not jump over a spanning element.

Limitations of column-span

In the current level 1 specification there are only two allowable values for column-span. The value none is the initial value and means the item does not span, remaining within a column. The value all means the item spans all of the columns. You cannot cause an item to span two out of three columns for example.

Things to watch out for

If the spanning element is inside another element which has margins, padding and a border or a background color, it is possible to end up with the top of the box appearing above the spanner and the rest displaying below, as shown in the next example. For this reason, some care should be taken when deciding to make an element a spanner and ensure this scenario is accounted for.

Additionally, if a spanning element appears later in the content it can cause unexpected or unwanted behaviour when there is not enough content to create columns after the spanner. Use spanning carefully and test at various breakpoints to make sure you get the intended effect.

Column Filling and Balancing

A balanced set of columns is where all columns have approximately the same amount of content. Filling and balancing comes into play when the amount of content does not match the amount of space provided, such as when a height is declared on the container.

The initial value of multicol for column-fill is balance. The value of balance means all columns are as balanced as is possible. In fragmented contexts such as Paged Media, only the last fragment is balanced. This means that on the last page the final set of column boxes will be balanced.

There is a second value for balancing, balance-all, which attempts to balance all columns in fragmented contexts and not just the columns on the final fragment.

In this example, we have columns containing an image and some text which are balanced. The image cannot break and so goes into the first column and the other columns fill with equal amounts of text.

The other value for column-fill is auto. In this case, rather than filling all the columns equally so their heights are balanced, the columns are filled sequentially. In the example below we have changed column-fill to auto and the columns are now filled, in order, to the height of the multicol container, leaving some columns empty at the end.

Note that column balancing is not supported by all browsers. Check that you are getting the sort of effect that you expect in the browsers you support.

In the next guide you will learn how multicol handles overflow, both within columns and where there are more columns than will fit the container.

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

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

发布评论

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

词条统计

浏览:137 次

字数:5814

最后编辑:8 年前

编辑次数:0 次

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