CSS 彈性盒子 Flexbox
Flexbox 基本概念
主軸與交叉軸
Flexbox 佈局基於兩個軸線:
- 主軸 (Main Axis): 由
flex-direction定義的軸線,flex items 沿著主軸排列 - 交叉軸 (Cross Axis): 垂直於主軸的軸線
flex-direction: row (預設)
┌─────────────────────────────────┐
│ ↓ Cross Axis │
│ ┌───┐ ┌───┐ ┌───┐ │
│ │ 1 │ │ 2 │ │ 3 │ → Main Axis │
│ └───┘ └───┘ └───┘ │
└─────────────────────────────────┘
flex-direction: column
┌─────────────────┐
│ → Cross Axis │
│ ┌───┐ │
│ │ 1 │ ↓ │
│ └───┘ │
│ ┌───┐ Main │
│ │ 2 │ Axis │
│ └───┘ │
│ ┌───┐ │
│ │ 3 │ │
│ └───┘ │
└─────────────────┘
容器與項目
- Flex Container: 設定
display: flex或display: inline-flex的父元素 - Flex Items: Flex Container 的直接子元素
Flex Container 屬性
1. display
啟用 Flexbox 佈局:
.container {
display: flex; /* 或 inline-flex */
}
2. flex-direction
定義主軸方向,決定 flex items 的排列方向:
.container {
flex-direction: row; /* 預設值,水平從左到右 */
flex-direction: row-reverse; /* 水平從右到左 */
flex-direction: column; /* 垂直從上到下 */
flex-direction: column-reverse; /* 垂直從下到上 */
}
flex-direction 範例
觀察不同 flex-direction 值如何改變項目排列方向
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
align-content: stretch;
flex-wrap: nowrap;
gap: 10px;1
2
3
4
3. justify-content
定義 flex items 在主軸上的對齊方式:
.container {
justify-content: flex-start; /* 預設值,靠主軸起點對齊 */
justify-content: flex-end; /* 靠主軸終點對齊 */
justify-content: center; /* 置中對齊 */
justify-content: space-between; /* 兩端對齊,項目間距相等 */
justify-content: space-around; /* 項目兩側間距相等 */
justify-content: space-evenly; /* 所有間距完全相等 */
}
間距示意
space-between: [1]────[2]────[3]
space-around: ──[1]────[2]────[3]──
space-evenly: ───[1]───[2]───[3]───
justify-content 範例
調整 justify-content 觀察主軸上的對齊效果
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
align-content: stretch;
flex-wrap: nowrap;
gap: 10px;1
2
3
4
4. align-items
定義 flex items 在交叉軸上的對齊方式:
.container {
align-items: stretch; /* 預設值,拉伸填滿容器 */
align-items: flex-start; /* 靠交叉軸起點對齊 */
align-items: flex-end; /* 靠交叉軸終點對齊 */
align-items: center; /* 交叉軸置中對齊 */
align-items: baseline; /* 基線對齊 */
}
align-items 範例
調整 align-items 觀察交叉軸上的對齊效果
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
align-content: stretch;
flex-wrap: nowrap;
gap: 10px;1
2
3
4
5. flex-wrap
定義 flex items 是否換行:
.container {
flex-wrap: nowrap; /* 預設值,不換行 */
flex-wrap: wrap; /* 換行,第一行在上方 */
flex-wrap: wrap-reverse; /* 換行,第一行在下方 */
}
重要觀念
flex-wrap 的值決定了 flex container 是單行容器還是多行容器:
flex-wrap: nowrap→ 單行容器 (即使項目很多也會強制在一行)flex-wrap: wrap或wrap-reverse→ 多行容器 (即使視覺上只有一行)
這個定義會影響 align-content 屬性是否生效。參考:MDN - flex-wrap
flex-wrap 範例
當容器寬度不足時,觀察 flex-wrap 的換行效果。試著切換到 nowrap 或 wrap-reverse 看看差異!
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
align-content: stretch;
flex-wrap: wrap;
gap: 30px;1
2
3
4
5
6
7
8
9
10
11
12
6. gap (row-gap, column-gap)
定義 flex items 之間的間距:
.container {
gap: 20px; /* 行列間距都是 20px */
gap: 20px 10px; /* 行間距 20px, 列間距 10px */
/* 或分別設定 */
row-gap: 20px;
column-gap: 10px;
}
gap 範例
調整 gap 值觀察項目間距變化
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
align-content: stretch;
flex-wrap: wrap;
gap: 30px;1
2
3
4
5
6
7
8
7. align-content
定義多行 flex container 中,各行在交叉軸上的對齊與分布方式:
.container {
flex-wrap: wrap;
align-content: stretch; /* 預設值 */
align-content: flex-start;
align-content: flex-end;
align-content: center;
align-content: space-between;
align-content: space-around;
align-content: space-evenly;
}
重要規範
根據 CSS Flexbox 規範,align-content 的生效條件:
- ✅ 多行容器 (
flex-wrap: wrap或wrap-reverse) →align-content生效- 即使視覺上只有一行,只要設定了
flex-wrap: wrap,就是多行容器
- 即使視覺上只有一行,只要設定了
- ❌ 單行容器 (
flex-wrap: nowrap) →align-content無效- 單行容器請使用
align-items
- 單行容器請使用
關鍵概念:單行/多行容器的定義取決於 flex-wrap 的值,而非實際的視覺行數。
align-content 範例
當有多行時,調整 align-content 觀察行與行之間的對齊效果 (需要 flex-wrap: wrap)
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
align-content: stretch;
flex-wrap: wrap;
gap: 10px;1
2
3
4
5
6
7
8
9
10
11
12