弹性盒子 Flexbox
布局这事,传统 CSS 一直不太顺手:想让几个元素垂直居中,得拿 margin 一点点抠;想让它们均匀分布,得靠 float 再清一堆浮动。弹性盒子(Flexible Box)就是为这些场景生的——父容器加一句 display: flex,子元素的排列、对齐、分配空间全变成几行声明的事。
下面每个知识点都配了能跑的代码。新建一个 html 文件粘进去,浏览器打开就能看效果。
从一句 display: flex 开始
<div class="box">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
<style>
.box {
display: flex;
border: 2px dotted #608bba;
}
.box div {
width: 100px;
border: 2px solid #608bba;
background-color: rgba(96, 139, 168, 0.2);
}
</style>加了 display: flex,box 的三个子元素不再上下堆叠,而是排成一行。后面所有玩法都是围绕这一行怎么排、怎么对齐、空间怎么分来展开的。
主轴和交叉轴
弹性盒子的对齐全绕两条轴转:
- 主轴:子元素排列的方向,默认水平从左到右,方向由
flex-direction决定。 - 交叉轴:垂直于主轴的那条。主轴横着,交叉轴就竖着。
两个最重要的属性各管一条轴:
| 属性 | 管哪条轴 | 常用值 |
|---|---|---|
| justify-content | 主轴 | center、space-between、space-around |
| align-items | 交叉轴 | center、flex-start、flex-end、stretch |
flex-direction 一换,轴就跟着换:
.box {
display: flex;
flex-direction: row; /* 默认,主轴水平 */
/* flex-direction: column; 主轴变竖直,子元素从上往下排 */
}主轴变竖了之后,justify-content 管的就是上下怎么排,align-items 管左右——属性没变,管的方向跟着主轴走。
justify-content:主轴上怎么分空间
<div class="box">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
<style>
.box {
display: flex;
justify-content: space-between; /* 改这一行看效果 */
border: 2px dotted #608bba;
}
.box div {
width: 100px;
border: 2px solid #608bba;
}
</style>把 justify-content 换成不同的值感受一下:
flex-start:全挤在开头(默认)center:挤在中间flex-end:挤在末尾space-between:两端贴边,中间平分空隙space-around:每个元素两侧都有空隙,中间的空隙是边缘的两倍space-evenly:所有空隙完全一样宽
align-items:交叉轴上怎么对齐
align-items 默认值是 stretch——子元素被拉成和容器一样高,这就是为什么 flex 布局里几张卡片天生等高。
.box {
display: flex;
align-items: center; /* 换成 stretch / flex-start / flex-end / baseline 对比 */
height: 200px;
}容器给个高度,换值看效果最直观。baseline 比较少用,它按第一行文字的基线对齐。
两行代码完美居中
网上流传很久的「水平垂直居中怎么写」,在 flex 里就两行:
.box {
display: flex;
justify-content: center; /* 主轴(水平)居中 */
align-items: center; /* 交叉轴(垂直)居中 */
height: 300px;
}<div class="box">
<div class="card">我被居中了</div>
</div>单独照顾某一个元素
align-items 管全体,想单独动某个元素用 align-self:
.box {
display: flex;
align-items: flex-start;
}
.box .special {
align-self: center; /* 只有它居中,其余贴顶 */
}主轴上没有 justify-self 这种东西,但有个更好用的写法——margin 设成 auto:
<nav class="menu">
<div>首页</div>
<div>文章</div>
<div class="push">登录</div>
</nav>
<style>
.menu {
display: flex;
gap: 16px;
}
.push {
margin-left: auto; /* 把「登录」推到最右边 */
}
</style>auto 的 margin 会吃掉主轴上所有剩余空间,导航栏左边放菜单、右边放登录按钮,一个 margin-left: auto 就够了。
换行和间距
子元素默认往一行里硬塞,塞不下会压缩变形。放开换行用 flex-wrap,元素之间的固定间距用 gap:
<div class="cards">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
<div class="card">5</div>
</div>
<style>
.cards {
display: flex;
flex-wrap: wrap; /* 一行放不下就换行 */
gap: 12px; /* 行和列的间距都是 12px */
}
.card {
width: 150px;
height: 80px;
border: 1px solid #608bba;
background-color: rgba(96, 139, 168, 0.2);
}
</style>gap: 12px 行列间距相同;想分开写就是 row-gap 和 column-gap。有了 gap 就不需要再写一堆 margin-right 然后给最后一个元素清边距。
flex:1 —— 分蛋糕
容器有富余空间时,谁分得多由子元素的 flex 决定。最常用的是 flex: 1:把剩余空间全按比例分掉。
典型场景:侧边栏固定宽度,内容区占剩下的全部。
<div class="layout">
<aside>侧边栏 200px</aside>
<main>内容区自动占满</main>
</div>
<style>
.layout {
display: flex;
min-height: 300px;
}
aside {
width: 200px;
border: 1px solid #608bba;
}
main {
flex: 1; /* 吃掉除侧边栏以外的所有宽度 */
background-color: rgba(96, 139, 168, 0.2);
}
</style>flex 是三个属性的组合:flex-grow(分剩余空间的比例)、flex-shrink(空间不够时收缩的比例)、flex-basis(分配前的基准尺寸)。日常写 flex: 1 和 flex: none(不许伸也不许缩)基本就够用。
一张速查
| 写在哪 | 属性 | 干什么 |
|---|---|---|
| 容器 | display: flex | 开启弹性布局 |
| 容器 | flex-direction | 主轴方向:row / column |
| 容器 | justify-content | 主轴对齐 |
| 容器 | align-items | 交叉轴对齐 |
| 容器 | flex-wrap | 允许换行 |
| 容器 | gap | 元素间距 |
| 子元素 | flex: 1 | 按比例分剩余空间 |
| 子元素 | align-self | 单独改交叉轴对齐 |
| 子元素 | margin: auto | 主轴上吃掉剩余空间 |
日常布局里这几条基本覆盖了,遇到具体场景回来查就行。更复杂的二维布局(同时按行和列排)交给 Grid,那是另一套东西了。