Bootstrap 5 Flex 佈局
Flexbox
Bootstrap 3 與 Bootstrap 4 & 5 之間最大的區別在於,Bootstrap 5 現在使用 flexbox 來處理佈局,而不是浮動。
Flexible Box Layout Module,使設計靈活的響應式佈局結構更加容易,無需使用浮動或定位。如果您不熟悉 flex,可以在我們的 CSS Flexbox 教程 中閱讀相關內容。
注意: Flexbox 不支援 IE9 及更早版本。
如果您需要支援 IE8-9,請使用 Bootstrap 3。 它是最穩定的 Bootstrap 版本,並且仍然受到團隊的支援以進行關鍵的 bug 修復和文件更改。但是,它不會新增任何新功能。
要建立 flexbox 容器並將直接子元素轉換為 flex 專案,請使用 d-flex
類
示例
示例
<div class="d-flex p-3 bg-secondary text-white">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
自己動手試一試 »要建立內聯 flexbox 容器,請使用 d-inline-flex
類
示例
示例
<div class="d-inline-flex p-3 bg-secondary text-white">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
自己動手試一試 »水平方向
使用 .flex-row
按水平方向(並排)顯示 flex 專案。這是預設設定。
提示: 使用 .flex-row-reverse
來右對齊水平方向
示例
示例
<div class="d-flex flex-row bg-secondary">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
<div class="d-flex flex-row-reverse bg-secondary">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
自己動手試一試 »垂直方向
使用 .flex-column
按垂直方向(堆疊)顯示 flex 專案,或使用 .flex-column-reverse
來反轉垂直方向
示例
示例
<div class="d-flex flex-column">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
<div class="d-flex flex-column-reverse">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
自己動手試一試 »內容對齊
使用 .justify-content-*
類來更改 flex 專案的對齊方式。有效類包括 start
(預設)、end
、center
、between
或 around
示例
示例
<div class="d-flex justify-content-start">...</div>
<div class="d-flex justify-content-end">...</div>
<div class="d-flex justify-content-center">...</div>
<div class="d-flex justify-content-between">...</div>
<div class="d-flex justify-content-around">...</div>
自己動手試一試 »填充 / 等寬
在 flex 專案上使用 .flex-fill
來強制它們具有相同的寬度
示例
示例
<div class="d-flex">
<div class="p-2 bg-info flex-fill">Flex item 1</div>
<div class="p-2 bg-warning flex-fill">Flex item 2</div>
<div class="p-2 bg-primary flex-fill">Flex item 3</div>
</div>
自己動手試一試 »增長
在 flex 專案上使用 .flex-grow-1
來佔據剩餘空間。在下面的示例中,前兩個 flex 專案佔據它們所需的空間,而最後一個專案佔據所有可用空間。
示例
示例
<div class="d-flex">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary flex-grow-1">Flex item 3</div>
</div>
自己動手試一試 »提示: 在 flex 專案上使用 .flex-shrink-1
使其在需要時收縮。
順序
使用 .order
類來更改特定 flex 專案的視覺順序。有效類從 0 到 5,數字越小優先順序越高(order-1 顯示在 order-2 之前,依此類推)。
示例
示例
<div class="d-flex bg-secondary">
<div class="p-2 bg-info order-3">Flex item 1</div>
<div class="p-2 bg-warning order-2">Flex item 2</div>
<div class="p-2 bg-primary order-1">Flex item 3</div>
</div>
自己動手試一試 »自動邊距
使用 .ms-auto
(將專案推向右側)或 .me-auto
(將專案推向左側)輕鬆地為 flex 專案新增自動邊距。
示例
示例
<div class="d-flex bg-secondary">
<div class="p-2 ms-auto bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 bg-primary">Flex item 3</div>
</div>
<div class="d-flex bg-secondary">
<div class="p-2 bg-info">Flex item 1</div>
<div class="p-2 bg-warning">Flex item 2</div>
<div class="p-2 me-auto bg-primary">Flex item 3</div>
</div>
自己動手試一試 »換行
使用 .flex-nowrap
(預設)、.flex-wrap
或 .flex-wrap-reverse
來控制 flex 專案如何在 flex 容器中換行。
點選下面的按鈕,透過更改示例框中 flex 專案的換行來檢視三個類之間的區別
示例
示例
<div class="d-flex flex-wrap">..</div>
<div class="d-flex flex-wrap-reverse">..</div>
<div class="d-flex flex-nowrap">..</div>
自己動手試一試 »內容對齊
使用 .align-content-*
類控制聚集的 flex 專案的垂直對齊。有效類包括 .align-content-start
(預設)、.align-content-end
、.align-content-center
、.align-content-between
、.align-content-around
和 .align-content-stretch
。
注意: 這些類對單行 flex 專案無效。
示例
示例
<div class="d-flex flex-wrap align-content-start">..</div>
<div class="d-flex flex-wrap align-content-end">..</div>
<div class="d-flex flex-wrap align-content-center">..</div>
<div class="d-flex flex-wrap align-content-around">..</div>
<div class="d-flex flex-wrap align-content-stretch">..</div>
自己動手試一試 »專案對齊
使用 .align-items-*
類控制單行 flex 專案的垂直對齊。有效類包括 .align-items-start
、.align-items-end
、.align-items-center
、.align-items-baseline
和 .align-items-stretch
(預設)。
點選下面的按鈕,檢視五個類之間的區別
示例
示例
<div class="d-flex align-items-start">..</div>
<div class="d-flex align-items-end">..</div>
<div class="d-flex align-items-center">..</div>
<div class="d-flex align-items-baseline">..</div>
<div class="d-flex align-items-stretch">..</div>
自己動手試一試 »自對齊
使用 .align-self-*
類控制特定 flex 專案的垂直對齊。有效類包括 .align-self-start
、.align-self-end
、.align-self-center
、.align-self-baseline
和 .align-self-stretch
(預設)。
點選下面的按鈕,檢視五個類之間的區別
示例
示例
<div class="d-flex bg-light" style="height:150px">
<div class="p-2 border">Flex item 1</div>
<div class="p-2 border align-self-start">Flex item 2</div>
<div class="p-2 border">Flex item 3</div>
</div>
自己動手試一試 »響應式 Flex 類
所有 flex 類都帶有附加的響應式類,可以輕鬆地在特定螢幕尺寸上設定特定的 flex 類。
*
符號可以替換為 sm, md, lg, xl 或 xxl,分別代表小、中、大、超大和特大螢幕。
類 | 描述 | 示例 |
---|---|---|
Flex Container | ||
.d-*-flex |
為不同螢幕建立 flexbox 容器 | 試一試 |
.d-*-inline-flex |
為不同螢幕建立內聯 flexbox 容器 | 試一試 |
方向 | ||
.flex-*-row |
在不同螢幕上水平顯示 flex 專案 | 試一試 |
.flex-*-row-reverse |
在不同螢幕上水平顯示 flex 專案,並右對齊 | 試一試 |
.flex-*-column |
在不同螢幕上垂直顯示 flex 專案 | 試一試 |
.flex-*-column-reverse |
在不同螢幕上垂直顯示 flex 專案,並反轉順序 | 試一試 |
內容對齊 | ||
.justify-content-*-start |
在不同螢幕上從開始(左對齊)顯示 flex 專案 | 試一試 |
.justify-content-*-end |
在不同螢幕上結束(右對齊)顯示 flex 專案 | 試一試 |
.justify-content-*-center |
在不同螢幕的 flex 容器中心顯示 flex 專案 | 試一試 |
.justify-content-*-between |
在不同螢幕上將 flex 專案顯示為“之間” | 試一試 |
.justify-content-*-around |
在不同螢幕上將 flex 專案顯示為“圍繞” | 試一試 |
填充 / 等寬 | ||
.flex-*-fill |
在不同螢幕上強制 flex 專案成為等寬 | 試一試 |
增長 | ||
.flex-*-grow-0 |
在不同螢幕上不讓專案增長 | |
.flex-*-grow-1 |
在不同螢幕上使專案增長 | |
收縮 | ||
.flex-*-shrink-0 |
在不同螢幕上不讓專案收縮 | |
.flex-*-shrink-1 |
在不同螢幕上使專案收縮 | |
順序 | ||
.order-*-0-12 |
在小螢幕上更改 0 到 5 的順序 | 試一試 |
換行 | ||
.flex-*-nowrap |
在不同螢幕上不換行專案 | 試一試 |
.flex-*-wrap |
在不同螢幕上換行專案 | 試一試 |
.flex-*-wrap-reverse |
在不同螢幕上反轉專案的換行 | 試一試 |
內容對齊 | ||
.align-content-*-start |
在不同螢幕上從開頭對齊收集的專案 | 試一試 |
.align-content-*-end |
在不同螢幕上在末尾對齊收集的專案 | 試一試 |
.align-content-*-center |
在不同螢幕上居中對齊收集的專案 | 試一試 |
.align-content-*-around |
在不同螢幕上“圍繞”對齊收集的專案 | 試一試 |
.align-content-*-stretch |
在不同螢幕上拉伸收集的專案 | 試一試 |
專案對齊 | ||
.align-items-*-start |
在不同螢幕上從開頭對齊單行專案 | 試一試 |
.align-items-*-end |
在不同螢幕上在末尾對齊單行專案 | 試一試 |
.align-items-*-center |
在不同螢幕上居中對齊單行專案 | 試一試 |
.align-items-*-baseline |
在不同螢幕上將單行專案對齊到基線 | 試一試 |
.align-items-*-stretch |
在不同螢幕上拉伸單行專案 | 試一試 |
自對齊 | ||
.align-self-*-start |
在不同螢幕上從開頭對齊一個 flex 專案 | 試一試 |
.align-self-*-end |
在不同螢幕上在末尾對齊一個 flex 專案 | 試一試 |
.align-self-*-center |
在不同螢幕上居中對齊一個 flex 專案 | 試一試 |
.align-self-*-baseline |
在不同螢幕上將 flex 專案對齊到基線 | 試一試 |
.align-self-*-stretch |
在不同螢幕上拉伸一個 flex 專案 | 試一試 |