跳至主要內容 跳至文件導覽

欄(Columns)

了解如何使用我們的 Flexbox 格線系統,透過對齊、排序和偏移等多種選項來修改欄。此外,了解如何使用欄類別來管理非格線元素的寬度。

請注意! 在深入了解如何修改和自訂格線欄之前,請務必先閱讀格線系統頁面

運作原理(How they work)

  • 欄建立在格線的 Flexbox 架構上。 Flexbox 意味著我們有選項可以更改個別欄和在列層級修改欄群組。您可以選擇欄如何增長、縮小或以其他方式改變。

  • 建立格線版面配置時,所有內容都放在欄中。 Bootstrap 格線的層級結構從容器到列到欄再到您的內容。在極少數情況下,您可能會將內容和欄結合,但請注意可能會有意外的後果。

  • Bootstrap 包含用於建立快速響應式版面配置的預先定義類別。六個中斷點和每個格線層級十幾個欄,我們已經為您建立了數十個類別來建立所需的版面配置。如果您願意,可以透過 Sass 停用此功能。

對齊(Alignment)

使用 Flexbox 對齊通用類別來垂直和水平對齊欄。

垂直對齊(Vertical alignment)

使用任何響應式 align-items-* 類別來更改垂直對齊。

One of three columns
One of three columns
One of three columns
html
<div class="container text-center">
  <div class="row align-items-start">
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
</div>
One of three columns
One of three columns
One of three columns
html
<div class="container text-center">
  <div class="row align-items-center">
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
</div>
One of three columns
One of three columns
One of three columns
html
<div class="container text-center">
  <div class="row align-items-end">
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
</div>

或者,使用任何響應式 .align-self-* 類別來個別更改每個欄的對齊。

One of three columns
One of three columns
One of three columns
html
<div class="container text-center">
  <div class="row">
    <div class="col align-self-start">
      One of three columns
    </div>
    <div class="col align-self-center">
      One of three columns
    </div>
    <div class="col align-self-end">
      One of three columns
    </div>
  </div>
</div>

水平對齊(Horizontal alignment)

使用任何響應式 justify-content-* 類別來更改水平對齊。

One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
One of two columns
html
<div class="container text-center">
  <div class="row justify-content-start">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-center">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-end">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-around">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-between">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-evenly">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
</div>

欄換行(Column wrapping)

如果在單一列中放置超過 12 欄,每組額外的欄將作為一個單元換到新行。

.col-9
.col-4
Since 9 + 4 = 13 > 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.
.col-6
Subsequent columns continue along the new line.
html
<div class="container">
  <div class="row">
    <div class="col-9">.col-9</div>
    <div class="col-4">.col-4<br>Since 9 + 4 = 13 &gt; 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
    <div class="col-6">.col-6<br>Subsequent columns continue along the new line.</div>
  </div>
</div>

欄中斷(Column breaks)

在 Flexbox 中將欄換到新行需要一個小技巧:在您想要將欄換到新行的任何位置加入一個 width: 100% 的元素。通常這是透過多個 .row 來完成的,但不是每個實作方法都能做到這一點。

.col-6 .col-sm-3
.col-6 .col-sm-3
.col-6 .col-sm-3
.col-6 .col-sm-3
html
<div class="container text-center">
  <div class="row">
    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>

    <!-- Force next columns to break to new line -->
    <div class="w-100"></div>

    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
  </div>
</div>

您也可以使用我們的響應式顯示通用類別在特定中斷點套用此中斷。

.col-6 .col-sm-4
.col-6 .col-sm-4
.col-6 .col-sm-4
.col-6 .col-sm-4
html
<div class="container text-center">
  <div class="row">
    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>

    <!-- Force next columns to break to new line at md breakpoint and up -->
    <div class="w-100 d-none d-md-block"></div>

    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
  </div>
</div>

重新排序(Reordering)

排序類別(Order classes)

使用 .order- 類別來控制內容的視覺順序。這些類別是響應式的,因此您可以按中斷點設定 order(例如 .order-1.order-md-2)。在所有六個格線層級中支援 15

First in DOM, no order applied
Second in DOM, with a larger order
Third in DOM, with an order of 1
html
<div class="container text-center">
  <div class="row">
    <div class="col">
      First in DOM, no order applied
    </div>
    <div class="col order-5">
      Second in DOM, with a larger order
    </div>
    <div class="col order-1">
      Third in DOM, with an order of 1
    </div>
  </div>
</div>

還有響應式 .order-first.order-last 類別,分別透過套用 order: -1order: 6 來更改元素的 order。這些類別也可以根據需要與編號的 .order-* 類別混合使用。

First in DOM, ordered last
Second in DOM, unordered
Third in DOM, ordered first
html
<div class="container text-center">
  <div class="row">
    <div class="col order-last">
      First in DOM, ordered last
    </div>
    <div class="col">
      Second in DOM, unordered
    </div>
    <div class="col order-first">
      Third in DOM, ordered first
    </div>
  </div>
</div>

如果您需要更多 .order-* 類別,可以透過修改我們的 $utilities Sass map 來加入新的。詳情請閱讀我們的 Sass maps 和迴圈文件我們的修改通用類別文件

