CSS Flexbox 与 Grid 速查表
两套布局系统,一页纸讲完。Flexbox 沿一个方向排东西;Grid 同时管行和列。选对工具,一半的 CSS 就不用写了。
该用哪一个?
| 场景 | 用 | 原因 |
|---|---|---|
| 一行或一列元素(导航栏、按钮组、表单行) | Flexbox | 一维:元素沿一条轴流动 |
| 同时有行和列的页面布局(侧栏 + 内容、卡片网格) | Grid | 二维:行和列一起控制 |
| 把一个东西水平垂直都居中 | 都可以 | Grid:place-items: center 一行搞定;Flexbox 要写两个属性,见下文 |
| 屏幕变窄时元素要自动换行 | Flexbox(flex-wrap: wrap)或 Grid(auto-fill) | 都能做;Grid 的列对得齐,Flexbox 的行流动更自然 |
| 列数不确定、但要等宽 | Grid(repeat(auto-fill, minmax(…))) | Flexbox 没法让跨行的列对齐 |
Flexbox——容器
写在父元素上。主轴沿 flex-direction 的方向,交叉轴与它垂直。
.row {
display: flex; /* 或 inline-flex */
flex-direction: row; /* row | row-reverse | column | column-reverse */
justify-content: center; /* 主轴方向 */
align-items: center; /* 交叉轴方向,单行 */
flex-wrap: wrap; /* nowrap | wrap | wrap-reverse */
align-content: center; /* 交叉轴方向,只对多行有效 */
gap: 12px; /* 行间距 列间距 */
flex-flow: row wrap; /* 简写:方向 + 换行 */
}
| 属性 | 取值 | 作用 |
|---|---|---|
display: flex | flex / inline-flex | 把元素变成弹性容器,子元素成为弹性子项 |
flex-direction | row(默认)· row-reverse · column · column-reverse | 定主轴方向——同时决定了 "justify" 和 "align" 各管哪条轴 |
justify-content | flex-start(默认)· flex-end · center · space-between · space-around · space-evenly | 沿主轴分配元素 |
align-items | stretch(默认)· flex-start · flex-end · center · baseline | 沿交叉轴对齐元素,针对单行 |
flex-wrap | nowrap(默认)· wrap · wrap-reverse | 放不下时是否换到下一行,而不是继续压缩 |
align-content | stretch · flex-start · flex-end · center · space-between · space-around · space-evenly | 沿交叉轴分配各行——没有换行时不起作用 |
gap | 例如 12px 或 8px 16px(行 列) | 只在元素之间留空——容器边缘没有间距 |
间距的区别:space-between 让第一个元素贴左、最后一个贴右,中间的间距相等。space-around 给每个元素左右都留相等的空间,所以边缘的间距只有中间的一半。space-evenly 让所有间距——包括两边的——完全相等。
Flexbox——子项
写在子元素上。三个属性决定子项如何放大缩小,flex 简写把它们打包在一起。
.item {
flex-grow: 1; /* 分一份剩余空间(0 = 不放大) */
flex-shrink: 1; /* 空间不够时让出空间(0 = 绝不缩小) */
flex-basis: 0; /* 放大/缩小前的基准尺寸 */
flex: 1 1 0; /* 简写:放大 缩小 基准 */
align-self: center; /* 单独覆盖这个子项的 align-items */
order: 2; /* 默认 0;负数排最前面 */
margin-left: auto; /* 吃掉剩余空间——把这个子项推到最右边 */
}
| 简写 | 展开 | 含义 |
|---|---|---|
flex: 1 | 1 1 0% | 无视内容尺寸、平分空间——等宽列的经典写法 |
flex: auto | 1 1 auto | 能放大缩小,但从内容的自然尺寸出发 |
flex: none | 0 0 auto | 固定尺寸——既不放大也不缩小 |
flex: 0 0 200px | — | 固定 200px 的一列,完全不参与伸缩 |
flex: 2 | 2 1 0% | 分到的剩余空间是 flex: 1 兄弟的两倍 |
最常见的坑:弹性子项缩不到内容最小尺寸以下——一个很长的单词或一张宽图会把布局撑爆。在子项上写 min-width: 0(或 overflow: hidden)就能解决。
Grid——容器
写在父元素上。fr 是固定轨道占完之后剩下的自由空间的几分之一。
.grid {
display: grid; /* 或 inline-grid */
grid-template-columns: 200px 1fr 2fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
gap: 16px; /* 行间距 列间距 */
justify-items: stretch; /* 每个格子里默认的对齐方式 */
align-items: stretch;
place-items: center; /* 简写:align-items justify-items */
justify-content: start; /* 网格比容器小时,分配轨道 */
align-content: start;
grid-auto-flow: row; /* row | column | dense */
grid-auto-rows: minmax(100px, auto); /* 没声明的行的尺寸 */
}
| 写法 | 做出什么 |
|---|---|
grid-template-columns: repeat(3, 1fr) | 三列等宽 |
repeat(12, 1fr) | 12 列布局网格——子项按需跨列 |
repeat(auto-fill, minmax(240px, 1fr)) | 响应式卡片网格:能放几列 240px+ 就放几列,空轨道留空 |
repeat(auto-fit, minmax(240px, 1fr)) | 同上,但空轨道会塌掉——卡片被拉伸填满整行 |
grid-template-columns: 240px 1fr | 固定侧栏 + 自适应内容 |
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr) | 两列按 1:2 分配,且允许缩到内容尺寸以下(见文末的坑) |
grid-template-rows: auto 1fr auto | 页眉 / 内容 / 页脚——内容行占掉剩余高度 |
Grid——子项
写在子元素上。线号从 1 开始数;span 表示从起始线往后数几条轨道。
.item {
grid-column: 1 / 3; /* 从第 1 条线到第 3 条线(跨 2 列) */
grid-column: 2 / span 2; /* 从第 2 条线起,跨 2 列 */
grid-row: 1 / -1; /* -1 = 最后一条线:跨所有行 */
grid-area: sidebar; /* grid-template-areas 里起的名字 */
grid-area: 1 / 1 / 3 / 4; /* 行起 / 列起 / 行止 / 列止 */
justify-self: center; /* start | end | center | stretch */
align-self: center;
place-self: center; /* 简写:align-self justify-self */
}
| 属性 | 作用 |
|---|---|
grid-column / grid-row | 把子项放到指定的线上——1 / 3、span 2,或 1 / -1 表示"全部" |
grid-area | grid-template-areas 里的区域名,或四条线的简写 |
justify-self / align-self | 单独覆盖容器的 justify-items / align-items |
grid-auto-flow: dense | 回填跨列子项留下的空洞——适合类瀑布流网格,但视觉顺序会变 |
居中,以及三个一辈子都在用的布局
/* 一个东西,水平垂直都居中——一行搞定 */
.center { display: grid; place-items: center; }
/* Flexbox 的写法也一样 */
.center { display: flex; align-items: center; justify-content: center; }
/* 侧栏 + 内容 */
.layout { display: grid; grid-template-columns: 240px 1fr; gap: 24px; }
/* 页脚吸底:内容把页脚顶到底部 */
.page { display: grid; grid-template-rows: auto 1fr auto; min-height: 100vh; }
/* 命名区域的三栏布局 */
.holy-grail {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-areas: "nav main aside";
}
.holy-grail nav { grid-area: nav; }
.holy-grail main { grid-area: main; }
.holy-grail aside { grid-area: aside; }
让两者翻车的坑
| 症状 | 原因 | 解决办法 |
|---|---|---|
| 弹性子项溢出容器,怎么都不缩小 | 子项缩不到内容最小尺寸以下 | 给子项写 min-width: 0(或 overflow: hidden) |
| 等宽的弹性列宽度不一样 | flex: 1 和 flex: auto 的区别:1 把基准设为 0%(无视内容),auto 保留内容尺寸 | 想要真正等宽就用 flex: 1 |
网格列比它分到的 1fr 还宽 | 1fr 实际是 minmax(auto, 1fr)——内容能把它撑宽 | 写 minmax(0, 1fr) 代替 1fr |
align-content 写了没反应 | 它只分配各行——只有一行弹性子项、或只有一行网格轨道时,没东西可分 | 单行用 align-items;align-content 需要换行或多行 |
space-between 两边没有间距 | 它就是这么设计的——第一个贴左、最后一个贴右 | 用 space-evenly(处处等距),或 gap + justify-content: center |
| 网格行里的百分比高度不起作用 | 百分比要参照一个确定的高度——auto 的行没有确定高度 | 给容器一个高度(min-height: 100vh),或用 1fr 的行 |
旧浏览器里 Flexbox 的 gap 不生效 | Flexbox 的 gap 是 2021 年才进浏览器的(Safari 14.1+) | 还要兼容很老的 Safari 就用 margin 做后备 |
继续探索
- 颜色转换器——CSS 里颜色的 HEX / RGB / HSL / CMYK 互转,附带对比度检查
- Markdown 语法速查表——另一张每周都要翻的一页纸
- Regex 速查表——校验布局里要展示的那些值