gap CSS property sets the gaps ( gutters ) between rows and columns. It is a 简写 for row-gap and column-gap .

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.

CSS 栅格布局 initially defined the grid-gap property. This prefixed property is being replaced by gap . However, in order to support browsers that implemented grid-gap and not gap for grid, you will need to use the prefixed property as in the interactive example above.

句法

/* One <length> value */
gap: 20px;
gap: 1em;
gap: 3vmin;
gap: 0.5cm;
/* One <percentage> value */
gap: 16%;
gap: 100%;
/* Two <length> values */
gap: 20px 10px;
gap: 1em 0.5em;
gap: 3vmin 2vmax;
gap: 0.5cm 2mm;
/* One or two <percentage> values */
gap: 16% 100%;
gap: 21px 82%;
/* calc() values */
gap: calc(10% + 20px);
gap: calc(20px + 10%) calc(10% - 5px);
/* Global values */
gap: inherit;
gap: initial;
gap: unset;
				

This property is specified as a value for <'row-gap'> followed optionally by a value for <'column-gap'> 。若 <'column-gap'> is omitted, it’s set to the same value as <'row-gap'> .

<'row-gap'> and <'column-gap'> are each specified as a <length> <percentage> .

<length>

Is the width of the gutter separating the grid lines.

<percentage>

Is the width of the gutter separating the grid lines, relative to the dimension of the element.

形式定义

初始值 as each of the properties of the shorthand:
适用于 multi-column elements, flex containers, grid containers
继承 no
计算值 as each of the properties of the shorthand:
  • row-gap : as specified, with <length>s made absolute, and normal computing to zero except on multi-column elements
  • column-gap : as specified, with <length>s made absolute, and normal computing to zero except on multi-column elements
动画类型 as each of the properties of the shorthand:

形式句法

<'row-gap'> <'column-gap'>?
					

范例

Flex layout

HTML

<div id="flexbox">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
					

CSS

#flexbox {
  display: flex;
  flex-wrap: wrap;
  width: 300px;
  gap: 20px 5px;
}
#flexbox > div {
  border: 1px solid green;
  background-color: lime;
  flex: 1 1 auto;
  width: 100px;
  height: 50px;
}
					

结果

Grid layout

HTML

<div id="grid">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
					

CSS

#grid {
  grid-gap: 20px 5px;
}
					
#grid {
  display: grid;
  height: 200px;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  gap: 20px 5px;
}
#grid > div {
  border: 1px solid green;
  background-color: lime;
}
					

结果

Multi-column layout

HTML

<p class="content-box">
  This is some multi-column text with a 40px column
  gap created with the CSS <code>gap</code> property.
  Don't you think that's fun and exciting? I sure do!
</p>
					

CSS

.content-box {
  column-count: 3;
  gap: 40px;
}
					

结果

规范

规范 状态 注释
CSS Box Alignment Module Level 3
The definition of 'gap' in that specification.
工作草案 初始定义

浏览器兼容性

The compatibility table on 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.

Support in Flex layout

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Supported in Flex Layout Chrome 完整支持 84 Edge 完整支持 84 Firefox 完整支持 63 IE 不支持 No Opera 完整支持 70 Safari 不支持 No WebView Android 完整支持 84 Chrome Android 完整支持 84 Firefox Android 完整支持 63 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

不支持

不支持

Support in Grid layout

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Supported in Grid Layout Chrome 完整支持 66
完整支持 66
完整支持 57 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
完整支持 29 Alternate Name Disabled
Alternate Name Uses the non-standard name: grid-gap
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge 完整支持 16
完整支持 16
完整支持 16 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
Firefox 完整支持 61
完整支持 61
完整支持 52 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
不支持 40 — 59 Alternate Name Disabled
Alternate Name Uses the non-standard name: grid-gap
Disabled ). To change preferences in Firefox, visit about:config.
IE 不支持 No Opera 完整支持 53
完整支持 53
完整支持 44 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
完整支持 28 Alternate Name Disabled
Alternate Name Uses the non-standard name: grid-gap
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari 完整支持 10.1 Alternate Name
完整支持 10.1 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
完整支持 12
WebView Android 完整支持 66
完整支持 66
完整支持 57 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
Chrome Android 完整支持 66
完整支持 66
完整支持 57 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
完整支持 29 Alternate Name Disabled
Alternate Name Uses the non-standard name: grid-gap
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 完整支持 61
完整支持 61
完整支持 52 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
不支持 40 — 59 Alternate Name Disabled
Alternate Name Uses the non-standard name: grid-gap
Disabled ). To change preferences in Firefox, visit about:config.
Opera Android 完整支持 47
完整支持 47
完整支持 43 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
完整支持 28 Alternate Name Disabled
Alternate Name Uses the non-standard name: grid-gap
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari iOS 完整支持 10.3 Alternate Name
完整支持 10.3 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
完整支持 12
Samsung Internet Android 完整支持 9.0
完整支持 9.0
完整支持 7.0 Alternate Name
Alternate Name Uses the non-standard name: grid-gap
calc() Chrome 完整支持 66 Edge 完整支持 16 Firefox 完整支持 52 IE 不支持 No Opera 完整支持 53 Safari 不支持 No WebView Android 完整支持 66 Chrome Android 完整支持 66 Firefox Android 完整支持 52 Opera Android 完整支持 47 Safari iOS 不支持 No Samsung Internet Android 完整支持 9.0
<percentage> Chrome 完整支持 66 Edge 完整支持 16 Firefox 完整支持 52 IE 不支持 No Opera 完整支持 53 Safari 不支持 No WebView Android 完整支持 66 Chrome Android 完整支持 66 Firefox Android 完整支持 52 Opera Android 完整支持 47 Safari iOS 不支持 No Samsung Internet Android 完整支持 9.0

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

使用非标名称。

使用非标名称。

Support in Multi-column layout

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Supported in Multi-column Layout Chrome 完整支持 66 Edge 完整支持 16 Firefox 完整支持 61 IE 不支持 No Opera 完整支持 53 Safari 不支持 No WebView Android 完整支持 66 Chrome Android 完整支持 66 Firefox Android 完整支持 61 Opera Android 完整支持 47 Safari iOS 不支持 No Samsung Internet Android 完整支持 9.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. CSS Box Alignment
  4. 指南
    1. Box alignment for block, absolutely positioned and table layout
    2. Box alignment in Flexbox
    3. Box alignment in Multi-column Layout
    4. Box alignment in grid layout
  5. 特性
    1. align-content
    2. align-items
    3. align-self
    4. column-gap (grid-column-gap)
    5. gap (grid-gap)
    6. justify-content
    7. justify-items
    8. justify-self
    9. place-content
    10. place-items
    11. place-self
    12. row-gap (grid-row-gap)

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1