$utilities: map-merge(
  $utilities,
  (
    "order": map-merge(
      map-get($utilities, "order"),
      (
        values: map-merge(
          map-get(map-get($utilities, "order"), "values"),
          (
            6: 6, // Add a new `.order-{breakpoint}-6` utility
            last: 7 // Change the `.order-{breakpoint}-last` utility to use the next number
          )
        ),
      ),
    ),
  )
);

偏移欄(Offsetting columns)

您可以透過兩種方式偏移格線欄:我們的響應式 .offset- 格線類別和我們的外距通用類別。格線類別的大小與欄相符,而外距對於偏移寬度可變的快速版面配置更有用。

偏移類別(Offset classes)

使用 .offset-md-* 類別將欄向右移動。這些類別將欄的左外距增加 * 欄。例如,.offset-md-4.col-md-4 移動四欄。

.col-md-4
.col-md-4 .offset-md-4
.col-md-3 .offset-md-3
.col-md-3 .offset-md-3
.col-md-6 .offset-md-3
html
<div class="container text-center">
  <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-4 offset-md-4">.col-md-4 .offset-md-4</div>
  </div>
  <div class="row">
    <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
    <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
  </div>
  <div class="row">
    <div class="col-md-6 offset-md-3">.col-md-6 .offset-md-3</div>
  </div>
</div>

除了在響應式中斷點清除欄之外,您可能需要重設偏移。在格線系統範例中查看實際效果。

.col-sm-5 .col-md-6
.col-sm-5 .offset-sm-2 .col-md-6 .offset-md-0
.col-sm-6 .col-md-5 .col-lg-6
.col-sm-6 .col-md-5 .offset-md-2 .col-lg-6 .offset-lg-0
html
<div class="container text-center">
  <div class="row">
    <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
    <div class="col-sm-5 offset-sm-2 col-md-6 offset-md-0">.col-sm-5 .offset-sm-2 .col-md-6 .offset-md-0</div>
  </div>
  <div class="row">
    <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
    <div class="col-sm-6 col-md-5 offset-md-2 col-lg-6 offset-lg-0">.col-sm-6 .col-md-5 .offset-md-2 .col-lg-6 .offset-lg-0</div>
  </div>
</div>

外距通用類別(Margin utilities)

隨著 v4 遷移到 Flexbox,您可以使用像 .me-auto 這樣的外距通用類別來強制將同層欄彼此分開。

.col-md-4
.col-md-4 .ms-auto
.col-md-3 .ms-md-auto
.col-md-3 .ms-md-auto
.col-auto .me-auto
.col-auto
html
<div class="container text-center">
  <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
  </div>
  <div class="row">
    <div class="col-md-3 ms-md-auto">.col-md-3 .ms-md-auto</div>
    <div class="col-md-3 ms-md-auto">.col-md-3 .ms-md-auto</div>
  </div>
  <div class="row">
    <div class="col-auto me-auto">.col-auto .me-auto</div>
    <div class="col-auto">.col-auto</div>
  </div>
</div>

獨立欄類別(Standalone column classes)

.col-* 類別也可以在 .row 外部使用,為元素提供特定寬度。當欄類別用作非列的直接子元素時,內距會被省略。

.col-3: width of 25%
.col-sm-9: width of 75% above sm breakpoint
html
<div class="col-3 p-3 mb-2">
  .col-3: width of 25%
</div>

<div class="col-sm-9 p-3">
  .col-sm-9: width of 75% above sm breakpoint
</div>

這些類別可以與通用類別一起使用來建立響應式浮動圖片。如果文字較短,請確保將內容包裝在 .clearfix 包裝器中以清除浮動。

PlaceholderResponsive floated image

A paragraph of placeholder text. We’re using it here to show the use of the clearfix class. We’re adding quite a few meaningless phrases here to demonstrate how the columns interact here with the floated image.

As you can see the paragraphs gracefully wrap around the floated image. Now imagine how this would look with some actual content in here, rather than just this boring placeholder text that goes on and on, but actually conveys no tangible information at. It simply takes up space and should not really be read.

And yet, here you are, still persevering in reading this placeholder text, hoping for some more insights, or some hidden easter egg of content. A joke, perhaps. Unfortunately, there’s none of that here.

html
<div class="clearfix">
  <img src="..." class="col-md-6 float-md-end mb-3 ms-md-3" alt="...">

  <p>
    A paragraph of placeholder text. We’re using it here to show the use of the clearfix class. We’re adding quite a few meaningless phrases here to demonstrate how the columns interact here with the floated image.
  </p>

  <p>
    As you can see the paragraphs gracefully wrap around the floated image. Now imagine how this would look with some actual content in here, rather than just this boring placeholder text that goes on and on, but actually conveys no tangible information at. It simply takes up space and should not really be read.
  </p>

  <p>
    And yet, here you are, still persevering in reading this placeholder text, hoping for some more insights, or some hidden easter egg of content. A joke, perhaps. Unfortunately, there’s none of that here.
  </p>
</div>