column-span

Baseline 已廣泛支援

此特性已經十分成熟,可在許多裝置和瀏覽器版本上使用。自 2020 年 7 月以來,它已在各大瀏覽器中可用。

column-span CSS 屬性使得元素在設定為 all 時可以跨越所有列。

試一試

column-span: none;
column-span: all;
<section id="default-example">
  <div class="multicol-element">
    <p>
      London. Michaelmas term lately over, and the Lord Chancellor sitting in
      Lincoln's Inn Hall.
    </p>
    <div id="example-element">Spanner?</div>
    <p>
      Implacable November weather. As much mud in the streets as if the waters
      had but newly retired from the face of the earth, and it would not be
      wonderful to meet a Megalosaurus, forty feet long or so, waddling like an
      elephantine lizard up Holborn Hill.
    </p>
  </div>
</section>
.multicol-element {
  width: 100%;
  text-align: left;
  column-count: 3;
}

.multicol-element p {
  margin: 0;
}

#example-element {
  background-color: rebeccapurple;
  padding: 10px;
  color: white;
}

跨越多列的元素稱為跨列元素

語法

css
/* Keyword values */
column-span: none;
column-span: all;

/* Global values */
column-span: inherit;
column-span: initial;
column-span: revert;
column-span: revert-layer;
column-span: unset;

column-span 屬性的值為下面列出的關鍵詞之一。

none

該元素不跨越多個列。

all

該元素跨越所有列。出現在元素之前的正常流中的內容會自動在元素出現之前在所有列中平衡。該元素會建立一個新的塊級格式化上下文。

正式定義

初始值none
應用於流內塊級元素
繼承性
計算值同指定值
動畫型別離散

正式語法

column-span = 
none |
<integer [1,∞]> |
all |
auto

示例

使標題跨列

在此示例中,標題被設定為跨越文章的所有列。

HTML

html
<article>
  <h2>Header spanning all of the columns</h2>
  <p>
    The h2 should span all the columns. The rest of the text should be
    distributed among the columns.
  </p>
  <p>
    This is a bunch of text split into three columns using the CSS `columns`
    property. The text is equally distributed over the columns.
  </p>
  <p>
    This is a bunch of text split into three columns using the CSS `columns`
    property. The text is equally distributed over the columns.
  </p>
  <p>
    This is a bunch of text split into three columns using the CSS `columns`
    property. The text is equally distributed over the columns.
  </p>
  <p>
    This is a bunch of text split into three columns using the CSS `columns`
    property. The text is equally distributed over the columns.
  </p>
</article>

CSS

css
article {
  columns: 3;
}

h2 {
  column-span: all;
}

結果

規範

規範
CSS Multi-column Layout Module Level 1
# column-span

瀏覽器相容性

另見