CSS 布局属性笔记
📐 CSS 布局核心属性与高级技巧速查笔记 (完整版)
这份笔记旨在提供一份关于现代CSS布局技术的全面参考,涵盖核心概念、属性详解、实用技巧以及前沿特性。持续更新中!
🔄 1. Flexbox 布局属性 (一维布局神器)
Flexbox 擅长处理一维空间(行或列)中的元素分布、对齐和顺序控制,尤其适合组件内部布局、导航栏等。
| 属性 | 适用对象 | 常用值 | 说明 |
|---|---|---|---|
display |
容器 | flex / inline-flex |
将元素定义为 Flex 容器。 |
flex-direction |
容器 | row (默认) / row-reverse / column / column-reverse |
定义主轴方向(项目排列方向)。 |
flex-wrap |
容器 | nowrap (默认) / wrap / wrap-reverse |
定义项目是否换行及换行方向。 |
flex-flow |
容器 | <flex-direction> <flex-wrap> |
flex-direction 和 flex-wrap 的简写(如 row wrap)。 |
justify-content |
容器 | flex-start (默认) / flex-end / center / space-between / space-around / space-evenly |
定义项目在主轴上的对齐方式。 |
align-items |
容器 | stretch (默认) / flex-start / flex-end / center / baseline |
定义项目在交叉轴上的默认对齐方式。 |
align-content |
容器 | stretch (默认) / flex-start / flex-end / center / space-between / space-around / space-evenly |
定义多行/多列项目在交叉轴上的对齐方式(单行无效)。 |
order |
项目 | <integer> (默认为0) |
定义项目的排列顺序(数值小的在前)。 |
flex-grow |
项目 | <number> (默认为0) |
定义项目的放大比例(分配剩余空间)。 |
flex-shrink |
项目 | <number> (默认为1) |
定义项目的缩小比例(空间不足时收缩)。 |
flex-basis |
项目 | <length> / auto (默认) / content |
定义项目在分配多余空间之前的初始主轴尺寸。 |
flex |
项目 | none / `[ <'flex-grow'> <'flex-shrink'>? |
|
align-self |
项目 | auto (默认) / stretch / flex-start / flex-end / center / baseline |
允许单个项目有与其他项目不同的交叉轴对齐方式,覆盖 align-items。 |
💡 Flexbox 实用技巧:
margin: auto;在 Flex 项目上能实现完美居中。- 使用
flex: 1;(1 1 0%) 让项目填满剩余空间。 - 利用
order调整响应式布局中元素的视觉顺序(不影响源码或可访问性顺序)。 flex-wrap: wrap;+justify-content: space-between/space-around;轻松创建自适应网格(简单场景)。
🧱 2. Grid 布局属性 (二维布局王者)
Grid 是强大的二维布局系统,可以同时处理行和列,适合构建整体页面框架、复杂卡片布局、杂志式排版等。
| 属性 | 适用对象 | 常用值 | 说明 |
|---|---|---|---|
display |
容器 | grid / inline-grid |
将元素定义为 Grid 容器。 |
grid-template-columns |
容器 | <track-size> ... / repeat() / minmax() / auto-fill / auto-fit / [linename] |
定义列轨道的大小和结构(如 1fr 200px / repeat(4, 1fr) / repeat(auto-fit, minmax(250px, 1fr)))。 |
grid-template-rows |
容器 | <track-size> ... |
定义行轨道的大小和结构。 |
grid-template-areas |
容器 | <string> ... |
通过命名区域定义网格模板(如 "header header" "sidebar main" "footer footer")。 |
grid-template |
容器 | <'grid-template-rows'> / <'grid-template-columns'> / <'grid-template-areas'> |
grid-template-rows, grid-template-columns, grid-template-areas 的简写。 |
grid-auto-columns |
容器 | <track-size> ... |
定义隐式创建的列轨道的大小(当项目超出显式网格时)。 |
grid-auto-rows |
容器 | <track-size> ... |
定义隐式创建的行轨道的大小。 |
grid-auto-flow |
容器 | row (默认) / column / row dense / column dense |
定义自动放置算法如何工作(dense 尝试填充空白)。 |
grid |
容器 | 非常复杂的简写,不常用。 | 不建议新手使用,容易混淆。 |
grid-column-gap / column-gap |
容器 | <size> |
定义列与列之间的间距(column-gap 是标准属性)。 |
grid-row-gap / row-gap |
容器 | <size> |
定义行与行之间的间距(row-gap 是标准属性)。 |
grid-gap / gap |
容器 | <row-gap> <column-gap> |
row-gap 和 column-gap 的简写(如 20px 10px)。gap 是标准属性。 |
justify-items |
容器 | stretch (默认) / start / end / center |
定义所有网格项目在行轴(Inline Axis)上的默认对齐方式(类似 Flexbox 的 justify-content 但作用于项目内部)。 |
align-items |
容器 | stretch (默认) / start / end / center |
定义所有网格项目在块轴(Block Axis)上的默认对齐方式(类似 Flexbox 的 align-items)。 |
justify-content |
容器 | start / end / center / stretch / space-around / space-between / space-evenly |
定义整个网格内容在行轴(Inline Axis)上(当网格总宽度小于容器时)的对齐方式。 |
align-content |
容器 | start / end / center / stretch / space-around / space-between / space-evenly |
定义整个网格内容在块轴(Block Axis)上(当网格总高度小于容器时)的对齐方式。 |
place-items |
容器 | <align-items> <justify-items>? |
align-items 和 justify-items 的简写(如 center start)。 |
place-content |
容器 | <align-content> <justify-content>? |
align-content 和 justify-content 的简写。 |
grid-column-start |
项目 | <number> / <name> / span <number> / span <name> / auto |
定义项目起始于哪条列网格线。 |
grid-column-end |
项目 | <number> / <name> / span <number> / span <name> / auto |
定义项目结束于哪条列网格线。 |
grid-row-start |
项目 | <number> / <name> / span <number> / span <name> / auto |
定义项目起始于哪条行网格线。 |
grid-row-end |
项目 | <number> / <name> / span <number> / span <name> / auto |
定义项目结束于哪条行网格线。 |
grid-column |
项目 | <start-line> / <end-line> / span <number> |
grid-column-start 和 grid-column-end 的简写(如 1 / 3 或 1 / span 2)。 |
grid-row |
项目 | <start-line> / <end-line> / span <number> |
grid-row-start 和 grid-row-end 的简写。 |
grid-area |
项目 | <name> (来自 grid-template-areas) / <row-start> / <column-start> / <row-end> / <column-end> |
指定项目放置在哪个命名区域,或作为 grid-row-start, grid-column-start, grid-row-end, grid-column-end 的极简写(如 1 / 1 / 3 / 3)。 |
justify-self |
项目 | stretch / start / end / center |
覆盖容器的 justify-items 设置,定义单个项目在行轴(Inline Axis)上的对齐方式。 |
align-self |
项目 | stretch / start / end / center |
覆盖容器的 align-items 设置,定义单个项目在块轴(Block Axis)上的对齐方式。 |
place-self |
项目 | <align-self> <justify-self>? |
align-self 和 justify-self 的简写。 |
💡 Grid 高级特性与技巧:
- 子网格 (
subgrid): (实验性,逐步支持中) 在网格项目中嵌套使用父网格的行/列轨道定义,实现深层嵌套布局的完美对齐。display: grid; grid-template-columns: subgrid;(需在网格项目上设置)。 minmax(min, max): 定义轨道尺寸范围(如minmax(200px, 1fr))。auto-fill/auto-fit+minmax(): 创建自适应网格的核心(如repeat(auto-fit, minmax(250px, 1fr)))。- 命名网格线 (
[linename]): 为网格线命名,提高布局代码可读性(如grid-template-columns: [sidebar-start] 200px [sidebar-end main-start] 1fr [main-end];)。 grid-template-areas: 直观地定义布局模板区域。- 隐式网格控制 (
grid-auto-rows/columns,grid-auto-flow): 处理动态添加内容的布局。 - 层叠网格: 多个项目放置在同一个网格区域 (
grid-area) 实现层叠效果。 - 结合 Flexbox: Grid 负责宏观框架(页面、卡片容器),Flexbox 负责微观组件内部布局(导航项、表单控件)。
📍 3. 定位 (Positioning)
控制元素在文档流中的确切位置或相对于视口/其他元素的位置。
| 属性 | 常用值 | 说明 |
|---|---|---|
position |
static (默认) / relative / absolute / fixed / sticky |
设置元素的定位方式。 |
top |
<length> / <percentage> / auto |
定位元素的上外边距边界相对于其包含块上边界的偏移量。 |
right |
<length> / <percentage> / auto |
定位元素的右外边距边界相对于其包含块右边界的偏移量。 |
bottom |
<length> / <percentage> / auto |
定位元素的下外边距边界相对于其包含块下边界的偏移量。 |
left |
<length> / <percentage> / auto |
定位元素的左外边距边界相对于其包含块左边界的偏移量。 |
z-index |
<integer> / auto (默认) |
控制定位元素的堆叠顺序(仅对非 static 定位元素有效)。数值越大越靠前。 |
inset |
<top> <right> <bottom> <left> |
top, right, bottom, left 的简写(如 inset: 10px 20px;)。 |
定位类型详解:
static: 默认值。元素在正常文档流中,top,right,bottom,left,z-index属性无效。relative: 相对定位。元素仍在文档流中占据空间。偏移量相对于元素自身原本的位置。不影响其他元素布局。absolute: 绝对定位。元素脱离文档流,不占据空间。偏移量相对于最近的非static定位祖先元素 (包含块)。如果无,则相对于初始包含块(通常是视口)。fixed: 固定定位。元素脱离文档流,不占据空间。偏移量相对于浏览器视口 (viewport)。即使页面滚动,元素位置也不会改变。sticky: 粘性定位。元素在跨越特定阈值前为相对定位,之后为固定定位。偏移量相对于最近的滚动祖先和包含块(块级祖先)。常用于导航栏、表头。
🔍 4. 其他重要布局相关属性
| 属性 | 常用值 | 说明 |
|---|---|---|
float |
left / right / none (默认) |
使元素向左或向右浮动,文本和内联元素将环绕它。(主要用于文字环绕图片,现代布局首选 Flexbox/Grid) |
clear |
left / right / both / none (默认) |
指定元素必须移动到前面浮动元素的下方(清除浮动)。 |
display (补充) |
block / inline / inline-block / flow-root / contents / table 等 |
flow-root: 创建 BFC (块级格式化上下文),常用于清除浮动或隔离布局。contents: 元素自身不生成盒子,其子元素和伪元素如同直接是其父元素的子元素。 |
box-sizing |
content-box (默认) / border-box |
至关重要! content-box: width/height = 内容尺寸。 border-box: width/height = 内容 + 内边距 + 边框尺寸。强烈推荐全局设置 * { box-sizing: border-box; }。 |
width / min-width / max-width |
<length> / <percentage> / auto / fit-content / min-content / max-content |
控制宽度尺寸约束。fit-content 类似 float 行为。min/max-content 基于内容内在尺寸。 |
height / min-height / max-height |
<length> / <percentage> / auto / fit-content / min-content / max-content |
控制高度尺寸约束。 |
aspect-ratio |
<width> / <height> (如 16/9) |
设置元素的宽高比。非常实用,尤其在响应式图片/视频容器、保持卡片比例等场景。 |
object-fit |
fill / contain / cover / none / scale-down |
控制替换元素(如 ``, <video>)的内容如何适应其容器框。 |
overflow |
visible (默认) / hidden / scroll / auto |
定义内容溢出元素框时的处理方式。overflow-x, overflow-y 可单独控制。 |
visibility |
visible (默认) / hidden / collapse |
控制元素是否可见。hidden 隐藏元素但仍占据空间(不同于 display: none;)。 |
clip-path |
inset() / circle() / ellipse() / polygon() / url() |
裁剪元素的可视区域(仅显示路径内的部分)。用于创建非矩形元素。 |
resize |
none (默认) / both / horizontal / vertical |
允许用户调整元素大小(通常需配合 overflow: auto/hidden/scroll;)。 |
columns |
<column-count> <column-width> |
创建多列文本布局的简写属性(如 columns: 2 200px;)。 |
column-gap |
<size> |
多列布局中列之间的间距(已标准化,Grid/Flexbox 的 gap 也用它)。 |
scroll-snap-type |
none / x / y / both / mandatory / proximity |
在滚动容器上设置,定义其滚动捕捉行为。 |
scroll-snap-align |
none / start / end / center |
在滚动子项上设置,定义其捕捉点位置。 |
writing-mode |
horizontal-tb (默认) / vertical-rl / vertical-lr |
定义文本行是水平还是垂直排列,以及块流方向。影响布局逻辑。 |
direction |
ltr (默认) / rtl |
设置文本方向。影响内联元素的流向。 |
🔮 5. 现代布局技术进阶
- 容器查询 (Container Queries): (逐步支持中) 终极响应式方案!元素样式根据其自身容器的尺寸(而非视口)变化。需要先定义容器:
.container { container-type: inline-size; /* 基于宽度查询 (常用) */ /* container-type: size; */ /* 基于宽度和高度查询 */ /* container-name: my-container; */ /* 可选,命名容器 */ } @container (min-width: 500px) { .element { /* 当.container宽度 >=500px时应用此样式 */ } } @container my-container (min-height: 300px) { ... } /* 使用命名容器 */ - 逻辑属性与值 (Logical Properties & Values): 如
margin-inline-start代替margin-left,padding-block代替padding-top/bottom。使布局代码适应不同的文本方向(LTR/RTL)和书写模式(水平/垂直),提高国际化支持。 - CSS 变量 (Custom Properties) 在布局中的应用: 使用
--gap: 20px;定义间距变量,然后在gap,margin,padding等布局属性中使用var(--gap)。便于主题切换和全局调整。
🧪 6. 常见布局模式与技巧 (实战)
- 圣杯布局 (Holy Grail) / 双飞翼布局: 经典三栏布局(Header, 左右固定宽度侧栏 + 自适应主内容区, Footer),现代实现优先用 Grid (
grid-template: "header header" "sidebar main" "footer footer";或grid-template-columns: 200px 1fr 200px;)。 - 水平垂直居中:
- Flexbox:
display: flex; justify-content: center; align-items: center;(容器)。 - Grid:
display: grid; place-items: center;(容器) 或place-self: center;(项目)。 - 绝对定位 + Transform:
position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);(需非static定位祖先)。
- Flexbox:
- 等高分栏: Flexbox (
align-items: stretch;默认即是) 或 Grid (自动等高,或设置行高grid-auto-rows: 1fr;) 轻松实现。 - 粘性页脚 (Sticky Footer): Flexbox (容器
min-height: 100vh; display: flex; flex-direction: column;+ 内容区flex: 1;) 或 Grid (容器min-height: 100vh; display: grid; grid-template-rows: auto 1fr auto;+ 页脚放在最后一行)。 - 流式卡片网格 (Masonry 效果替代): Grid 结合
auto-fill/auto-fit+minmax()创建自适应列,再配合grid-auto-rows: masonry;(实验性) 或使用 Flexbox +flex-wrap: wrap;+align-content: flex-start;+ 项目设置固定高度(或aspect-ratio)实现类似效果(非真正瀑布流)。 - 全屏布局: Viewport Units (
vw,vh,vmin,vmax) + Grid/Flexbox。 - 响应式图片/视频容器: 使用
aspect-ratio+width: 100%;+object-fit: cover/contain;。
🛡 7. 布局策略与最佳实践
- 移动优先 (Mobile First): 先为小屏幕设计,再用媒体查询 (
@media) 或容器查询 (@container) 逐步增强大屏幕布局。 - 渐进增强 (Progressive Enhancement): 确保核心布局在旧浏览器中可用(如使用浮动、
inline-block等老技术作为 fallback),然后在支持的浏览器中使用 Grid/Flexbox 增强。 - 使用 CSS Reset / Normalize: 重置浏览器默认样式,确保跨浏览器一致性。
- 全局
box-sizing: border-box;: 强烈推荐,尺寸计算更直观。 - 善用开发者工具: 浏览器 DevTools 是学习和调试布局的利器(Elements面板、Grid/Flexbox 检查器、布局调试边框)。
- 性能考量:
- 避免过于复杂的选择器(影响不大但需注意)。
- 避免不必要的重排 (Reflow) 属性(如频繁改变
width/height,优先使用transform/opacity做动画)。 - 理解
contain属性(优化渲染性能)。
- 可访问性 (A11y):
- 确保视觉顺序 (
order, Grid 位置) 不影响源码顺序和键盘导航顺序。 - 使用语义化 HTML 结构。
- 为焦点状态 (
:focus-visible) 提供清晰样式。 - 考虑
prefers-reduced-motion媒体查询。
- 确保视觉顺序 (
📌 核心布局选择指南:
- 整体页面框架、复杂二维布局:Grid 是首选。
- 组件内部、一维排列、导航、表单控件:Flexbox 擅长。
- 精确定位、悬浮元素、粘性效果:Positioning。
- 组件级响应式:容器查询 (Container Queries)。
- 组合使用:Grid (宏观) + Flexbox (微观) + Positioning (点缀) 是黄金组合。
本文来自博客园,作者:liessay,转载请注明原文链接:https://www.cnblogs.com/liessay/p/19065220

浙公网安备 33010602011771号