CSS3 Flexbox 布局完全指南:flex容器/项目/对齐/换行
Introduction
CSS3 Flexbox(弹性盒布局)是现代 CSS 布局的核心利器,它提供了一种高效的方式来分配盒模型空间、对齐内容以及构建响应式界面。无论是一行导航栏、商品卡片列表,还是垂直居中对话框,Flexbox 都能以极简的代码实现以往需要大量浮动和定位才能完成的效果。本指南从容器属性到项目属性全覆盖,配以完整示例,助你彻底掌握 Flexbox 布局。
基础语法
1. 容器属性(display: flex)
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
align-content: flex-start;
gap: 20px;
}
2. 项目属性
.flex-item {
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
flex: 1 0 auto;
align-self: center;
order: 1;
}
完整代码示例
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox 布局实战</title>
<style>
- { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: Arial, sans-serif; padding: 20px; }
h2 { margin: 30px 0 15px; border-left: 4px solid #3498db; padding-left: 10px; }
h3 { margin: 15px 0 10px; color: #555; }
.demo-box { background: #f5f5f5; border-radius: 8px; padding: 15px; margin-bottom: 20px; }
.navbar { display: flex; justify-content: space-between; align-items: center; background: #2c3e50; padding: 15px 20px; border-radius: 8px; }
.navbar .logo { color: #fff; font-size: 20px; font-weight: bold; }
.navbar ul { display: flex; list-style: none; gap: 20px; }
.navbar a { color: #ecf0f1; text-decoration: none; }
.navbar .btn { background: #e74c3c; padding: 8px 16px; border-radius: 4px; color: #fff; }
.card-grid { display: flex; flex-wrap: wrap; gap: 15px; }
.card { flex: 1 1 200px; background: #fff; border-radius: 8px; padding: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); text-align: center; }
.card.featured { flex: 2 1 300px; background: linear-gradient(135deg, #667eea, #764ba2); color: #fff; }
.modal-wrapper { display: flex; justify-content: center; align-items: center; height: 200px; background: #eee; border-radius: 8px; }
.modal { background: #fff; padding: 30px 40px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.2); text-align: center; }
.form-row { display: flex; gap: 10px; align-items: center; }
.form-row label { white-space: nowrap; }
.form-row input { flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }
.form-row button { flex-shrink: 0; }
</style>
</head>
<body>
<h2>1. 响应式导航栏</h2>
<nav class="navbar">
<div class="logo">MySite</div>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">产品</a></li>
<li><a href="#">关于我们</a></li>
</ul>
<a href="#" class="btn">登录</a>
</nav>
<h2>2. 弹性卡片网格</h2>
<div class="card-grid">
<div class="card"><h3>卡片1</h3><p>flex: 1 1 200px</p></div>
<div class="card featured"><h3>特色卡片</h3><p>flex: 2 1 300px</p></div>
<div class="card"><h3>卡片3</h3><p>flex: 1 1 200px</p></div>
</div>
<h2>3. 垂直水平居中(弹窗)</h2>
<div class="modal-wrapper">
<div class="modal"><h3>登录成功</h3><p>欢迎回来!</p></div>
</div>
<h2>4. 表单行布局</h2>
<div class="demo-box">
<div class="form-row">
<label for="name">姓名:</label>
<input type="text" id="name" placeholder="请输入姓名">
<button>提交</button>
</div>
<div class="form-row" style="margin-top:10px">
<label for="email">邮箱:</label>
<input type="email" id="email" placeholder="请输入邮箱">
<button>提交</button>
</div>
</div>
</body>
</html>
常见问题
- flex: 1 是什么意思? 等同于
flex: 1 1 0%,即放大比例1、缩小比例1、基准尺寸0%。会让项目均分剩余空间。 - justify-content 和 align-items 区别? justify-content 控制主轴方向的对齐(默认水平),align-items 控制侧轴方向(默认垂直)。
- flex-basis 和 width 哪个优先? flex-basis 更适合 Flex 项目,它描述的是主轴方向上的基准尺寸,会覆盖 width。
- 子元素不换行怎么办? 检查父容器是否有
flex-wrap: wrap,以及 flex-basis 设置是否合理。