← 返回首页
gap - CSS:层叠样式表 | MDN

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

gap

基线 广泛可用

自 2017年10月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

CSS gap 简写属性用于设置行与列之间的间隙(网格间距)。

规范的早期版本将该属性命名为 grid-gap,且为了保持与旧网站的兼容性,浏览器仍然会接受 grid-gap 作为 gap 的别名。

本文内容

尝试一下

gap: 0;
gap: 10%;
gap: 1em;
gap: 10px 20px;
gap: calc(20px + 10%);
<section class="default-example" id="default-example"> <div class="example-container"> <div class="transition-all" id="example-element"> <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div> </div> </div> </section>
#example-element { border: 1px solid #c5c5c5; display: grid; grid-template-columns: 1fr 1fr; width: 200px; } #example-element > div { background-color: rgba(0, 0, 255, 0.2); border: 3px solid blue; }

组成属性

该属性为以下 CSS 属性的简写:

语法

css
/* 一个 <length> 值 */ gap: 20px; gap: 1em; gap: 3vmin; gap: 0.5cm; /* 一个 <percentage> 值 */ gap: 16%; gap: 100%; /* 两个 <length> 值 */ gap: 20px 10px; gap: 1em 0.5em; gap: 3vmin 2vmax; gap: 0.5cm 2mm; /* 一个或两个 <percentage> 值 */ gap: 16% 100%; gap: 21px 82%; /* calc() 值 */ gap: calc(10% + 20px); gap: calc(20px + 10%) calc(10% - 5px); /* 全局值 */ gap: inherit; gap: initial; gap: revert; gap: revert-layer; gap: unset;

该属性用来表示 <'row-gap'> 和可选的 <'column-gap'> 的值。如果缺失 <'column-gap'>,则其会被设置成跟 <'row-gap'> 一样的值。

<'row-gap'> 和 <'column-gap'> 都可以用 <length> 或者 <percentage> 来指定。

<length>

网格线之间的间隙宽度。

<percentage>

网格线之间的间隙宽度,为相对于当前元素尺寸的百分比。

形式定义

初始值适用元素是否是继承属性计算值动画类型
该简写所对应的每个属性:
multi-column elements, flex containers, grid containers
该简写所对应的每个属性:
  • 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
该简写所对应的每个属性:

形式语法

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

<row-gap> =
normal |
<length-percentage [0,∞]> |
<line-width>

<column-gap> =
normal |
<length-percentage [0,∞]> |
<line-width>

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

<line-width> =
<length [0,∞]> |
hairline |
thin |
medium |
thick
此语法反映了基于 CSS Borders and Box Decorations Module Level 4, CSS Gap Decorations Module Level 1, CSS Values and Units Module Level 4 的最新标准。并非所有浏览器都已实现全部内容。有关支持信息,请参阅浏览器兼容性

示例

弹性布局

HTML

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

CSS

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; }

结果

网格布局

HTML

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

CSS

css
#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; }

结果

多列布局

HTML

html
<p class="content-box"> 这是使用 CSS <code>gap</code> 属性创建的具有 40px 列间距的多栏文本。你不觉得这很有趣和令人兴奋吗?我当然是这么认为的!!! </p>

CSS

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

结果

规范

规范
CSS Gaps Module Level 1
# gap-shorthand

浏览器兼容性

启用 JavaScript 以查看此浏览器兼容性表。

参见