1. 引言
(本节非规范性内容。)
本模块描述了 CSS 中的 多列布局。 通过使用本文档中描述的功能, 样式表可以声明元素的内容 应以多列形式进行布局。
CSS 中的其他布局方法, 应用于父元素时, 会更改直接子元素的显示属性。 例如,当创建一个三列网格布局时, 网格容器 的直接子元素会变成 网格项, 并被放置在列轨中, 每个单元格中一个元素,必要时创建其他行。
然而,多列容器 的子元素依然保持正常流, 该流被安排在若干列中。 这些列具有灵活的行内尺寸, 因此会根据可用空间 通过改变列的大小或显示的列数进行响应。
在 CSS 中描述多列布局非常简单。 以下是一个简单的示例:
在样式表中也可以明确设置列的数量:
可以使用速记属性 columns 在一个声明中同时设置这两个属性。
本模块中引入的另一组属性描述了列之间的间隙和规则。
body{ column-gap : 1 em ; column-rule : thin solid black; }
上面的第一个声明将两个相邻列之间的间隙设置为 1em。 列间距类似于填充区域。 间隙中间将有一个通过 column-rule 属性描述的规则。
column-rule 属性的值与 CSS border 属性的值类似。 和 border 一样,column-rule 是一个速记属性。
body{ column-gap : 1 em ; column-rule-width : thin; column-rule-style : solid; column-rule-color : black; }
column-fill 和 column-span 属性 使样式表在多列布局中具有更广泛的视觉表现力。
本规范引入了十个新属性, 这些属性都在上面的示例中使用了。
如果所有列属性都具有初始值, 则元素的布局将与仅包含一列的多列布局相同。
1.1. 值的定义
本规范遵循 CSS 属性定义约定, 引用了 [CSS21],并使用了 值定义语法 来自 [CSS-VALUES-3]。 未在本规范中定义的值类型在 CSS 值与单位 [CSS-VALUES-3] 中定义。 结合其他 CSS 模块可能扩展这些值类型的定义。
除了各属性定义中列出的特定值外, 本规范中定义的所有属性 还接受 CSS 范围内的关键字 作为其属性值。 为了可读性,未在此明确重复。
2. 多列模型
当元素的 column-width 或 column-count 属性不为 auto 时,会创建一个 多列容器(简称 多列容器), 并因此作为 多列布局 的容器。
测试
基础多列测试。
测试展示 auto 值不会创建多列容器。
多列属性不会继承。
带滚动列的多列布局。
高度为零的多列布局。
- multicol-zero-height-001.xht (在线测试) (源码)
- multicol-zero-height-002.html (在线测试) (源码)
- multicol-zero-height-003.html (在线测试) (源码)
在传统的 CSS 盒模型中, 元素的内容流入相应元素的内容框中。 多列布局引入了一个由分片上下文 组成的 匿名 分片容器,称为 列框(或简称列)。 这些 列框 创建了一个独立的 块格式化上下文,多列容器的内容流入其中, 并为其非定位子元素形成 包含块。
img{ display : block; width : 100 % ; }
由于列框创建了一个新的 块格式化上下文, 所以 width 是 相对于列框计算的。 因此,图像不会溢出列框:
测试
- multicol-margin-001.xht (在线测试) (源码)
- multicol-margin-002.xht (在线测试) (源码)
- multicol-margin-003.html (在线测试) (源码)
- multicol-margin-child-001.xht (在线测试) (源码)
- multicol-nested-margin-001.xht (在线测试) (源码)
- multicol-nested-margin-002.xht (在线测试) (源码)
- multicol-nested-margin-003.xht (在线测试) (源码)
- multicol-nested-margin-004.xht (在线测试) (源码)
- multicol-nested-margin-005.xht (在线测试) (源码)
- multicol-collapsing-001.xht (在线测试) (源码)
出现在多列布局中的浮动元素是根据浮动出现的列框进行定位的。
img{ display : block; float : right; }
在 HTML 中,图像出现在句子结尾 "the leg of a chicken" 之后。
在列框中溢出的内容会沿块轴进行分片,并继续在下一个列框中显示。
注意: 列框是匿名框, 不会成为包含块,用于绝对定位的框。 用于创建包含块的position 属性,应用于多列容器,它是主框。
测试
- multicol-containing-001.xht (在线测试) (源码)
- multicol-containing-002.xht (在线测试) (源码)
- multicol-containing-003.html (在线测试) (源码)
- multicol-contained-absolute.html (在线测试) (源码)
- abspos-autopos-contained-by-viewport-000.html (在线测试) (源码)
- abspos-autopos-contained-by-viewport-001.html (在线测试) (源码)
- abspos-multicol-in-second-outer-clipped.html (在线测试) (源码)
.container{ position : relative; column-count : 3 ; } img{ position : absolute; top : 20 px ; left : 40 px ; }
多列容器的脱离文档流的后代元素会影响列的平衡, 以及多列容器的块大小。
列框按多列容器的行基方向排序, 并排列成多列行。 列宽是列框在行内方向的长度。 列高是列框在块方向的长度。 同一行中的所有列框具有相同的列宽, 且同一行中的所有列框具有相同的列高。
测试
以下测试与列内容的基线对齐有关,尽管本规范中未对此进行定义。
- baseline-000.html (live test) (source)
- baseline-001.html (live test) (source)
- baseline-002.html (live test) (source)
- baseline-003.html (live test) (source)
- baseline-004.html (live test) (source)
- baseline-005.html (live test) (source)
- baseline-006.html (live test) (source)
- baseline-007.html (live test) (source)
- baseline-008.html (live test) (source)
- as-baseline-aligned-grid-item.html (live test) (source)
以下测试检查同时作为多列容器的列表项的行为。
- multicol-list-item-001.xht (live test) (source)
- multicol-list-item-002.html (live test) (source)
- multicol-list-item-003.html (live test) (source)
- multicol-list-item-004.html (live test) (source)
- multicol-list-item-005.html (live test) (source)
- multicol-list-item-006.html (live test) (source)
- multicol-list-item-007.html (live test) (source)
- multicol-list-item-008.html (live test) (source)
测试多列中的网格项
以下测试检查表格元素的行为。
- multicol-table-cell-001.xht (live test) (source)
- multicol-table-cell-height-001.xht (live test) (source)
- multicol-table-cell-height-002.xht (live test) (source)
- multicol-table-cell-vertical-align-001.xht (live test) (source)
- table-cell-as-multicol.html (live test) (source)
- table-cell-content-change-000.html (live test) (source)
- table-cell-content-change-001.html (live test) (source)
- table-cell-multicol-nested-001.html (live test) (source)
- table-cell-multicol-nested-002.html (live test) (source)
- table-cell-multicol-nested-003.html (live test) (source)
- break-before-multicol-caption.html (live test) (source)
- table-cell-writing-mode-root.html (live test) (source)
- balance-breakafter-before-table-section-crash.html (live test) (source)
- balance-table-with-border-spacing.html (live test) (source)
- balance-table-with-fractional-height-row.html (live test) (source)
以下测试检查绘制顺序是否正确。
- float-and-block.html (live test) (source)
- move-with-text-after-paint.html (live test) (source)
- resize-with-text-after-paint.html (live test) (source)
以下测试与多列属性的动画或转换有关。
- column-width-interpolation.html (live test) (source)
- discrete-no-interpolation.html (live test) (source)
- multicol-overflow-positioned-transform-001.html (live test) (source)
- multicol-overflow-transform-001.html (live test) (source)
- resize-multicol-with-fixed-size-children.html (live test) (source)
- resize-in-strict-containment-nested.html (live test) (source)
- remove-child-in-strict-containment-also-spanner.html (live test) (source)
- composited-under-clip-under-multicol.html (live test) (source)
- change-intrinsic-width.html (live test) (source)
- change-fragmentainer-size-000.html (live test) (source)
- change-fragmentainer-size-001.html (live test) (source)
- change-fragmentainer-size-002.html (live test) (source)
- change-fragmentainer-size-003.html (live test) (source)
- hit-test-child-under-perspective.html (live test) (source)
- hit-test-transformed-child.html (live test) (source)
与实现错误相关的测试,与具体的规范性文本无关。
- multicol-with-monolithic-oof-with-multicol-with-oof.html (live test) (source)
- chrome-bug-1293905.html (live test) (source)
- chrome-bug-1297118.html (live test) (source)
- chrome-bug-1301281.html (live test) (source)
- chrome-bug-1303256.html (live test) (source)
- chrome-bug-1314866.html (live test) (source)
- dynamic-simplified-layout-break-propagation.html (live test) (source)
- float-multicol-crash.html (live test) (source)
- change-abspos-width-in-second-column-crash.html (live test) (source)
- change-out-of-flow-type-and-remove-inner-multicol-crash.html (live test) (source)
- monolithic-oof-in-clipped-container.html (live test) (source)
- move-linebreak-to-different-column.html (live test) (source)
- move-newline-pre-text.html (live test) (source)
- multicol-at-page-boundary-print.html (live test) (source)
- multicol-block-in-inline-crash.html (live test) (source)
- multicol-cached-consumed-bsize-crash.html (live test) (source)
- multicol-column-change-crash.html (live test) (source)
- multicol-dynamic-contain-crash.html (live test) (source)
- multicol-dynamic-transform-crash.html (live test) (source)
- multicol-floats-in-ifc.html (live test) (source)
- multicol-parallel-flow-after-spanner-in-inline.html (live test) (source)
- outline-move-oof-with-inline.html (live test) (source)
- relpos-inline-with-abspos-multicol-gets-block-child.html (live test) (source)
- size-containment-become-multicol-add-inline-child.html (live test) (source)
- sticky-in-abs-in-sticky.html (live test) (source)
- vertical-rl-column-rules-wide-columns.html (live test) (source)
- dynamic-become-multicol-add-oof-inside-inline-crash.html (live test) (source)
- extremely-tall-multicol-with-extremely-tall-child-crash.html (live test) (source)
- file-control-crash.html (live test) (source)
- img-alt-as-multicol-crash.html (live test) (source)
- overflow-scroll-in-multicol-crash.html (live test) (source)
- remove-block-sibling-of-inline-with-block-crash.html (live test) (source)
- subpixel-scroll-crash.html (live test) (source)
- text-child-crash.html (live test) (source)
- with-custom-layout-on-same-element-crash.https.html (live test) (source)
- oof-in-area-001.html (live test) (source)
- oof-in-area-002.html (live test) (source)
- oof-in-area-003.html (live test) (source)
- oof-in-oof-multicol-in-multicol-spanner-in-multicol.html (live test) (source)
- oof-in-oof-multicol-in-relpos-in-oof-in-multicol-in-multicol.html (live test) (source)
- oof-in-oof-multicol-in-relpos-spanner-in-multicol-in-relpos-multicol-in-multicol.html (live test) (source)
- oof-in-oof-multicol-in-relpos-spanner-in-spanner-multicol-in-multicol-in-multicol.html (live test) (source)
- oof-in-oof-multicol-in-spanner-in-multicol-in-spanner-in-nested-multicol.html (live test) (source)
- oof-in-oof-multicol-in-spanner-in-nested-multicol.html (live test) (source)
- oof-in-relpos-in-oof-multicol-in-oof-in-relpos-in-oof-multicol-in-multicol.html (live test) (source)
- oof-in-relpos-in-oof-multicol-in-relpos-in-oof-multicol-in-relpos-multicol.html (live test) (source)
- floated-input-in-inline-next-column.html (live test) (source)
- inline-float-parallel-flow.html (live test) (source)
- table-caption-in-clipped-overflow.html (live test) (source)
- text-in-inline-interrupted-by-float.html (live test) (source)
- increase-prev-sibling-height.html (live test) (source)
- interleaved-bfc-crash.html (live test) (source)
- relayout-fixedpos-in-abspos-in-relpos-in-nested-multicol.html (live test) (source)
- inline-become-oof-container-make-oof-inflow.html (live test) (source)
- multicol-dynamic-change-inside-break-inside-avoid-001.html (live test) (source)
- multicol-dynamic-add-001.html (live test) (source)
- scroll-width-height.tentative.html (live test) (source)
- filter-with-abspos.html (live test) (source)
- add-list-item-marker.html (live test) (source)
与多列相关的打印和分页媒体测试。
- auto-fill-auto-size-001-print.html (live test) (source)
- auto-fill-auto-size-002-print.html (live test) (source)
- multicol-height-002-print.xht (live test) (source)
- named-page.html (live test) (source)
- page-property-ignored.html (live test) (source)
注意: 在使用垂直书写模式设置的文本中,块方向是水平的。 在垂直书写模式下,列是水平排列的,块流动的方向可以是从右到左,或从左到右。 因此,column-width 属性指的是列的内联大小,而不是物理的水平宽度。
测试
关于垂直书写模式的测试。
- orthogonal-writing-mode-shrink-to-fit.html (live test) (source)
- multicol-under-vertical-rl-scroll.html (live test) (source)
- hit-test-in-vertical-rl.html (live test) (source)
在每个多列线中, 多列容器中的相邻列框由列间距分隔, 列间距中可能包含一个列规则。 相同多列容器中的所有列间距是相等的。 如果出现列规则, 相同多列容器中的所有列规则也是相等的; 列规则只出现在两侧都有内容的列之间。
在最简单的情况下, 多列容器只包含一行列, 每列的高度等于多列容器内容框的使用高度。 然而, 分段或跨列可以将多列容器的内容分为多行多列线。
如果多列容器被分页, 每列的高度受页面限制, 内容将在下一页的新列框中继续; 列框永远不会跨页拆分。
当跨列元素将多列容器分开时, 也会发生同样的效果: 跨列元素之前的列会平衡并缩短以适应其内容。 跨列元素之后的内容将流入新的一行列框中。
因此,多列容器 是一个常规的块容器,它建立了一个新的独立的格式化上下文,其内容由一系列多列线和跨列元素组成。 每个多列线作为一个块级框,为其列框建立了一个多列格式化上下文; 每个跨列元素作为一个块级框,根据其display属性值,建立了一个独立的格式化上下文。
允许嵌套多列容器, 但可能存在特定于实现的限制。
测试
- multicol-nested-002.xht (live test) (source)
- multicol-nested-005.xht (live test) (source)
- multicol-nested-006.html (live test) (source)
- multicol-nested-007.html (live test) (source)
- multicol-nested-008.html (live test) (source)
- multicol-nested-009.html (live test) (source)
- multicol-nested-010.html (live test) (source)
- multicol-nested-011.html (live test) (source)
- multicol-nested-012.html (live test) (source)
- multicol-nested-013.html (live test) (source)
- multicol-nested-014.html (live test) (source)
- multicol-nested-015.html (live test) (source)
- multicol-nested-016.html (live test) (source)
- multicol-nested-017.html (live test) (source)
- multicol-nested-018.html (live test) (source)
- multicol-nested-019.html (live test) (source)
- multicol-nested-020.html (live test) (source)
- multicol-nested-021.html (live test) (source)
- multicol-nested-022.html (live test) (source)
- multicol-nested-023.html (live test) (source)
- multicol-nested-024.html (live test) (source)
- multicol-nested-025.html (live test) (source)
- multicol-nested-026.html (live test) (source)
- multicol-nested-027.html (live test) (source)
- multicol-nested-028.html (live test) (source)
- multicol-nested-029.html (live test) (source)
- multicol-nested-030.html (live test) (source)
- multicol-nested-031.html (live test) (source)
- nested-as-balanced-legend.html (live test) (source)
- nested-as-nested-balanced-legend.html (live test) (source)
- nested-floated-multicol-with-tall-margin.html (live test) (source)
- nested-multicol-and-float-with-tall-padding-before-float.html (live test) (source)
- nested-multicol-and-float-with-tall-padding.html (live test) (source)
- nested-multicol-fieldset-tall-trailing-border-freeze.html (live test) (source)
- nested-multicol-fieldset-tall-trailing-padding.html (live test) (source)
- nested-multicol-in-svg-foreignobject.html (live test) (source)
- nested-multicol-nested-flex.html (live test) (source)
- nested-multicol-with-float-between.html (live test) (source)
- nested-oof-multicol-with-monolithic-child.html (live test) (source)
- nested-oof-multicol-with-oof-needing-additional-columns.html (live test) (source)
- nested-oof-multicol-with-padding.html (live test) (source)
- nested-with-fragmented-oof-negative-top-offset.html (live test) (source)
- nested-with-multicol-table-caption.html (live test) (source)
- multicol-table-caption-parallel-flow-after-spanner-in-inline.html (live test) (source)
- nested-with-multicol-table-cell.html (live test) (source)
- nested-with-oof-inside-fixed-width.html (live test) (source)
- nested-with-percentage-size-and-oof.html (live test) (source)
- nested-with-tall-padding-and-oof.html (live test) (source)
- nested-with-tall-padding.html (live test) (source)
- oof-in-nested-line-float.html (live test) (source)
- oof-nested-multicol-inside-oof.html (live test) (source)
- relayout-nested-with-oof.html (live test) (source)
- repeated-section-in-nested-table-nested-multicol.html (live test) (source)
- repeated-table-footer-in-caption-nested-multicol.html (live test) (source)
- nested-balanced-monolithic-multicol-crash.html (live test) (source)
- nested-balanced-very-tall-content-crash.html (live test) (source)
- nested-floated-shape-outside-multicol-with-monolithic-child-crash.html (live test) (source)
- nested-with-overflowing-padding-crash.html (live test) (source)
- triply-nested-with-fixedpos-in-abspos-crash.html (live test) (source)
- fixed-in-nested-multicol-with-transform-container.html (live test) (source)
- fixed-in-nested-multicol-with-viewport-container.html (live test) (source)
- fixed-in-nested-multicol.html (live test) (source)
- nested-after-float-clearance.html (live test) (source)
- nested-at-outer-boundary-as-fieldset.html (live test) (source)
- nested-at-outer-boundary-as-float.html (live test) (source)
- nested-at-outer-boundary-as-legend.html (live test) (source)
- nested-floated-multicol-with-monolithic-child.html (live test) (source)
- nested-oofs-in-relative-multicol.html (live test) (source)
- nested-past-fragmentation-line.html (live test) (source)
- nested-with-too-tall-line.html (live test) (source)
- oof-nested-in-single-column.html (live test) (source)
- nested-non-auto-inline-size-offset-top.html (live test) (source)
- fixedpos-static-pos-with-viewport-cb-001.html (live test) (source)
- fixedpos-static-pos-with-viewport-cb-002.html (live test) (source)
- fixedpos-static-pos-with-viewport-cb-003.html (live test) (source)
- multicol-height-block-child-001.xht (live test) (source)
注意:无法在列框上设置属性/值。 例如, 无法设置某个列框的背景, 并且列框没有填充、边距或边框的概念。 未来的规范可能会增加额外的功能。 例如, 可能支持不同宽度和不同背景的列。
注意:列高大于视口的多列容器可能会带来可访问性问题。 有关更多详细信息, 请参阅可访问性注意事项。
3. 列数和列宽
在布局多列内容时,确定列数和列宽是基本步骤。 以下属性用于设置列数和列宽:
第三个属性,columns, 是一个简写属性, 它同时设置column-width和column-count。
其他因素,如显式的列分隔符、内容和高度限制, 也可能影响实际的列数和列宽。
3.1. 列的内联尺寸:column-width属性
名称: | column-width |
---|---|
值: | auto | <length [0,∞]> |
初始值: | auto |
应用于: | 块容器,但不包括表包装框 |
是否继承: | 否 |
百分比: | 不适用 |
计算值: | 关键词auto或绝对长度 |
规范顺序: | 根据语法 |
动画类型: | 按计算值类型 |
此属性描述了多列容器中列的宽度。
- auto
- 表示列宽将由其他属性决定 (例如,如果column-count有一个非auto值)。
- <length [0,∞]>
- 描述了最佳列宽。 实际列宽可能会更宽(以填充可用空间), 也可能更窄(仅当可用空间小于指定的列宽时)。 不允许负值。 使用的值将被限制在至少1px。
测试
- multicol-basic-003.html (live test) (source)
- multicol-basic-008.xht (live test) (source)
- multicol-reduce-000.xht (live test) (source)
- multicol-width-001.xht (live test) (source)
- multicol-width-002.xht (live test) (source)
- multicol-width-003.xht (live test) (source)
- multicol-width-005.html (live test) (source)
- multicol-width-ch-001.xht (live test) (source)
- multicol-width-negative-001.xht (live test) (source)
- multicol-width-invalid-001.xht (live test) (source)
- multicol-width-large-001.xht (live test) (source)
- multicol-width-large-002.xht (live test) (source)
- multicol-inherit-003.xht (live test) (source)
- column-width-computed.html (live test) (source)
- column-width-invalid.html (live test) (source)
- column-width-valid.html (live test) (source)
- multicol-width-small-001.xht (live test) (source)
div{ width : 100 px ; column-width : 45 px ; column-gap : 0 ; column-rule : none; }
在这个100px宽的元素内,有足够的空间放置两列45px宽的列。 为了填满可用空间,实际列宽将增加到50px。
div{ width : 40 px ; column-width : 45 px ; column-gap : 0 ; column-rule : none; }
可用空间小于指定的列宽,因此实际的列宽将会减少。
为了确保column-width可以用于竖排文本, 列宽表示列内行框的长度。
注意: 使column-width具有一定灵活性的原因 是为了实现可以适应多种屏幕尺寸的可扩展设计。 要设置精确的列宽, 还必须指定列间距和多列容器的宽度(假设是水平文本)。
3.2. 列数:column-count 属性
名称: | column-count |
---|---|
值: | auto | <integer [1,∞]> |
初始值: | auto |
适用于: | 块级容器,除了 表包装盒 |
是否继承: | 否 |
百分比: | 不适用 |
计算值: | 指定值 |
规范顺序: | 按语法 |
动画类型: | 按计算值类型 |
该属性描述了多列容器的列数。
- auto
- 表示列数将由其他属性决定 (例如,column-width 如果其具有非 auto 值)。
- <integer [1,∞]>
- 描述了将内容流入元素的最佳列数。值必须大于 0。 如果 column-width 和 column-count 都具有非 auto 值,整数值描述最大列数。
测试
- multicol-count-001.xht (live test) (source)
- multicol-count-002.xht (live test) (source)
- multicol-basic-006.xht (live test) (source)
- multicol-width-count-001.xht (live test) (source)
- multicol-width-count-002.xht (live test) (source)
- multicol-columns-toolong-001.xht (live test) (source)
- multicol-count-negative-001.xht (live test) (source)
- multicol-count-negative-002.xht (live test) (source)
- multicol-count-non-integer-001.xht (live test) (source)
- multicol-count-non-integer-002.xht (live test) (source)
- multicol-count-non-integer-003.xht (live test) (source)
- multicol-inherit-001.xht (live test) (source)
- multicol-inherit-002.xht (live test) (source)
- column-count-invalid.html (live test) (source)
- column-count-valid.html (live test) (source)
- column-count-computed.html (live test) (source)
- column-count-interpolation.html (live test) (source)
- large-actual-column-count.html (live test) (source)
- with-custom-layout-on-same-element.https.html (live test) (source)
3.3. column-width 和 column-count 的简写: columns 属性
名称: | columns |
---|---|
值: | <'column-width'> || <'column-count'> |
初始值: | 见各个属性 |
适用于: | 见各个属性 |
是否继承: | 见各个属性 |
百分比: | 见各个属性 |
计算值: | 见各个属性 |
动画类型: | 见各个属性 |
规范顺序: | 按语法 |
这是一个用于设置 column-width 和 column-count 的简写属性。省略的值将被设置为它们的初始值。
columns : 12 em ; /* column-width: 12em; column-count: auto */ columns: auto12 em ; /* column-width: 12em; column-count: auto */ columns:2 ; /* column-width: auto; column-count: 2 */ columns:2 auto; /* column-width: auto; column-count: 2 */ columns: auto; /* column-width: auto; column-count: auto */ columns: auto auto; /* column-width: auto; column-count: auto */
测试
- multicol-columns-001.xht (live test) (source)
- multicol-columns-002.xht (live test) (source)
- multicol-columns-003.xht (live test) (source)
- multicol-columns-004.xht (live test) (source)
- multicol-columns-005.xht (live test) (source)
- multicol-columns-006.xht (live test) (source)
- multicol-columns-007.xht (live test) (source)
- multicol-columns-invalid-001.xht (live test) (source)
- multicol-columns-invalid-002.xht (live test) (source)
- multicol-basic-001.html (live test) (source)
- multicol-basic-003.html (live test) (source)
- multicol-basic-005.xht (live test) (source)
- multicol-basic-007.xht (live test) (source)
- columns-invalid.html (live test) (source)
- columns-valid.html (live test) (source)
3.4. 伪算法
下面的伪算法确定了 column-count (N) 和 column-width (W) 的使用值。伪算法中还有一个变量:U 是多列容器的使用宽度。
注意: 多列容器的使用宽度 U 可能依赖于元素的内容, 此时它还依赖于 column-count 和 column-width 属性的计算值。本规范未定义如何计算 U。另一个模块(可能是基本框模型 [CSS3BOX] 或框尺寸模块 [CSS3-SIZING])预计将定义这一点。
测试
- intrinsic-size-001.html (live test) (source)
- intrinsic-size-002.html (live test) (source)
- intrinsic-size-003.html (live test) (source)
- intrinsic-size-004.html (live test) (source)
- intrinsic-size-005.html (live test) (source)
- as-column-flex-item.html (live test) (source)
- intrinsic-width-change-column-count.html (live test) (source)
floor(X)
函数返回小于等于 X 的最大整数 Y。
(01) if ((column-width = auto) and (column-count = auto)) then (02) exit; /* 不是多列容器 */ (03) if column-width = auto then (04) N := column-count (05) else if column-count = auto then (06) N := max(1, (07) floor((U + column-gap)/(column-width + column-gap))) (08) else (09) N := min(column-count, max(1, (10) floor((U + column-gap)/(column-width + column-gap))))
以及:
(11) W := max(0, ((U + column-gap)/N - column-gap))
为了找到自动重复列的数量,UA 必须将列的大小舍入到 UA 指定的值,以避免除以零。建议这个值舍入到 1px 或更小。
在分片上下文中,例如 分页媒体,用户代理可以基于每个片段执行此计算。
column-count 的使用值是在不考虑显式列分隔或受限列高的情况下计算的,而实际值会考虑这些因素。
div{ width : 40 em ; columns : 20 em ; column-gap : 0 ; } p{ break-after : column; }
< div > < p > one< p > two< p > three</ div >
div{ width : 80 em ; height : 10 em ; columns : 20 em ; column-gap : 0 ; column-fill : auto; }
< div > foo</ div >
计算出的 column-count 为 auto,使用的 column-count 为 4,而实际的 column-count 为 1。
3.5. 层叠上下文
多列容器中的所有列框都位于相同的层叠上下文中,其内容的绘制顺序如 CSS 2.1 中所规定。 列框不会建立新的层叠上下文。
4. 列间距和列规则
列间距和列规则放置在同一 多列容器 的列之间。 列间距和列规则的长度等于列高。 列间距占据空间。 也就是说,列间距会推开相邻列中的内容(在同一 多列容器 内)。
测试
- multicol-height-001.xht (live test) (source)
- multicol-nested-column-rule-001.xht (live test) (source)
- multicol-nested-column-rule-002.html (live test) (source)
- multicol-nested-column-rule-003.html (live test) (source)
- multicol-rule-nested-balancing-001.html (live test) (source)
- multicol-rule-nested-balancing-002.html (live test) (source)
- multicol-rule-nested-balancing-003.html (live test) (source)
- multicol-rule-nested-balancing-004.html (live test) (source)
一个 列规则 在 列间距 的中间绘制,端点位于 多列容器 的相对内容边缘。列规则不占用空间。也就是说,列规则 的存在或厚度不会改变其他任何元素的位置。如果 列规则 比它的间距宽,相邻的列框将会重叠该规则,并且规则可能会扩展到 多列容器 的框之外。列规则绘制在 多列容器 的边框正上方。对于可滚动的多列容器,请注意,虽然 多列容器 的边框和背景显然不会滚动,但规则需要随着列一起滚动。列规则只绘制在两个都有内容的列之间。
测试
基本列规则测试
- multicol-rule-003.xht (live test) (source)
- multicol-rule-004.xht (live test) (source)
- multicol-rule-fraction-002.xht (live test) (source)
如果列规则比它的间距宽,相邻的框会重叠。
- multicol-rule-001.xht (live test) (source)
- multicol-rule-large-001.xht (live test) (source)
- multicol-rule-large-002.xht (visual test) (source)
列规则仅绘制在有内容的两列之间。
- multicol-count-computed-003.xht (live test) (source)
- multicol-count-computed-005.xht (live test) (source)
- broken-column-rule-1.html (live test) (source)
测试背景和列规则的行为。
- multicol-breaking-000.html (live test) (source)
- multicol-breaking-001.html (live test) (source)
- multicol-breaking-002.html (live test) (source)
- multicol-breaking-003.html (live test) (source)
- multicol-breaking-004.html (live test) (source)
- multicol-breaking-005.html (live test) (source)
- multicol-breaking-006.html (live test) (source)
- multicol-breaking-nobackground-000.html (live test) (source)
- multicol-breaking-nobackground-001.html (live test) (source)
- multicol-breaking-nobackground-002.html (live test) (source)
- multicol-breaking-nobackground-003.html (live test) (source)
- multicol-breaking-nobackground-004.html (live test) (source)
- multicol-breaking-nobackground-005.html (live test) (source)
4.1. 列之间的排水沟:column-gap 属性
column-gap 属性在[CSS3-ALIGN]中定义。
在 多列格式化上下文中,normal 对 column-gap 属性的使用值为 1em。 这确保了在使用初始值时列是可读的。 如果列之间有列规则, 它将出现在间隙的中间。
测试
- multicol-gap-fraction-001.xht (live test) (source)
- multicol-gap-fraction-002.html (live test) (source)
- multicol-gap-large-001.xht (live test) (source)
- multicol-gap-large-002.xht (live test) (source)
- multicol-gap-negative-001.xht (live test) (source)
- multicol-gap-000.xht (live test) (source)
- multicol-gap-002.xht (live test) (source)
- multicol-gap-percentage-001.html (live test) (source)
- multicol-gap-001.xht (live test) (source)
- multicol-gap-003.xht (live test) (source)
测试间隙是否可动画化。
- multicol-gap-animation-001.html (live test) (source)
- multicol-gap-animation-002.html (live test) (source)
- multicol-gap-animation-003.html (live test) (source)
4.2. 列规则的颜色:column-rule-color 属性
名称: | column-rule-color |
---|---|
值: | <color> |
初始值: | currentcolor |
应用于: | 多列容器 |
是否继承: | 否 |
百分比: | N/A |
计算值: | 计算后的颜色 |
规范顺序: | 按语法 |
动画类型: | 按计算值类型 |
- <color>
- 指定 列规则 的颜色。
测试
- multicol-rule-color-001.xht (live test) (source)
- multicol-rule-color-inherit-001.xht (live test) (source)
- multicol-rule-color-inherit-002.xht (live test) (source)
- column-rule-color-computed.html (live test) (source)
- column-rule-color-valid.html (live test) (source)
- column-rule-color-invalid.html (live test) (source)
- column-rule-color-interpolation.html (live test) (source)
4.3. 列规则的样式:column-rule-style 属性
名称: | column-rule-style |
---|---|
值: | <line-style> |
初始值: | none |
应用于: | 多列容器 |
是否继承: | 否 |
百分比: | N/A |
计算值: | 指定的关键词 |
规范顺序: | 按语法 |
动画类型: | 离散 |
column-rule-style 属性设置元素列之间的规则样式。 <line-style> 的值如同 折叠边框模型 中的解释。
测试
none 值会强制将 column-rule-width 的计算值设为 0。
4.4. 列规则的宽度:column-rule-width 属性
名称: | column-rule-width |
---|---|
值: | <line-width> |
初始值: | medium |
应用于: | 多列容器 |
是否继承: | 否 |
百分比: | N/A |
计算值: | 绝对长度,作为边框宽度捕捉;如果列规则样式为 none 或 ,则为 0 |
规范顺序: | 按语法 |
动画类型: | 按计算值类型 |
该属性设置列之间的规则宽度。负值不被允许。
测试
- multicol-rule-fraction-001.xht (live test) (source)
- multicol-rule-fraction-003.xht (live test) (source)
- multicol-rule-px-001.xht (live test) (source)
- multicol-rule-percent-001.xht (live test) (source)
- subpixel-column-rule-width.tentative.html (live test) (source)
- column-rule-width-computed.html (live test) (source)
- column-rule-width-invalid.html (live test) (source)
- column-rule-width-valid.html (live test) (source)
- column-rule-width-interpolation.html (live test) (source)
4.5. 列规则简写: column-rule 属性
名称: | column-rule |
---|---|
值: | <'column-rule-width'> || <'column-rule-style'> || <'column-rule-color'> |
初始值: | 查看各个属性 |
应用于: | 查看各个属性 |
是否继承: | 查看各个属性 |
百分比: | 查看各个属性 |
计算值: | 查看各个属性 |
动画类型: | 查看各个属性 |
规范顺序: | 按语法 |
该属性是 column-rule-width, column-rule-style, 和 column-rule-color 的简写。省略的值将设置为它们的初始值。
测试
- multicol-shorthand-001.xht (live test) (source)
- multicol-rule-shorthand-001.xht (live test) (source)
- multicol-rule-shorthand-2.xht (live test) (source)
- multicol-rule-000.xht (live test) (source)
- multicol-rule-002.xht (live test) (source)
- multicol-rule-dashed-000.xht (live test) (source)
- multicol-rule-dotted-000.xht (live test) (source)
- multicol-rule-double-000.xht (live test) (source)
- multicol-rule-outset-000.xht (live test) (source)
- multicol-rule-none-000.xht (live test) (source)
- multicol-rule-hidden-000.xht (live test) (source)
- multicol-rule-inset-000.xht (live test) (source)
- multicol-rule-groove-000.xht (live test) (source)
- multicol-rule-ridge-000.xht (live test) (source)
- multicol-rule-solid-000.xht (live test) (source)
- column-rule-computed.html (live test) (source)
- column-rule-invalid.html (live test) (source)
- column-rule-valid.html (live test) (source)
- column-rule-shorthand.html (live test) (source)
body{ column-gap : 35 px ; column-rule-width : 35 px ; column-rule-style : solid; column-rule-color : black; }
5. 列断点
当内容布局为多列时, 用户代理必须确定列断点的位置。 将内容分成多列的问题类似于将内容分页, 该过程在 CSS 2.1 第 13.3.3 节中描述 [CSS21]。
引入了三个新属性,以便在与页面断点相同的属性中描述列断点: break-before、break-after 和 break-inside。
5.1. 控制分段: break-before、break-after、break-inside 属性
break-before、break-after 和 break-inside 在 [CSS3-BREAK] 中定义。
测试
- multicol-break-000.xht (live test) (source)
- multicol-break-001.xht (live test) (source)
- multicol-br-inside-avoidcolumn-001.xht (live test) (source)
- moz-multicol3-column-balancing-break-inside-avoid-1.html (manual test) (source)
6. 跨列
column-span 属性使元素可以跨越多个列。
6.1. 跨列显示元素: column-span 属性
名称: | column-span |
---|---|
值: | none | all |
初始值: | none |
适用对象: | 流内的块级元素 |
是否继承: | 否 |
百分比: | N/A |
计算值: | 指定的关键字 |
规范顺序: | 根据语法 |
动画类型: | 离散 |
此属性描述一个元素跨越多少列。值为:
- none
- 元素不会跨越多列。
- all
-
元素会强制生成列断点,并被从流中移出以跨越最近的具有多列祖先的所有列
同时属于相同的 块格式化上下文。
正常流中的内容在元素之前自动均衡地分布在所有列中
在元素出现之前的立即前一多列行,并且任何后续内容都将在元素之后流入新的 多列行。
该元素建立一个独立的格式化上下文。
注意:元素是否建立一个新的 格式化上下文,不取决于该元素是否为多列的后代。 当 column-span 为 all 时,它总是会这样做。 这有助于在设计中消除多列时保持鲁棒性, 或当媒体查询在某些情况下关闭多列时。
测试
- multicol-span-000.xht (live test) (source)
- multicol-span-all-001.xht (live test) (source)
- multicol-span-all-003.xht (live test) (source)
- multicol-span-all-block-sibling-003.xht (live test) (source)
- multicol-span-all-margin-001.xht (live test) (source)
- multicol-span-all-margin-002.xht (live test) (source)
- multicol-span-all-margin-003.html (live test) (source)
- multicol-span-all-margin-bottom-001.xht (live test) (source)
- multicol-span-all-margin-nested-001.xht (live test) (source)
- multicol-span-all-margin-nested-002.xht (live test) (source)
- multicol-span-all-margin-nested-firstchild-001.xht (live test) (source)
- multicol-span-float-001.xht (live test) (source)
- multicol-span-float-002.html (live test) (source)
- multicol-span-float-003.html (live test) (source)
- inline-block-and-column-span-all.html (live test) (source)
- multicol-span-all-dynamic-remove-001.html (live test) (source)
- multicol-span-all-dynamic-add-001.html (live test) (source)
- multicol-span-all-dynamic-remove-002.html (live test) (source)
- multicol-span-all-dynamic-add-002.html (live test) (source)
- multicol-span-all-dynamic-remove-003.html (live test) (source)
- multicol-span-all-dynamic-add-003.html (live test) (source)
- multicol-span-all-dynamic-remove-004.html (live test) (source)
- multicol-span-all-dynamic-add-004.html (live test) (source)
- multicol-span-all-dynamic-add-005.html (live test) (source)
- multicol-span-all-dynamic-remove-005.html (live test) (source)
- multicol-span-all-dynamic-remove-006.html (live test) (source)
- multicol-span-all-dynamic-add-006.html (live test) (source)
- multicol-span-all-dynamic-remove-007.html (live test) (source)
- multicol-span-all-dynamic-add-007.html (live test) (source)
- multicol-span-all-dynamic-add-008.html (live test) (source)
- multicol-span-all-dynamic-add-009.html (live test) (source)
- multicol-span-all-dynamic-add-010.html (live test) (source)
- multicol-span-all-dynamic-add-011.html (live test) (source)
- multicol-span-all-dynamic-add-012.html (live test) (source)
- multicol-span-all-dynamic-add-013.html (live test) (source)
- multicol-span-all-children-height-001.html (live test) (source)
- multicol-span-all-children-height-002.html (live test) (source)
- multicol-span-all-children-height-003.html (live test) (source)
- multicol-span-all-children-height-004a.html (live test) (source)
- multicol-span-all-children-height-004b.html (live test) (source)
- multicol-span-all-children-height-005.html (live test) (source)
- multicol-span-all-children-height-006.html (live test) (source)
- multicol-span-all-children-height-007.html (live test) (source)
- multicol-span-all-children-height-008.html (live test) (source)
- multicol-span-all-children-height-009.html (live test) (source)
- multicol-span-all-children-height-010.html (live test) (source)
- multicol-span-all-children-height-011.html (live test) (source)
- multicol-span-all-children-height-012.html (live test) (source)
- multicol-span-all-children-height-013.html (live test) (source)
- multicol-span-all-004.html (live test) (source)
- multicol-span-all-005.html (live test) (source)
- multicol-span-all-006.html (live test) (source)
- multicol-span-all-007.html (live test) (source)
- multicol-span-all-008.html (live test) (source)
- multicol-span-all-009.html (live test) (source)
- multicol-span-all-010.html (live test) (source)
- multicol-span-all-011.html (live test) (source)
- multicol-span-all-012.html (live test) (source)
- multicol-span-all-013.html (live test) (source)
- multicol-span-all-014.html (live test) (source)
- multicol-span-all-015.html (live test) (source)
- multicol-span-all-016.html (live test) (source)
- multicol-span-all-017.html (live test) (source)
- multicol-span-all-018.html (live test) (source)
- multicol-span-all-019.html (live test) (source)
- multicol-span-all-rule-001.html (live test) (source)
- multicol-span-all-button-001.html (live test) (source)
- multicol-span-all-button-002.html (live test) (source)
- multicol-span-all-button-003.html (live test) (source)
- multicol-span-all-fieldset-001.html (live test) (source)
- multicol-span-all-fieldset-002.html (live test) (source)
- multicol-span-all-fieldset-003.html (live test) (source)
- multicol-span-all-restyle-001.html (live test) (source)
- multicol-span-all-restyle-002.html (live test) (source)
- multicol-span-all-restyle-003.html (live test) (source)
- multicol-span-all-restyle-004.html (live test) (source)
- multicol-span-all-list-item-001.html (live test) (source)
- multicol-span-all-list-item-002.html (live test) (source)
- float-with-line-after-spanner.html (live test) (source)
- parallel-flow-after-spanner-001.html (live test) (source)
- parallel-flow-after-spanner-002.html (live test) (source)
- margin-and-break-before-child-spanner.html (live test) (source)
- multicol-width-004.html (live test) (source)
跨越多列的元素称为 多列跨越元素,它创建的框称为 多列跨越框。
跨越框的 包含块是 多列容器 本身。 因此,如果跨越框本身没有为其中的 绝对定位的框建立 包含块, 那么它们的 包含块链会直接跳过 多列容器,略过 跨越框和 多列容器之间的任何祖先元素。
虽然跨越框被 从流中移出, 但这不会影响跨越元素的 绘制顺序 [CSS21]。
h2
元素已被添加到示例文档中的第六个句子之后
(即在 “the leg of a” 之后)。
应用了以下样式:
h2{ column-span : all; background : silver}
通过将 column-span 设置为 all,
所有出现在 h2
元素之前的内容都显示在 h2
元素上方。
跨越元素可能低于第一级后代 只要它们属于同一个 格式化上下文, 并且在跨越元素和 多列容器之间没有任何元素为固定定位的后代建立包含块。
< article > < section > < div class = "spanner" > 尝试创建跨越框</ div > </ section > </ article >
article{ columns : 2 ; } section{ transform : rotate ( 90 deg ); } .spanner{ column-span : all; background : silver; }
测试
如果跨越框之前的片段是空的,则不会发生特殊情况; 顶部的边距/边框/内边距位于跨越元素的上方,作为一个空的 片段。
article
元素。
在此父元素内是一个段落和一个 section 元素。
section 包含一个 h2
标题,设置为 all,这跨越了所有三列,
而包含的 section 仍然在列框中。
h2
是 section 的第一个子元素。
这意味着该 section 的边距、
边框(在图中以红色显示)
和内边距出现在跨越的 h2
之前,作为一个空片段。
< article > < p > ...</ p > < section > < h2 > An h2 element</ h2 > < p > ...</ p > </ section > </ article >
section{ border : 2 px solid red; margin-top : 65 px ; padding-top : 20 px ; } h2{ column-span : all; background : silver}
h2
元素设置为 column-span: all,
section 有红色边框和顶部内边距及边距。测试
- spanner-fragmentation-000.html (live test) (source)
- spanner-fragmentation-001.html (live test) (source)
- spanner-fragmentation-002.html (live test) (source)
- spanner-fragmentation-003.html (live test) (source)
- spanner-fragmentation-004.html (live test) (source)
- spanner-fragmentation-005.html (live test) (source)
- spanner-fragmentation-006.html (live test) (source)
- spanner-fragmentation-007.html (live test) (source)
- spanner-fragmentation-008.html (live test) (source)
- spanner-fragmentation-009.html (live test) (source)
- spanner-fragmentation-010.html (live test) (source)
- spanner-fragmentation-011.html (live test) (source)
- spanner-fragmentation-012.html (live test) (source)
跨越元素占用的空间比元素本身占用的空间更多。 当空间有限时,可能无法找到足够的空间放置跨越元素。 在这种情况下,用户代理可能会将该元素视为已在此属性上指定了 none。
h2
元素出现在内容的后面,
并且多列容器的高度受到限制。
因此,h2
元素出现在溢出部分,
并且没有足够的空间让该元素跨越列。
结果,该元素表现得就像指定了 column-span: none。
跨越元素是块级盒子, 因此两个相邻跨越元素的边距会相互折叠。 如果两个跨越元素之间仅由绝对定位项目隔开,它们的边距也会相互折叠, 因为绝对定位项目不会创建列盒子。 由于列盒子建立了一个新的 块格式化上下文, 因此列盒子内元素的边距不会与跨越元素的边距折叠。
测试
h2{ margin : 16 px 0 ; column-span : all; background : silver} p{ margin-top : 16 px }
测试
与跨越元素相关的附加测试。
- abspos-in-multicol-with-spanner-crash.html (live test) (source)
- body-becomes-spanner-html-becomes-vertical-rl.html (live test) (source)
- fit-content-with-spanner-and-auto-scrollbar-sibling.html (live test) (source)
- float-becomes-non-float-spanner-surprises-inside.html (live test) (source)
- float-becomes-spanner.html (live test) (source)
- multicol-floats-after-column-span-crash.html (live test) (source)
- negative-margin-on-column-spanner.html (live test) (source)
- nested-spanner-with-negative-margin.html (live test) (source)
- oof-becomes-spanner.html (live test) (source)
- oof-in-additional-column-before-spanner.html (live test) (source)
- relpos-spanner-with-spanner-child-becomes-regular.html (live test) (source)
- remove-spanner-after-spanner-in-inline-before-inline.html (live test) (source)
- remove-spanner-in-table-caption-nested-multicol.html (live test) (source)
- restricted-height-bottom-border-overflow-and-spanner.html (live test) (source)
- scrollable-spanner-in-nested.html (live test) (source)
- spanner-after-parallel-flow.html (live test) (source)
- spanner-in-inline-after-very-tall-content-001.html (live test) (source)
- spanner-in-inline-after-very-tall-content-002.html (live test) (source)
- spanner-in-overflowed-container-before-float.html (live test) (source)
- nested-with-tall-padding-and-spanner-and-content.html (live test) (source)
- specified-height-with-just-spanner-and-oof.html (live test) (source)
- trailing-parent-padding-between-spanners.html (live test) (source)
- table-caption-change-descendant-display-type.html (live test) (source)
- table-caption-inline-block-remove-child.html (live test) (source)
- remove-block-beside-spanner-in-inline-crash.html (live test) (source)
- remove-inline-with-block-beside-spanners-crash.html (live test) (source)
- remove-spanner-beside-spanner-in-inline-crash.html (live test) (source)
- spanning-legend-000-crash.html (live test) (source)
- spanning-legend-001-crash.html (live test) (source)
- toggle-spanner-float-crash.html (live test) (source)
- abspos-after-spanner-static-pos.html (live test) (source)
- abspos-after-spanner.html (live test) (source)
- abspos-containing-block-outside-spanner.html (live test) (source)
- change-transform-in-spanner.html (live test) (source)
- nested-with-padding-and-spanner.html (live test) (source)
- orthogonal-writing-mode-spanner.html (live test) (source)
- remove-inline-with-block-beside-spanners.html (live test) (source)
- replaced-content-spanner-auto-width.html (live test) (source)
- spanner-in-child-after-parallel-flow-001.html (live test) (source)
- spanner-in-child-after-parallel-flow-002.html (live test) (source)
- spanner-in-child-after-parallel-flow-003.html (live test) (source)
- spanner-in-child-after-parallel-flow-004.html (live test) (source)
- spanner-in-opacity.html (live test) (source)
- going-out-of-flow-after-spanner.html (live test) (source)
- inline-with-spanner-in-overflowed-container-before-multicol-float.html (live test) (source)
- spanner-in-overflowed-clipped-container.html (live test) (source)
- spanner-in-overflowed-container-before-inline-content.html (live test) (source)
- spanner-inside-inline-in-overflowed-container.html (live test) (source)
- column-span-valid.html (live test) (source)
- column-span-computed.html (live test) (source)
- getclientrects-000.html (live test) (source)
- getclientrects-001.html (live test) (source)
7. 填充列
填充列有两种策略: 列可以平衡填充,也可以不平衡填充。 如果列是平衡的,用户代理应尽量减少列高的差异, 同时遵守强制中断、widows 和 orphans, 以及其他可能影响列高的属性。 如果列不平衡填充,则它们会按顺序填充; 一些列可能只部分填充,或没有任何内容。
7.1. 列平衡:column-fill 属性
名称: | column-fill |
---|---|
值: | auto | balance | balance-all |
初始值: | balance |
适用于: | 多列容器 |
是否继承: | 否 |
百分比: | 不适用 |
计算值: | 指定的关键字 |
规范顺序: | 根据语法 |
动画类型: | 离散 |
此属性指定在不立即位于 跨列元素 之前的 多列行中的内容是否平衡填充列。
测试
可用的值包括:
- balance
-
尽可能在各列之间均衡内容。
在分段上下文中,只有最后一个片段是平衡的。
测试
- multicol-fill-000.xht (live test) (source)
- multicol-fill-001.xht (live test) (source)
- column-fill-invalid.html (live test) (source)
- column-fill-valid.html (live test) (source)
- column-fill-computed.html (live test) (source)
- column-fill-balance-orthog-block-001.html (live test) (source)
- column-balancing-paged-001-print.html (live test) (source)
- multicol-fill-balance-001.xht (live test) (source)
- multicol-fill-balance-002.html (live test) (source)
- multicol-fill-balance-003.html (live test) (source)
- multicol-fill-balance-004.html (live test) (source)
- multicol-fill-balance-005.html (live test) (source)
- multicol-fill-balance-006.html (live test) (source)
- multicol-fill-balance-007.html (live test) (source)
- multicol-fill-balance-008.html (live test) (source)
- multicol-fill-balance-009.html (live test) (source)
- multicol-fill-balance-010.html (live test) (source)
- multicol-fill-balance-011.html (live test) (source)
- multicol-fill-balance-012.html (live test) (source)
- multicol-fill-balance-013.html (live test) (source)
- multicol-fill-balance-014.html (live test) (source)
- multicol-fill-balance-015.html (live test) (source)
- multicol-fill-balance-016.html (live test) (source)
- multicol-fill-balance-018.html (live test) (source)
- multicol-fill-balance-019.html (live test) (source)
- multicol-fill-balance-020.html (live test) (source)
- multicol-fill-balance-021.html (live test) (source)
- multicol-fill-balance-022.html (live test) (source)
- multicol-fill-balance-023.html (live test) (source)
- multicol-fill-balance-024.html (live test) (source)
- multicol-fill-balance-025.html (live test) (source)
- multicol-fill-balance-026.html (live test) (source)
- multicol-fill-balance-027.html (live test) (source)
- multicol-fill-balance-028.html (live test) (source)
- multicol-fill-balance-nested-000.html (live test) (source)
- balance-all
- 尽可能在各列之间均衡内容。 在分段上下文中,所有片段都保持平衡。
- auto
- 按顺序填充列
测试
- multicol-fill-auto-001.xht (live test) (source)
- multicol-fill-auto-002.xht (live test) (source)
- multicol-fill-auto-003.xht (live test) (source)
- multicol-fill-auto-004.html (live test) (source)
- multicol-fill-auto-block-children-001.xht (live test) (source)
- multicol-fill-auto-block-children-002.xht (live test) (source)
- multicol-fill-auto-block-children-003.html (live test) (source)
- columnfill-auto-max-height-001.html (live test) (source)
- columnfill-auto-max-height-002.html (live test) (source)
- columnfill-auto-max-height-003.html (live test) (source)
在连续上下文中,当存在溢出列时,此属性不会产生任何效果。
article{ width : 60 em ; height : auto; columns : 4 ; column-fill : balance; }
article{ width : 60 em ; height : 4 em ; columns : 4 ; column-fill : auto; }
结果,第一列填充了所有内容:
article{ width : 60 em ; height : auto; columns : 4 ; column-fill : balance; } p{ break-after : column; }
最短的列高度包含五行文本。 确定列高度后,按顺序填充列。 因此,第三列与前两列一样高, 而最后一列则显著较短。
article{ width : 60 em ; height : auto; columns : 4 ; column-fill : balance; }
在此示例中,文章以一个不可分割的图像开始,这确定了列的高度。 随后的内容按顺序填充到其余的列中:
测试
与流外元素结合的列平衡相关测试。
- column-balancing-with-span-and-oof-001.html (live test) (source)
- column-balancing-with-span-and-oof-002.html (live test) (source)
- forced-break-in-oof-in-column-balancing-nested.html (live test) (source)
- forced-break-in-oof-in-column-balancing.html (live test) (source)
其他与平衡相关的测试
- balance-extremely-tall-monolithic-content-crash.html (live test) (source)
- column-balancing-with-overflow-auto-crash.html (live test) (source)
- balance-break-avoidance-000.html (live test) (source)
- balance-break-avoidance-001.html (live test) (source)
- balance-break-avoidance-002.html (live test) (source)
- balance-grid-container.html (live test) (source)
- balance-orphans-widows-000.html (live test) (source)
- balance-with-forced-break.html (live test) (source)
- balancing-flex-item-trailing-margin-freeze.html (live test) (source)
- balancing-tall-borders-freeze.html (live test) (source)
8. 溢出
8.1. 多列容器内部的溢出
除了可能导致列分割的情况,扩展到列框外的内容 仍然会可见溢出,并且不会被列框裁剪。
注意:请参阅§ 5 列分割, 以及§ 8.2 分页与多列容器外的溢出了解是否裁剪至多列容器的内容框。
测试
- multicol-block-no-clip-001.xht (live test) (source)
- multicol-block-no-clip-002.xht (live test) (source)
- multicol-clip-001.xht (live test) (source)
- multicol-clip-002.xht (live test) (source)
- multicol-clip-scrolled-content-001.html (live test) (source)
- multicol-overflow-clip-auto-sized.html (live test) (source)
- multicol-overflow-clip-positioned.html (live test) (source)
- multicol-overflow-clip.html (live test) (source)
- relative-child-overflowing-column-gap.html (live test) (source)
- relative-child-overflowing-container.html (live test) (source)
8.2. 分页与多列容器外的溢出
多列容器边缘超出列框的内容和列规则会根据overflow属性进行裁剪。
由于以下原因,多列容器可能包含比实际容纳空间更多的列:
- 使用声明约束了列的高度 (例如,使用height或 max-height)。 在这种情况下,会在内联方向创建额外的列框。
- 页面的尺寸。 在这种情况下,额外的列框会移动到下一页。
- 明确的列分割。 在这种情况下,额外的列框将在连续上下文中创建于内联方向,对于分段媒体,会移动到下一个片段。
测试
测试以检查可滚动容器是否未在多个列中拆分。
- overflow-unsplittable-001.html (live test) (source)
- overflow-unsplittable-002.html (live test) (source)
- overflow-unsplittable-003.html (live test) (source)
出现在多列容器之外的列在连续上下文中被称为溢出列。 溢出列可能会影响多列容器的高度。
div{ max-height : 5 em ; overflow : visible; }
因此,列的数量增加了。
假设列平衡,第二页将显示如下内容:
这将在第二页显示如下内容:
由于列平衡,最后一段被分割在三列之间。
附录 B. 更改
本附录为说明性。
2021年10月12日的候选推荐标准 (CR)中的更改
- 添加了文本“且在跨列元素与多列容器之间没有建立固定定位后代的包含块的内容。”2022年3月9日解决
2021年2月12日的工作草案 (WD)中的更改
- 添加了文本“多列容器的流外后代会影响列的平衡,以及多列容器的块尺寸。”2021年5月12日解决
- 添加了文本“两个跨列元素之间仅隔着绝对定位元素时,它们的边距将相互折叠,因为绝对定位元素不会创建列框。”2021年5月12日解决
- 添加了无障碍访问性考虑部分。APA审查后的请求。
- 添加了注释和示例,以明确垂直书写模式下列布局的方式。i18n WG审查后的请求。
2019年10月15日的工作草案 (WD)中的更改
- 添加了文本“跨列元素为其内部的绝对定位框创建包含块,除非跨列元素没有建立包含块,否则包含块链会直接指向多列容器。”2020年10月23日解决
- 添加了文本“该属性指定内容是否在不紧邻跨列元素的多列行中平衡。”2020年4月29日解决
2018年5月28日的工作草案 (WD)中的更改
- 删除了非规范性文本“然而,如下所述,设置宽度和列数同时设置的情况很少有意义。” 编辑,2019年9月16日,问题 4291。
- 添加了段落“跨列元素是块级框,因此两个相邻的跨列元素之间的边距会相互折叠。由于列框建立了新的块格式化上下文,因此列框内元素的边距不会与跨列元素的边距折叠。”2018年10月22日解决,另见问题 2582的解决方案。
- 对规范进行了澄清,解释了跨列元素是流外的,留下一个强制中断。添加了段落“跨列元素是流外的,留下一个强制中断。这不会影响跨列元素的绘制顺序。”2019年2月28日解决。
- 将column-gap属性的定义移动到[CSS3-ALIGN]中,并添加了有关多列中的column-gap的具体细节的段落:
“在多列格式化上下文中,column-gap属性的normal的使用值为1em。这确保了在使用初始值时列的可读性。如果列间有列规则,它将出现在间隙的中间。”2019年6月4日解决。 - 根据Morten Stenshorne在问题2203中的输入,重新措辞了多列模型部分。
- 删除了column-gap长度-百分比值的风险标记。2019年6月4日解决。
- 更新了介绍,删除了关于使用表格布局的多列优势的提及,转而引用了多列的独特特性。编辑变更参考了问题3654。
- 更改了2016年1月7日解决后伪算法部分中添加的句子,改为指“列”而非“轨道”,因为轨道未在本规范中定义。2019年3月13日解决。
- 对规范中使用的SVG图像进行了更改和澄清。
- 更改了语法以使用括号范围表示法,以反映对负值/非零值的限制。
2017年10月5日的工作草案 (WD)中的更改
- 将关于分页媒体的引用更改为指碎片化上下文。2018年4月12日解决。
- 更改了关于
column-fill
属性的行:在连续媒体中,此属性在溢出列中没有任何效果。
更改为:在连续媒体中,当存在溢出列时,此属性没有任何效果。
2018年4月12日解决 - 添加了一行文本和示例,说明溢出列可以影响多列容器的高度。2018年4月12日解决
- 用SVG版本替换了HTML模拟示例,因为这些示例不够清楚。问题1087。
- 将
column-gap
的normal值更改为1em,而不是UA指定的长度,并建议为1em。2018年4月4日解决。 - 澄清了负值不被允许,虽然可以指定0值,但使用值将被限制为最小1px。2018年3月14日解决。
- 澄清了在跨列元素的情况下,内容会在出现在跨列元素之前的所有列中自动平衡。2017年11月9日解决。
- 添加了澄清和一个额外的示例,说明跨列元素可能低于第一层后代,并且在跨列元素所在的元素上存在的边距、边框和填充将绘制在跨列元素之上。2017年11月8日解决。
- 更改了句子
列线在内联内容层绘制,但位于多列元素中的所有内联内容下方。
为列线绘制在多列元素的边框之上。对于可滚动的多列元素,注意虽然多列元素的边框和背景显然不会滚动,但列线需要随着列滚动。
2017年11月7日解决。 - 在“多列模型”部分中,删除了两句话
也就是说,列框的行为类似于块级框、表格单元格和内联块框,参见CSS 2.1,第10.1节,第2项CSS21。然而,列框不会为position: fixed或position: absolute的元素建立块容器框。
这些句子被一个关于主框的澄清和一个展示abspos元素如何引用多列容器的新示例取代。2017年11月7日解决。 - 删除了说明“为了指示列中断应该(或不应该)出现的位置,引入了新的关键字值”的句子及后续示例(2017年10月5日发布的工作草案中的示例7),因为多列规范不再引入这些属性。编辑
- 我们在引用应用多列的元素时,将多列或多列“元素”改为多列或多列“容器”。2017年11月22日解决。
- 删除了以下示例:“如果将高图像移动到下一页的列中以找到空间,其自然列可能会保持为空。如果是这样,列仍被认为有内容,以决定是否应绘制列规则。”2017年9月7日解决。
2011年4月12日的候选推荐标准 (CR)中的更改。
- 添加了2016年7月的决议,将轨道尺寸下限更改为与CSS网格规范一致的强制UA指定值。2016年1月7日解决
- 移除了关于溢出列仅在连续媒体中的限制,声明column-fill对溢出列没有影响。2013年9月解决
- 添加了关键字balance-all,并展示了其工作原理的示例。2013年9月解决
- 伪算法已在多次场合下进行了修订。最近一次更改:2013年2月
- 澄清了属性columns,column-width,column-count“适用于块容器”。2013年2月参考
- 中断属性已从本规范中移至CSS碎片化模块。
- 更改了column-fill的措辞,澄清column-fill在分页之前被尊重。2012年1月参考
- 修订了示例和文本,以澄清边距折叠和跨列元素的处理方式。2013年10月参考
- 澄清了column-rule-width不会更改列的大小或位置。2013年9月参考
- 添加了每个跨列元素都会建立单独的BFC,且它们之间的边距会折叠。2011年12月参考
- 列规则绘制在内联内容层,但位于多列内的所有内联内容下方。2013年2月参考
- 澄清了column-span即使不在多列中,也会导致元素建立格式化上下文。
- 列跨距并不总是建立块格式化上下文。
- 允许column-gap接受<长度百分比>而不仅仅是<长度>。
- column-width和column-count适用于块容器,除了表格包装框。
- 溢出列的内容不会被裁剪。
隐私考虑
多列不会引入新的隐私泄露。
安全考虑
多列不会引入新的安全考虑。
无障碍访问考虑
设置容器高度和行长度可能会给视觉或认知障碍人士带来挑战。参见WCAG成功标准1.4.10:重排和WCAG 1.4.8:视觉呈现以了解用户需求。
致谢
本文件基于若干旧的提案以及对旧提案的评论。贡献者包括:
Alex Mogilevsky, Andy Clarke, Anton Prowse, Bert Bos, Björn Höhrmann, Cédric Savarese, Chris Lilley, Chris Wilson, Daniel Glazman and Dave Raggett, David Hyatt, David Singer, David Woolley, Elika Etemad, Giovanni Campagna, Ian Hickson. Joost de Valk, Kevin Lawver, L. David Baron, Markus Mielke, Melinda Grant, Michael Day, Morten Stenshorne, Øyvind Stenhaug, Peter Linss, Peter-Paul Koch, Robert O’Callahan, Robert Stevahn, Sergey Genkin, Shelby Moore, Steve Zilles, Sylvain Galineau, Tantek Çelik, Till Halbach