column-gap

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

column-gap CSS 屬性設定元素列之間的間距(gutter)大小。

column-gap 最初是 多列布局 的一部分,其定義已擴充套件到包括多種佈局方法。現在在 CSS 盒對齊 中指定,它可用於多列、彈性盒和網格佈局。

規範的早期版本將此屬性稱為 grid-column-gap,為了保持與舊版網站的相容性,瀏覽器仍將接受 grid-column-gap 作為 column-gap 的別名。

試一試

column-gap: 0;
column-gap: 10%;
column-gap: 1em;
column-gap: 20px;
<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: rgb(0 0 255 / 0.2);
  border: 3px solid blue;
}

語法

css
/* 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: revert;
column-gap: revert-layer;
column-gap: unset;

column-gap 屬性指定為以下列出的值之一。

normal

在列之間使用瀏覽器的預設間距。對於多列布局,這指定為 1em。對於所有其他佈局型別,它為 0。

<length>

列之間的間距大小,定義為 <length><length> 屬性的值必須是非負數。

<percentage>

列之間的間距大小,定義為 <percentage><percentage> 屬性的值必須是非負數。

正式定義

初始值normal
應用於多列元素、彈性容器、網格容器
繼承性
百分比參照內容區域的相應維度
計算值按指定,將 <length> 轉換為絕對值,並且 normal 計算為零,多列元素除外
動畫型別一個長度百分比或 calc();

正式語法

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

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

示例

彈性佈局

在此示例中,一個彈性容器包含六個具有兩種不同寬度(200px300px)的彈性專案,建立的彈性專案未按網格佈局。 column-gap 屬性用於在相鄰的彈性專案之間新增水平空間。

HTML

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

CSS

為了建立彈性容器,我們將其 display 屬性值設定為 flex。然後,我們使用 flex-flow 簡寫屬性將 flex-direction 設定為 row(預設值),將 flex-wrap 設定為 wrap,允許彈性專案在需要時流向新行。預設情況下,彈性專案會拉伸到與其容器一樣高。透過設定 height,即使是空彈性專案也將高 100px

為了更好地演示 column-gap 屬性,此示例中的彈性專案具有兩個不同的寬度值。彈性專案的寬度在 <div> 彈性專案內設定。我們使用 flex 簡寫屬性的 flex-basis 元件使所有彈性專案寬 200px。然後,我們透過使用 :nth-of-type(3n) 選擇器定位每個第三個彈性專案,將其寬度擴充套件到 300px

column-gap 值在彈性容器上設定為 20px,以在每行相鄰的彈性專案之間建立 20px 的間隙。

css
.flexbox {
  display: flex;
  flex-flow: row wrap;
  height: 100px;
  column-gap: 20px;
}

.flexbox > div {
  border: 1px solid green;
  background-color: lime;
  flex: 200px;
}
div:nth-of-type(3n) {
  flex: 300px;
}

結果

注意:雖然每個彈性行中相鄰的彈性專案之間有水平空間,但行之間沒有空間。要在彈性行之間設定垂直空間,您可以為 row-gap 屬性指定一個非零值。 gap 簡寫屬性也可用於在一個宣告中設定 row-gapcolumn-gap,按該順序。

網格佈局

HTML

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

CSS

css
#grid {
  display: grid;
  height: 100px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 100px;
  column-gap: 20px;
}

#grid > div {
  border: 1px solid green;
  background-color: lime;
}

結果

多列布局

HTML

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

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

結果

規範

規範
CSS Box Alignment Module Level 3
# 列行間距
CSS 網格佈局模組 Level 2
# 間距
CSS Multi-column Layout Module Level 1
# column-gap

瀏覽器相容性

另見