CSS Flexbox(彈性盒子)簡介
Flexbox 是一種一維布局模型,用于高效處理元素在容器內的空間分配、對齊和排序。它通過父容器(flex container
)和子元素(flex items
)的配合實現靈活響應式布局。
核心概念
-
容器屬性(作用于父元素)
display: flex | inline-flex;
flex-direction
: 主軸方向(row | column | row-reverse | column-reverse
)flex-wrap
: 換行(nowrap | wrap | wrap-reverse
)justify-content
: 主軸對齊(center | space-between | space-around
等)align-items
: 交叉軸對齊(stretch | center | flex-start
等)
-
項目屬性(作用于子元素)
order
: 排序優先級(數值越小越靠前)flex-grow
: 放大比例(默認0
不放大)flex-shrink
: 縮小比例(默認1
可縮小)flex-basis
: 初始尺寸(auto | 長度值
)align-self
: 覆蓋父容器的align-items
關鍵優勢
- 自適應布局:元素自動填充/收縮適應容器。
- 簡化對齊:無需浮動或定位即可實現居中/均分。
- 響應式友好:結合媒體查詢輕松適配不同屏幕。
💡 提示:Flexbox 適合局部布局(如導航、卡片列表),復雜網格布局建議使用 CSS Grid。
以下是整合后的Flexbox示例代碼,每個示例均為完整HTML+CSS文件,可直接運行:
示例1:水平居中導航欄
<!DOCTYPE html>
<html>
<head>
<style>
.navbar {display: flex;justify-content: center;gap: 20px;background: #333;padding: 10px;
}
.navbar > div {color: white;padding: 5px 10px;border-radius: 4px;transition: background 0.3s;
}
.navbar > div:hover {background: #555;cursor: pointer;
}
</style>
</head>
<body>
<div class="navbar"><div>首頁</div><div>產品</div><div>關于</div><div>聯系</div>
</div>
</body>
</html>
示例2:自適應兩欄布局
<!DOCTYPE html>
<html>
<head>
<style>
.container {display: flex;height: 200px;border: 1px solid #ccc;margin-top: 20px;
}
.sidebar {flex: 0 0 200px;background: #3498db;color: white;padding: 20px;font-weight: bold;
}
.content {flex: 1;background: #ecf0f1;padding: 20px;
}
</style>
</head>
<body>
<div class="container"><div class="sidebar">側邊欄導航</div><div class="content"><h3>主要內容區</h3><p>自適應填充剩余空間</p></div>
</div>
</body>
</html>
示例3:響應式相冊布局
<!DOCTYPE html>
<html>
<head>
<style>
.gallery {display: flex;flex-wrap: wrap;justify-content: center;gap: 15px;padding: 20px;background: #f5f5f5;margin-top: 20px;
}
.gallery > div {width: 120px;height: 120px;background: #e74c3c;color: white;display: flex;align-items: center;justify-content: center;border-radius: 8px;box-shadow: 0 2px 4px rgba(0,0,0,0.1);transition: transform 0.3s;
}
.gallery > div:hover {transform: scale(1.05);
}
@media (max-width: 600px) {.gallery > div {width: 100px;height: 100px;}
}
</style>
</head>
<body>
<div class="gallery"><div>風景</div><div>建筑</div><div>人像</div><div>美食</div><div>動物</div><div>夜景</div>
</div>
</body>
</html>
示例4:動態排序卡片
<!DOCTYPE html>
<html>
<head>
<style>
.card-container {display: flex;flex-direction: column;gap: 10px;max-width: 400px;margin-top: 20px;
}
.card {padding: 15px;border: 1px solid #ddd;border-radius: 8px;background: #f9f9f9;
}
.card:nth-child(1) { order: 3; background: #ffebee; }
.card:nth-child(2) { order: 1; background: #e8f5e9; }
.card:nth-child(3) { order: 2; background: #e3f2fd; }
.card:nth-child(4) { order: 4; background: #fff8e1; }
</style>
</head>
<body>
<div class="card-container"><div class="card">卡片3 (order:3)</div><div class="card">卡片1 (order:1)</div><div class="card">卡片2 (order:2)</div><div class="card">卡片4 (order:4)</div>
</div>
</body>
</html>
示例5:圣杯布局(頁眉+主體+頁腳)
<!DOCTYPE html>
<html>
<head>
<style>
body {margin: 0;min-height: 100vh;display: flex;flex-direction: column;
}
header {background: #2c3e50;color: white;padding: 15px;text-align: center;
}
main {flex: 1;display: flex;padding: 20px;gap: 20px;
}
.content {flex: 1;background: #ecf0f1;padding: 20px;border-radius: 8px;
}
.sidebar {width: 200px;background: #3498db;color: white;padding: 20px;border-radius: 8px;
}
footer {background: #34495e;color: white;text-align: center;padding: 10px;
}
</style>
</head>
<body>
<header><h1>網站標題</h1></header>
<main><div class="sidebar"><h3>側邊欄</h3><p>導航菜單</p></div><div class="content"><h2>主要內容</h2><p>使用flex:1自動填充剩余空間</p></div>
</main>
<footer>? 2023 版權信息</footer>
</body>
</html>
關鍵特性演示:
- 容器屬性:
display: flex
、flex-direction
、justify-content
、align-items
- 項目屬性:
flex
、order
、align-self
- 響應式:結合
flex-wrap
和媒體查詢 - 實用技巧:等分布局、垂直居中、間距控制
💡 提示:復制單個示例到HTML文件直接運行,或組合使用創建復雜布局。Flexbox特別適合構建組件級布局(導航欄、卡片、表單等),結合CSS Grid可實現完整的頁面布局體系。