justify-content
、align-items
、align-content
的使用差異flex-wrap
與 flex-flow
讓元素自動換行align-self
微調單一元素位置body {
font-family: "Microsoft JhengHei", sans-serif;
margin: 20px;
background: #f5f5f5;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.container {
display: flex;
margin: 20px auto;
gap: 10px;
padding: 10px;
background: #ecf0f1;
border-radius: 8px;
}
.row {
flex-direction: row; /* 水平排列 */
justify-content: space-around;
align-items: center;
}
.column {
flex-direction: column; /* 垂直排列 */
align-items: flex-start;
}
.item {
flex: 1;
padding: 20px;
background: #3498db;
color: white;
text-align: center;
border-radius: 6px;
}
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
background: #9b59b6;
color: white;
border-radius: 8px;
margin-top: 30px;
}
justify-content
、align-items
、align-content
的差異flex-wrap
在多列布局中的應用align-self
調整單一項目的對齊方式justify-content
想控制垂直方向 → ✅ 用 align-items
或 align-content
flex-wrap
→ ✅ 元素太多時會擠爆容器align-self
做單獨調整height
或使用 min-height
穩定結構justify-content
各種排列方式align-items
調整垂直對齊flex-wrap
換行效果align-self
align-content
控制多行對齊今天的 Flexbox 課程更進一步,學會了如何「讓版面自己對齊」。以前常常靠 margin、padding 微調位置,現在只要用 justify-content
和 align-items
就能完成,整個世界都乾淨了許多。
我特別喜歡 align-self
,因為它能讓單一元素跳脫一般規則,像是在整齊的隊伍裡稍微站高一點,很靈活也很好玩。flex-wrap
也讓我感受到彈性布局的魅力,版面不再被固定寬度限制。
這堂課讓我理解 Flexbox 不只是讓元素「排好」,而是讓布局更「有智慧」。未來進到 Grid 時,應該會更容易掌握整體版面思維。