--- title: gap (grid-gap) slug: Web/CSS/gap tags: - CSS - CSS Flexible Boxes - CSS Grid translation_of: Web/CSS/gap --- <div>{{CSSRef}}</div> <p><strong><code>gap</code></strong> 属性是用来设置网格行与列之间的间隙({{glossary("gutters")}}),该属性是{{cssxref("row-gap")}} and {{cssxref("column-gap")}}的简写形式。</p> <div>{{EmbedInteractiveExample("pages/css/gap.html")}}</div> <div class="note"> <p><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout">CSS Grid Layout</a> 起初是用 {{cssxref('grid-gap')}} 属性来定义的,目前逐渐被 <code>gap</code> 替代。但是,为了兼容那些不支持 <code>gap</code> 属性的浏览器,你需要像上面的例子一样,使用带有前缀的属性。</p> </div> <h2 id="语法">语法</h2> <pre class="brush: css no-line-numbers notranslate">/* 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; </pre> <p>该属性用来表示 <code><'row-gap'></code> 和 <code><'column-gap'></code>的值,而<code><'column-gap'></code>是可选的,假如 <code><'column-gap'></code> 缺失的话,则会被设置成跟<code><'row-gap'></code>一样的的值。</p> <p><code><'row-gap'></code> and <code><'column-gap'></code> 都可以用 <code><length></code> 或者 <code><percentage></code> 来表示。</p> <h3 id="值">值</h3> <dl> <dt>{{cssxref("<length>")}}</dt> <dd>网格线之间的间隙宽度。</dd> <dt>{{cssxref("<percentage>")}}</dt> <dd>网格线直接的间隙宽度,相对网格容器的百分比。</dd> </dl> <h3 id="正式语法">正式语法</h3> {{csssyntax}} <h2 id="示例">示例</h2> <h3 id="Flex布局">Flex布局</h3> <p>{{SeeCompatTable}}</p> <h4 id="HTML">HTML</h4> <pre class="brush: html; notranslate"><div id="flexbox"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </pre> <h4 id="CSS">CSS</h4> <pre class="brush: css; highlight[5] notranslate">#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; } </pre> <h4 id="Result">Result</h4> <p>{{EmbedLiveSample('Flex布局', "auto", "120px")}}</p> <h3 id="Grid布局">Grid布局</h3> <h4 id="HTML_2">HTML</h4> <pre class="brush: html notranslate"><div id="grid"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div></pre> <h4 id="CSS_2">CSS</h4> <pre class="brush: css; highlight[5] notranslate">#grid { display: grid; height: 200px; grid-template: repeat(3, 1fr) / repeat(3, 1fr); gap: 20px 5px; } #grid > div { background-color: lime; } </pre> <h4 id="Result_2">Result</h4> <p>{{EmbedLiveSample('Grid布局', 'auto', 120)}}</p> <h3 id="多列布局">多列布局</h3> <h4 id="HTML_3">HTML</h4> <pre class="brush: html; notranslate"><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> </pre> <h4 id="CSS_3">CSS</h4> <pre class="brush: css; highlight[3] notranslate">.content-box { column-count: 3; gap: 40px; } </pre> <h4 id="Result_3">Result</h4> <p>{{EmbedLiveSample("多列布局", "auto", "120px")}}</p> <h2 id="规范">规范</h2> <table class="standard-table"> <thead> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">备注</th> </tr> </thead> <tbody> <tr> <td>{{SpecName("CSS3 Box Alignment", "#propdef-gap", "gap")}}</td> <td>{{Spec2("CSS3 Box Alignment")}}</td> <td>Initial definition</td> </tr> </tbody> </table> <p>{{cssinfo}}</p> <h2 id="浏览器兼容性">浏览器兼容性</h2> <h3 id="在Flex_布局的兼容性">在Flex 布局的兼容性</h3> <p>{{Compat("css.properties.gap.flex_context")}}</p> <h3 id="在Grid_layout的兼容性">在Grid layout的兼容性</h3> <p>{{Compat("css.properties.gap.grid_context")}}</p> <h3 id="多列布局的兼容性">多列布局的兼容性</h3> <p>{{Compat("css.properties.gap.multicol_context")}}</p> <h2 id="参见">参见</h2> <ul> <li>Related CSS properties: {{cssxref("row-gap")}}, {{cssxref("column-gap")}}</li> <li>Grid Layout Guide: <em><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout#Gutters">Basic concepts of grid layout - Gutters</a></em></li> </ul>