column-width

Baseline 已广泛支持

此功能已得到良好确立,并在许多设备和浏览器版本中都可使用。自 ⁨2016 年 11 月⁩以来,它已在所有浏览器中可用。

column-width CSS 属性设置多列布局中理想的列宽。容器将容纳尽可能多的列,且所有列的宽度都不会小于 column-width 值。如果容器的宽度窄于指定值,则单列的宽度将小于声明的列宽。

试一试

column-width: auto;
column-width: 6rem;
column-width: 120px;
column-width: 18ch;
<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 {
  width: 100%;
  columns: auto;
  text-align: left;
}

此属性可帮助您创建适合不同屏幕尺寸的响应式设计。尤其是在存在 column-count 属性(具有优先权)的情况下,您必须指定所有相关的长度值才能实现精确的列宽。在水平文本中,这些值包括 widthcolumn-widthcolumn-gapcolumn-rule-width

语法

css
/* Keyword value */
column-width: auto;

/* <length> values */
column-width: 60px;
column-width: 15.5em;
column-width: 3.3vw;

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

column-width 属性指定为以下值之一。

<length>

表示最佳列宽。实际列宽可能与指定值不同:在需要填充可用空间时可能会更宽,在可用空间太小时可能会更窄。该值必须严格为正数,否则声明无效。百分比值也无效。

auto

列的宽度由其他 CSS 属性决定,例如 column-count

正式定义

初始值auto
应用于块容器,除了表格包装盒
继承性
计算值绝对长度,零或更大
动画类型一个长度

正式语法

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

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

示例

以像素设置列宽

HTML

html
<p class="content-box">
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
  nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi
  enim ad minim veniam, quis nostrud exercitation ullamcorper suscipit lobortis
  nisl ut aliquip ex ea commodo consequat.
</p>

CSS

css
.content-box {
  column-width: 100px;
}

结果

规范

规范
CSS Box Sizing Module Level 3
# 列大小调整
CSS Multi-column Layout Module Level 1
# cw

浏览器兼容性

另见