column-gap
该 CSS 属性用来设置元素列之间的间隔 (gutter) 大小。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
column-gap
一开始是 Multi-column 布局 下的特有属性,后来在其他布局中也使用这个属性。如 Box Alignment 中的表述,该属性已经在Multi-column(多列布局)、Flexible Box(弹性盒子)以及 Grid layouts(网格布局)中使用。
CSS Grid Layout 起初是用 grid-gap
属性来定义的,目前逐渐被 gap
替代。但是,为了兼容那些不支持 gap
属性的浏览器,你需要像上面的例子一样使用带有前缀的属性。
语法
/* Keyword value */
column-gap: normal;
/* <length> values */
column-gap: 3px;
column-gap: 2.5em;
/* <percentage> value */
column-gap: 3%;
/* Global values */
column-gap: inherit;
column-gap: initial;
column-gap: unset;
column-gap
属性有以下值来表示。
取值
normal
- 表示列之间的间隔宽度。在
多列布局
时默认间隔为1em,其他类型布局默认间隔为 0。 <length>
- 用
<length>
来定义列之间的间隔大小。而且<length>
值必须是非负数的。 <percentage>
- 用
<percentage>
(百分比)来定义列之间的间隔大小。同样的,<percentage>
值也必须是非负数的。
正式语法
normal | <length-percentage>where
<length-percentage> = <length> | <percentage>
示例
Flex布局
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
HTML
<div id="flexbox"> <div></div> <div></div> <div></div> </div>
CSS
#flexbox { display: flex; height: 100px; column-gap: 20px; } #flexbox > div { background-color: lime; flex: auto; }
结果
Grid布局
HTML
<div id="grid"> <div></div> <div></div> <div></div> </div>
CSS
#grid { grid-column-gap: 20px; }
#grid { display: grid; height: 100px; grid-template-columns: repeat(3, 1fr); grid-template-rows: 100px; column-gap: 20px; } #grid > div { background-color: lime; }
结果
Multi-column布局
HTML
<p class="content-box"> This is some multi-column text with a 40px column gap created with the CSS `column-gap` property. Don't you think that's fun and exciting? I sure do! </p>
CSS
.content-box { column-count: 3; column-gap: 40px; }
结果
规范
规范 | 状态 | 备注 |
---|---|---|
CSS Box Alignment Module Level 3 column-gap |
Working Draft | 在弹性布局和网格布局中应用 |
CSS Grid Layout column-gap |
Candidate Recommendation | 描述在网格布局中的作用 |
CSS Multi-column Layout Module column-gap |
Working Draft | 初始化定义 |
初始值 | normal |
---|---|
适用元素 | multi-column elements, flex containers, grid containers |
是否是继承属性 | 否 |
Percentages | refer to corresponding dimension of the content area |
适用媒体 | visual |
计算值 | as specified, with <length>s made absolute, and normal computing to zero except on multi-column elements |
Animation type | a length, percentage or calc(); |
正规顺序 | per grammar |
浏览器兼容性
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.
在 Flex 布局中的兼容性
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Supported in Flex Layout | Chrome No support No | Edge No support No | Firefox Full support 63 | IE No support No | Opera No support No | Safari
Full support
3
| WebView Android No support No | Chrome Android No support No | Firefox Android Full support 63 | Opera Android No support No | Safari iOS
Full support
2
| Samsung Internet Android No support No |
Legend
- Full support
- Full support
- No support
- No support
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
在 Grid 布局中的兼容性
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Supported in Grid Layout | Chrome
Full support
66
| Edge
Full support
16
| Firefox
Full support
61
| IE No support No | Opera
Full support
53
| Safari
Full support
10.1
| WebView Android
Full support
66
| Chrome Android
Full support
66
| Firefox Android
Full support
61
| Opera Android
Full support
47
| Safari iOS
Full support
10.3
| Samsung Internet Android
Full support
6.0
|
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.
- Uses a non-standard name.
- Uses a non-standard name.
在 Multi-column 布局中的兼容性
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Supported in Multi-column Layout | Chrome
Full support
50
| Edge
Full support
12
| Firefox
Full support
52
| IE Full support 10 | Opera
Full support
37
| Safari
Full support
10
| WebView Android
Full support
50
| Chrome Android
Full support
50
| Firefox Android
Full support
52
| Opera Android
Full support
37
| Safari iOS
Full support
10
| Samsung Internet Android
Full support
5.0
|
calc() values | Chrome Full support 66 | Edge Full support 16 | Firefox Full support 61 | IE No support No | Opera Full support 53 | Safari No support No | WebView Android Full support 66 | Chrome Android Full support 66 | Firefox Android Full support 61 | Opera Android Full support 47 | Safari iOS No support No | Samsung Internet Android No support No |
<percentage> values | Chrome Full support 66 | Edge Full support 16 | Firefox Full support 61 | IE No support No | Opera Full support 53 | Safari No support No | WebView Android Full support 66 | Chrome Android Full support 66 | Firefox Android Full support 61 | Opera Android Full support 47 | Safari iOS No support No | Samsung Internet Android No support No |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
- Requires a vendor prefix or different name for use.
- Requires a vendor prefix or different name for use.
参见
- 相关CSS属性:
row-gap
,gap
- Grid 布局指南: Basic concepts of grid layout - Gutters
- Multi-column 布局指南: Styling Columns