columns

Baseline 已广泛支持

此特性已得到良好确立,可跨多种设备和浏览器版本使用。自 2017 年 3 月起,所有浏览器均支持此特性。

columns 是一个 CSS 缩写属性,它设置了绘制元素内容时使用的列数,以及这些列的宽度。

试一试

columns: 2;
columns: 6rem auto;
columns: 12em;
columns: 3;
<section id="default-example">
  <p id="example-element">
    London. Michaelmas term lately over, and the Lord Chancellor sitting in
    Lincoln's Inn Hall. Implacable November weather. As much mud in the streets
    as if the waters had but newly retired from the face of the earth, and it
    would not be wonderful to meet a Megalosaurus, forty feet long or so,
    waddling like an elephantine lizard up Holborn Hill.
  </p>
</section>
#example-element {
  min-width: 21rem;
  text-align: left;
}

构成属性

此属性是以下 CSS 属性的简写:

语法

css
/* Column width */
columns: 18em;

/* Column count */
columns: auto;
columns: 2;

/* Both column width and count */
columns: 2 auto;
columns: auto 12em;
columns: auto auto;

/* Global values */
columns: inherit;
columns: initial;
columns: revert;
columns: revert-layer;
columns: unset;

columns 属性可以指定为下面列出的一个或两个值,顺序不限。

<'column-width'>

理想的列宽,定义为 <length> 或关键字 auto。实际宽度可能会更宽或更窄,以适应可用空间。请参阅 column-width

<'column-count'>

元素内容应该流入的理想列数,定义为 <integer> 或关键字 auto。如果此值和列宽都不是 auto,则它仅表示允许的最大列数。请参阅 column-count

正式定义

初始值作为简写中的每个属性
应用于块容器,除了表格包装盒
继承性
计算值作为简写中的每个属性
动画类型作为简写中的每个属性

正式语法

columns = 
[ <'column-width'> || <'column-count'> ] [ / <'column-height'> ]?

<column-width> =
auto |
<length [0,∞]> |
min-content |
max-content |
fit-content( <length-percentage> )

<column-count> =
auto |
<integer [1,∞]>

<column-height> =
auto |
<length [0,∞]>

<length-percentage> =
<length> |
<percentage>

示例

设置三等分列

HTML

html
<p class="content-box">
  This is a bunch of text split into three columns using the CSS `columns`
  property. The text is equally distributed over the columns.
</p>

CSS

css
.content-box {
  columns: 3 auto;
}

结果

规范

规范
CSS Multi-column Layout Module Level 1
# columns

浏览器兼容性

另见