Flexible Box Layout(伸縮盒佈局)是 CSS3 引入的**一維佈局模型**,專為解決「在未知/動態容器空間中,如何最優分配空間給子元素」而設計。它取代了 float、inline-block、table 等舊式佈局手段,成為現代 CSS 佈局基石。
| 術語 | 說明 | CSS 關鍵字 |
|---|---|---|
| Flex Container | 伸縮容器,設定 `display: flex/inline-flex` 的父元素 | flex container |
| Flex Item | 伸縮項目,容器的**直接子元素**(非後代) | flex item |
| Main Axis | 主軸,由 `flex-direction` 決定方向 | main axis |
| Cross Axis | 交叉軸,垂直於主軸 | cross axis |
| Main Start/End | 主軸起點/終點 | main-start / main-end |
| Cross Start/End | 交叉軸起點/終點 | cross-start / cross-end |
| Main Size | 主軸方向尺寸(寬或高) | main size |
| Cross Size | 交叉軸方向尺寸 | cross size |
.container {
display: flex; /* 區塊級伸縮容器 */
/* display: inline-flex; */ /* 行內級伸縮容器 */
}
.container {
flex-direction: row; /* 左→右(預設,LTR) */
flex-direction: row-reverse; /* 右→左 */
flex-direction: column; /* 上→下 */
flex-direction: column-reverse; /* 下→上 */
}
.container {
flex-wrap: nowrap; /* 預設:單行,壓縮項目 */
flex-wrap: wrap; /* 多行,超出換行 */
flex-wrap: wrap-reverse; /* 多行,反向換行 */
}
.container {
flex-flow: row wrap; /* 等同 flex-direction: row; flex-wrap: wrap; */
}
.container {
justify-content: flex-start; /* 預設:靠主軸起點 */
justify-content: flex-end; /* 靠主軸終點 */
justify-content: center; /* 置中 */
justify-content: space-between; /* 首尾貼邊,中間均分 */
justify-content: space-around; /* 兩側間距 = 中間間距一半 */
justify-content: space-evenly; /* 所有間距相等(現代) */
justify-content: stretch; /* 拉伸填滿(需項目無固定主軸尺寸) */
}
.container {
align-items: stretch; /* 預設:拉伸填滿交叉軸 */
align-items: flex-start; /* 靠交叉軸起點 */
align-items: flex-end; /* 靠交叉軸終點 */
align-items: center; /* 交叉軸置中 */
align-items: baseline; /* 基線對齊(文字底部對齊) */
}
.container {
flex-wrap: wrap;
align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch;
}
單行時無效,多行時控制「行群」在交叉軸的分佈。
.item { flex-grow: 1; } /* 剩餘空間按比例分配 */
.item-wide { flex-grow: 2; } /* 獲得雙倍空間 */
.item { flex-shrink: 1; } /* 預設:空間不足時等比縮小 */
.item-no-shrink { flex-shrink: 0; } /* 保持原尺寸,可能溢出 */
.item { flex-basis: 200px; } /* 分配剩餘空間前的初始主軸尺寸 */
.item { flex-basis: auto; } /* 預設:看 width/height,再看內容 */
.item { flex: 1; } /* = flex: 1 1 0% (放大、縮小、基準 0) */
.item { flex: auto; } /* = flex: 1 1 auto (放大、縮小、基準內容) */
.item { flex: none; } /* = flex: 0 0 auto (不放大、不縮小、基準內容) */
.item { flex: 2 1 300px; } /* grow=2, shrink=1, basis=300px */
最佳實踐:幾乎總是用 `flex` 簡寫,避免單獨設定三個屬性導致邏輯衝突。
.special-item {
align-self: flex-end; /* 只有這個項目靠下 */
}
.grail {
display: flex;
min-height: 100vh;
}
.grail-main { flex: 1; } /* 自適應佔滿 */
.grail-left { width: 200px; flex-shrink: 0; order: -1; } /* 固定寬,不縮小,視覺最左 */
.grail-right { width: 200px; flex-shrink: 0; } /* 固定寬,不縮小 */
/* 移動端單欄 */
@media (max-width: 767px) {
.grail { flex-direction: column; }
.grail-left, .grail-right { width: 100%; order: 0; }
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 1.5rem;
}
.nav-links {
display: flex;
gap: 2rem;
list-style: none;
}
.centered {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card {
flex: 1 1 300px; /* 基準 300px,可放大縮小 */
display: flex;
flex-direction: column;
}
.card-content { flex: 1; } /* 內容區撐開,推送按鈕到底部 */
.card-footer { margin-top: auto; } /* 或用 flex: 1 在 content 上 */
.form-group {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 1rem;
}
.form-label {
width: 100px;
flex-shrink: 0;
text-align: right;
}
.form-input { flex: 1; }
| 問題 | 原因 | 解決 |
|---|---|---|
| 項目不換行溢出 | 預設 flex-wrap: nowrap | 加 flex-wrap: wrap |
| 圖片/內容撐破佈局 | flex-shrink: 1 預設縮小,但 min-width 默認 auto(內容尺寸) | 項目加 min-width: 0 或 overflow: hidden |
| align-items: stretch 不生效 | 項目有固定高度或 align-self 覆蓋 | 移除固定高度,檢查 align-self |
| justify-content 失效 | 項目總主軸尺寸 ≥ 容器(無剩餘空間) | 檢查 flex-basis/width,或用空間分配值 |
| order 改變 Tab 順序 | 視覺順序 ≠ DOM 順序 | 謹慎使用,或配合 tabindex 管理 |
Flexbox 以「空間分配」為核心哲學,提供了前所未有的佈局靈活性。掌握**容器 6 大屬性**、**項目 6 大屬性**,理解主軸/交叉軸、基準尺寸/放大縮小比例的運算邏輯,即可輕鬆應對 90% 以上的佈局需求。現代 CSS 佈局,Flexbox 是必修內功。