跳转到主要内容
大前端 · 前端开发指南

CSS3 完全参考指南:一份详尽的速查手册

最后核对于 2026-07-26推断

本文提供了CSS3的全面参考,涵盖了从基础概念到高级特性的所有内容,帮助您快速查找和使用各种CSS3功能。

1. CSS3简介

CSS3是层叠样式表(Cascading Style Sheets)的第三个主要版本,它引入了众多新特性,使Web开发人员能够创建更复杂、更精美的网页设计,而无需依赖图像或JavaScript。

与CSS2相比,CSS3的主要优势包括:

  • 模块化结构:CSS3被划分为多个独立模块,可以单独开发和实现
  • 更强大的样式控制能力
  • 丰富的视觉效果和动画功能
  • 更好的响应式设计支持
  • 减少对图像和脚本的依赖

2. 选择器

CSS3引入了许多新的选择器,提供了更精确的元素定位能力。

属性选择器

CSS
/* 匹配具有特定属性的元素 */
element[attribute] { }

/* 匹配属性值完全等于指定值的元素 */
element[attribute="value"] { }

/* 匹配属性值包含指定词汇的元素(以空格分隔) */
element[attribute~="value"] { }

/* 匹配属性值以指定值开头的元素 */
element[attribute^="value"] { }

/* 匹配属性值以指定值结尾的元素 */
element[attribute$="value"] { }

/* 匹配属性值中包含指定值的元素 */
element[attribute*="value"] { }

/* 匹配属性值以指定值开头或后面紧连连字符的元素 */
element[attribute|="value"] { }

伪类选择器

CSS
/* 结构伪类 */
:root { } /* 选择文档的根元素(HTML中通常是<html>) */
:empty { } /* 选择没有子元素的元素 */
:first-child { } /* 选择作为其父元素第一个子元素的元素 */
:last-child { } /* 选择作为其父元素最后一个子元素的元素 */
:nth-child(n) { } /* 选择作为其父元素第n个子元素的元素 */
:nth-last-child(n) { } /* 选择作为其父元素倒数第n个子元素的元素 */
:first-of-type { } /* 选择作为其类型第一个兄弟元素的元素 */
:last-of-type { } /* 选择作为其类型最后一个兄弟元素的元素 */
:nth-of-type(n) { } /* 选择作为其类型第n个兄弟元素的元素 */
:nth-last-of-type(n) { } /* 选择作为其类型倒数第n个兄弟元素的元素 */
:only-child { } /* 选择作为其父元素唯一子元素的元素 */
:only-of-type { } /* 选择作为其类型唯一兄弟元素的元素 */

/* UI状态伪类 */
:enabled { } /* 选择启用的表单元素 */
:disabled { } /* 选择禁用的表单元素 */
:checked { } /* 选择被选中的单选按钮或复选框 */
:default { } /* 选择默认的表单元素 */
:valid { } /* 选择输入值有效的表单元素 */
:invalid { } /* 选择输入值无效的表单元素 */
:in-range { } /* 选择值在指定范围内的表单元素 */
:out-of-range { } /* 选择值超出指定范围的表单元素 */
:required { } /* 选择必填的表单元素 */
:optional { } /* 选择可选的表单元素 */
:read-only { } /* 选择只读的表单元素 */
:read-write { } /* 选择可读写的表单元素 */

/* 其他伪类 */
:not(selector) { } /* 选择不匹配指定选择器的元素 */
:target { } /* 选择当前活动的目标元素(URL片段标识的元素) */
:lang(language) { } /* 选择基于lang属性的元素 */

伪元素选择器

CSS
::first-line { } /* 选择元素的第一行 */
::first-letter { } /* 选择元素的第一个字母 */
::before { } /* 在元素内容前插入生成的内容 */
::after { } /* 在元素内容后插入生成的内容 */
::selection { } /* 选择用户选中的文本部分 */
::placeholder { } /* 选择表单元素的占位符文本 */

3. 盒模型与布局

box-sizing 属性

CSS
/* 默认值,宽度和高度只包含内容,不包含内边距和边框 */
box-sizing: content-box;

/* 宽度和高度包含内容、内边距和边框 */
box-sizing: border-box;

显示模式

CSS
/* 新的显示模式值 */
display: flex; /* 弹性盒子布局 */
display: inline-flex; /* 内联弹性盒子 */
display: grid; /* 网格布局 */
display: inline-grid; /* 内联网格 */
display: flow-root; /* 创建新的块级格式化上下文,类似overflow:hidden但无副作用 */

溢出处理

CSS
/* 新的溢出值 */
overflow: auto; /* 根据需要显示滚动条 */
overflow: scroll; /* 总是显示滚动条 */
overflow: hidden; /* 隐藏溢出内容 */
overflow: visible; /* 显示溢出内容 */
overflow: clip; /* 类似hidden但禁止编程滚动 */

/* 分别指定水平和垂直方向的溢出 */
overflow-x: auto;
overflow-y: hidden;

/* 文本溢出处理 */
text-overflow: clip; /* 简单裁剪文本 */
text-overflow: ellipsis; /* 显示省略号表示被裁剪的文本 */
text-overflow: "…"; /* 使用自定义字符串表示被裁剪的文本 */

/* 多行文本溢出显示省略号 */
display: -webkit-box;
-webkit-line-clamp: 3; /* 限制显示的行数 */
-webkit-box-orient: vertical;
overflow: hidden;

4. 背景与边框

多背景图像

CSS
background-image: url(image1.jpg), url(image2.png);
background-position: left top, right bottom;
background-repeat: no-repeat, repeat;
background-size: 50%, contain;

background-size 属性

CSS
background-size: auto; /* 默认值,图像原始尺寸 */
background-size: cover; /* 覆盖整个元素区域,可能裁剪图像 */
background-size: contain; /* 完整显示图像,可能留白 */
background-size: 50%; /* 指定宽度,高度自动 */
background-size: 100px 200px; /* 指定宽度和高度 */

background-clip 属性

CSS
background-clip: border-box; /* 背景延伸到边框外边缘 */
background-clip: padding-box; /* 背景延伸到内边距外边缘 */
background-clip: content-box; /* 背景仅延伸到内容外边缘 */
background-clip: text; /* 背景被裁剪为文本前景(需要浏览器前缀) */

background-origin 属性

CSS
background-origin: border-box; /* 背景相对于边框盒定位 */
background-origin: padding-box; /* 背景相对于内边距盒定位(默认) */
background-origin: content-box; /* 背景相对于内容盒定位 */

边框圆角

CSS
/* 统一设置四个角 */
border-radius: 10px;

/* 分别设置四个角 */
border-radius: 10px 20px 30px 40px; /* 左上 右上 右下 左下 */

/* 分别设置水平和垂直半径 */
border-radius: 10px 20px 30px 40px / 5px 15px 25px 35px;

/* 单独设置每个角 */
border-top-left-radius: 10px 5px;
border-top-right-radius: 20px 15px;
border-bottom-right-radius: 30px 25px;
border-bottom-left-radius: 40px 35px;

边框图像

CSS
border-image-source: url(border.png);
border-image-slice: 30; /* 所有边 */
border-image-slice: 30 30; /* 垂直 水平 */
border-image-slice: 30 30 30; /* 上 水平 下 */
border-image-slice: 30 30 30 30; /* 上 右 下 左 */
border-image-slice: 30 fill; /* 保留中间部分 */

border-image-width: 10px; /* 所有边 */
border-image-width: 10px 20px; /* 垂直 水平 */
border-image-width: 10px 20px 10px; /* 上 水平 下 */
border-image-width: 10px 20px 10px 20px; /* 上 右 下 左 */
border-image-width: auto; /* 使用border-width的值 */

border-image-outset: 5px; /* 所有边 */
border-image-outset: 5px 10px; /* 垂直 水平 */
border-image-outset: 5px 10px 5px; /* 上 水平 下 */
border-image-outset: 5px 10px 5px 10px; /* 上 右 下 左 */

border-image-repeat: stretch; /* 拉伸图像填充区域 */
border-image-repeat: repeat; /* 平铺图像 */
border-image-repeat: round; /* 平铺图像但调整大小以避免裁剪 */
border-image-repeat: space; /* 平铺图像但添加空白以避免裁剪 */

/* 简写形式 */
border-image: url(border.png) 30 / 10px / 5px round;

盒子阴影

CSS
/* 基本语法 */
box-shadow: none; /* 无阴影 */
box-shadow: h-offset v-offset blur spread color inset;

/* 示例 */
box-shadow: 5px 5px 10px rgba(0,0,0,0.5); /* 右下阴影 */
box-shadow: -5px -5px 10px rgba(0,0,0,0.5); /* 左上阴影 */
box-shadow: 0 0 10px 5px rgba(0,0,0,0.5); /* 四周扩散阴影 */
box-shadow: inset 0 0 10px rgba(0,0,0,0.5); /* 内阴影 */

/* 多重阴影 */
box-shadow: 
  0 2px 4px rgba(0,0,0,0.1),
  0 4px 8px rgba(0,0,0,0.1),
  0 8px 16px rgba(0,0,0,0.1);

5. 文本效果

文本阴影

CSS
/* 基本语法 */
text-shadow: none; /* 无阴影 */
text-shadow: h-offset v-offset blur color;

/* 示例 */
text-shadow: 2px 2px 4px rgba(0,0,0,0.5); /* 右下模糊阴影 */
text-shadow: -2px -2px 4px rgba(0,0,0,0.5); /* 左上模糊阴影 */
text-shadow: 0 0 10px rgba(255,255,255,0.8); /* 发光效果 */

/* 多重文本阴影 */
text-shadow: 
  1px 1px 1px rgba(0,0,0,0.3),
  2px 2px 2px rgba(0,0,0,0.2),
  3px 3px 3px rgba(0,0,0,0.1);

文本溢出

CSS
/* 单行文本溢出显示省略号 */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

/* 多行文本溢出显示省略号(WebKit浏览器) */
display: -webkit-box;
-webkit-line-clamp: 3; /* 限制显示的行数 */
-webkit-box-orient: vertical;
overflow: hidden;

文字换行

CSS
/* 控制长单词和URL的换行行为 */
word-wrap: normal; /* 只在正常断点处换行 */
word-wrap: break-word; /* 在长单词或URL内部换行 */

word-break: normal; /* 使用默认断行规则 */
word-break: break-all; /* 允许在任意字符间断行 */
word-break: keep-all; /* 中文/日文/韩文文本中不允许换行 */

overflow-wrap: normal; /* 等同于word-wrap: normal */
overflow-wrap: break-word; /* 等同于word-wrap: break-word */
overflow-wrap: anywhere; /* 类似break-word,但计算最小内容大小时考虑断点 */

@font-face 规则

CSS
@font-face {
  font-family: 'MyWebFont';
  src: url('webfont.woff2') format('woff2'),
       url('webfont.woff') format('woff'),
       url('webfont.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap; /* 控制字体加载期间的显示行为 */
}

body {
  font-family: 'MyWebFont', sans-serif;
}

6. 变换、过渡与动画

变换 (Transform)

CSS
/* 2D变换函数 */
transform: none; /* 无变换 */
transform: translate(50px, 100px); /* 移动元素 */
transform: translateX(50px); /* 水平移动 */
transform: translateY(100px); /* 垂直移动 */
transform: scale(2, 0.5); /* 缩放元素 */
transform: scaleX(2); /* 水平缩放 */
transform: scaleY(0.5); /* 垂直缩放 */
transform: rotate(30deg); /* 旋转元素 */
transform: skew(30deg, 20deg); /* 倾斜元素 */
transform: skewX(30deg); /* 水平倾斜 */
transform: skewY(20deg); /* 垂直倾斜 */
transform: matrix(1, 0.2, 0.3, 1, 0, 0); /* 使用矩阵变换 */

/* 3D变换函数 */
transform: translate3d(50px, 100px, 150px); /* 3D移动 */
transform: translateZ(150px); /* Z轴移动 */
transform: scale3d(2, 0.5, 1.5); /* 3D缩放 */
transform: scaleZ(1.5); /* Z轴缩放 */
transform: rotate3d(1, 1, 1, 45deg); /* 3D旋转 */
transform: rotateX(45deg); /* X轴旋转 */
transform: rotateY(45deg); /* Y轴旋转 */
transform: rotateZ(45deg); /* Z轴旋转 */
transform: perspective(500px); /* 设置透视距离 */
transform: matrix3d( ... ); /* 使用3D矩阵变换 */

/* 变换原点 */
transform-origin: center; /* 默认值,中心点 */
transform-origin: top left; /* 左上角 */
transform-origin: 50px 100px; /* 自定义位置 */
transform-origin: 50% 100%; /* 百分比值 */
transform-origin: bottom right 50px; /* 3D变换原点 */

/* 变换样式 */
transform-style: flat; /* 子元素位于2D平面 */
transform-style: preserve-3d; /* 子元素位于3D空间 */

/* 透视 */
perspective: none; /* 无透视效果 */
perspective: 500px; /* 设置透视距离 */

/* 透视原点 */
perspective-origin: center; /* 默认值,中心点 */
perspective-origin: top left; /* 左上角 */
perspective-origin: 50% 50%; /* 自定义位置 */

/* 背面可见性 */
backface-visibility: visible; /* 背面可见 */
backface-visibility: hidden; /* 背面隐藏 */

过渡 (Transition)

CSS
/* 过渡属性 */
transition-property: none; /* 无属性过渡 */
transition-property: all; /* 所有属性过渡 */
transition-property: width, height; /* 指定属性过渡 */

/* 过渡持续时间 */
transition-duration: 0s; /* 无过渡时间 */
transition-duration: 1s; /* 1秒过渡 */
transition-duration: 500ms; /* 500毫秒过渡 */

/* 过渡计时函数 */
transition-timing-function: ease; /* 默认值,慢快慢 */
transition-timing-function: linear; /* 匀速 */
transition-timing-function: ease-in; /* 慢开始 */
transition-timing-function: ease-out; /* 慢结束 */
transition-timing-function: ease-in-out; /* 慢开始和结束 */
transition-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1); /* 贝塞尔曲线 */
transition-timing-function: steps(4, end); /* 步进函数 */

/* 过渡延迟 */
transition-delay: 0s; /* 无延迟 */
transition-delay: 1s; /* 1秒延迟 */

/* 简写形式 */
transition: all 1s ease 0.5s; /* property duration timing-function delay */
transition: width 2s, height 1s;

动画 (Animation)

CSS
/* 关键帧定义 */
@keyframes slidein {
  from {
    transform: translateX(0%);
  }
  
  to {
    transform: translateX(100%);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  
  50% {
    transform: scale(1.1);
    opacity: 0.7;
  }
  
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 动画属性 */
animation-name: none; /* 无动画 */
animation-name: slidein; /* 使用定义的关键帧 */

/* 动画持续时间 */
animation-duration: 0s; /* 无动画时间 */
animation-duration: 1s; /* 1秒动画 */

/* 动画计时函数 */
animation-timing-function: ease; /* 默认值,慢快慢 */
animation-timing-function: linear; /* 匀速 */
animation-timing-function: ease-in; /* 慢开始 */
animation-timing-function: ease-out; /* 慢结束 */
animation-timing-function: ease-in-out; /* 慢开始和结束 */
animation-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1); /* 贝塞尔曲线 */
animation-timing-function: steps(4, end); /* 步进函数 */

/* 动画延迟 */
animation-delay: 0s; /* 无延迟 */
animation-delay: 1s; /* 1秒延迟 */

/* 动画迭代次数 */
animation-iteration-count: 1; /* 播放一次 */
animation-iteration-count: infinite; /* 无限循环 */
animation-iteration-count: 2.5; /* 播放2.5次 */

/* 动画方向 */
animation-direction: normal; /* 正常播放 */
animation-direction: reverse; /* 反向播放 */
animation-direction: alternate; /* 交替播放(正-反-正) */
animation-direction: alternate-reverse; /* 交替反向播放(反-正-反) */

/* 动画填充模式 */
animation-fill-mode: none; /* 不应用任何样式 */
animation-fill-mode: forwards; /* 保持最后一帧的样式 */
animation-fill-mode: backwards; /* 应用第一帧的样式(在延迟期间) */
animation-fill-mode: both; /* 同时应用forwards和backwards */

/* 动画播放状态 */
animation-play-state: running; /* 动画正在播放 */
animation-play-state: paused; /* 动画暂停 */

/* 简写形式 */
animation: slidein 3s ease-in 1s infinite reverse both running;

7. 多列布局

CSS
/* 列数 */
column-count: auto; /* 自动列数 */
column-count: 3; /* 固定列数 */

/* 列宽 */
column-width: auto; /* 自动列宽 */
column-width: 100px; /* 最小列宽 */

/* 简写形式 */
columns: 3; /* 只设置列数 */
columns: 100px; /* 只设置列宽 */
columns: 3 100px; /* 同时设置列数和列宽 */

/* 列间距 */
column-gap: normal; /* 默认间距(通常1em) */
column-gap: 20px; /* 自定义间距 */

/* 列间分隔线 */
column-rule: none; /* 无分隔线 */
column-rule: 1px solid #ccc; /* 简写形式:width style color */

/* 分隔线宽度 */
column-rule-width: 1px;

/* 分隔线样式 */
column-rule-style: none;
column-rule-style: hidden;
column-rule-style: dotted;
column-rule-style: dashed;
column-rule-style: solid;
column-rule-style: double;
column-rule-style: groove;
column-rule-style: ridge;
column-rule-style: inset;
column-rule-style: outset;

/* 分隔线颜色 */
column-rule-color: #ccc;

/* 跨列 */
column-span: none; /* 不跨列 */
column-span: all; /* 跨所有列 */

/* 列填充 */
column-fill: auto; /* 按顺序填充列 */
column-fill: balance; /* 平衡列内容高度 */

8. 弹性盒子布局

容器属性

CSS
/* 定义弹性容器 */
display: flex; /* 块级弹性容器 */
display: inline-flex; /* 内联弹性容器 */

/* 主轴方向 */
flex-direction: row; /* 默认值,水平从左到右 */
flex-direction: row-reverse; /* 水平从右到左 */
flex-direction: column; /* 垂直从上到下 */
flex-direction: column-reverse; /* 垂直从下到上 */

/* 换行行为 */
flex-wrap: nowrap; /* 默认值,不换行 */
flex-wrap: wrap; /* 换行,第一行在上方 */
flex-wrap: wrap-reverse; /* 换行,第一行在下方 */

/* 简写形式:flex-direction和flex-wrap */
flex-flow: row nowrap; 

/* 主轴对齐 */
justify-content: flex-start; /* 起始位置对齐 */
justify-content: flex-end; /* 结束位置对齐 */
justify-content: center; /* 居中对齐 */
justify-content: space-between; /* 两端对齐,项目间间隔相等 */
justify-content: space-around; /* 每个项目两侧间隔相等 */
justify-content: space-evenly; /* 所有间隔相等 */

/* 交叉轴对齐 */
align-items: stretch; /* 默认值,拉伸填满容器 */
align-items: flex-start; /* 起始位置对齐 */
align-items: flex-end; /* 结束位置对齐 */
align-items: center; /* 居中对齐 */
align-items: baseline; /* 基线对齐 */

/* 多行内容对齐 */
align-content: stretch; /* 默认值,拉伸填满容器 */
align-content: flex-start; /* 起始位置对齐 */
align-content: flex-end; /* 结束位置对齐 */
align-content: center; /* 居中对齐 */
align-content: space-between; /* 两端对齐,行间间隔相等 */
align-content: space-around; /* 每行两侧间隔相等 */
align-content: space-evenly; /* 所有间隔相等 */

项目属性

CSS
/* 项目顺序 */
order: 0; /* 默认值 */
order: 1; /* 数值越大,排列越靠后 */

/* 项目放大比例 */
flex-grow: 0; /* 默认值,不放大 */
flex-grow: 1; /* 有剩余空间时,项目放大 */

/* 项目缩小比例 */
flex-shrink: 1; /* 默认值,空间不足时,项目缩小 */
flex-shrink: 0; /* 空间不足时,项目不缩小 */

/* 项目初始大小 */
flex-basis: auto; /* 默认值,自动大小 */
flex-basis: 100px; /* 固定大小 */
flex-basis: 20%; /* 相对大小 */

/* 简写形式:flex-grow, flex-shrink, flex-basis */
flex: 0 1 auto; /* 默认值 */
flex: 1; /* 等价于flex: 1 1 0% */
flex: 1 1 100px; /* 自定义值 */

/* 单个项目对齐方式 */
align-self: auto; /* 默认值,继承容器的align-items */
align-self: stretch; /* 拉伸填满容器 */
align-self: flex-start; /* 起始位置对齐 */
align-self: flex-end; /* 结束位置对齐 */
align-self: center; /* 居中对齐 */
align-self: baseline; /* 基线对齐 */

9. 网格布局

容器属性

CSS
/* 定义网格容器 */
display: grid; /* 块级网格容器 */
display: inline-grid; /* 内联网格容器 */

/* 定义网格轨道 */
grid-template-columns: none; /* 无显式网格 */
grid-template-columns: 100px 100px 100px; /* 固定宽度列 */
grid-template-columns: 1fr 1fr 1fr; /* 分数单位列 */
grid-template-columns: repeat(3, 1fr); /* 重复模式 */
grid-template-columns: 100px 1fr max-content; /* 混合单位 */
grid-template-columns: [col1] 100px [col2] 1fr [col3] 1fr; /* 命名网格线 */

grid-template-rows: none;
grid-template-rows: 50px 100px 50px;
grid-template-rows: 1fr 2fr 1fr;
grid-template-rows: repeat(3, auto);
grid-template-rows: [row1] 50px [row2] auto [row3] auto;

/* 定义网格区域 */
grid-template-areas: none;
grid-template-areas: 
  "header header header"
  "sidebar content content"
  "footer footer footer";

/* 简写形式:grid-template-rows, grid-template-columns, grid-template-areas */
grid-template: 
  [row1] "header header header" 50px [row2]
  [row2] "sidebar content content" auto [row3]
  [row3] "footer footer footer" 50px [row4]
  / 1fr 2fr 1fr; /* 列轨道定义 */

/* 隐式网格轨道大小 */
grid-auto-columns: auto; /* 默认值,自动大小 */
grid-auto-columns: 100px; /* 固定大小 */
grid-auto-columns: minmax(100px, auto); /* 最小最大范围 */

grid-auto-rows: auto;
grid-auto-rows: 50px;
grid-auto-rows: minmax(50px, auto);

/* 自动放置算法 */
grid-auto-flow: row; /* 默认值,按行填充 */
grid-auto-flow: column; /* 按列填充 */
grid-auto-flow: dense; /* 密集填充,尝试填充前面留下的空白 */
grid-auto-flow: row dense; /* 按行密集填充 */
grid-auto-flow: column dense; /* 按列密集填充 */

/* 网格间距 */
grid-column-gap: 0; /* 默认值,无间距 */
grid-column-gap: 10px; /* 列间距 */

grid-row-gap: 0;
grid-row-gap: 10px; /* 行间距 */

/* 简写形式:grid-row-gap和grid-column-gap */
gap: 10px; /* 行间距和列间距相同 */
gap: 10px 20px; /* 行间距 列间距 */

/* 对齐内容(网格容器内) */
justify-content: start; /* 起始位置对齐 */
justify-content: end; /* 结束位置对齐 */
justify-content: center; /* 居中对齐 */
justify-content: stretch; /* 拉伸网格轨道填满容器 */
justify-content: space-around; /* 每个轨道两侧间隔相等 */
justify-content: space-between; /* 两端对齐,轨道间间隔相等 */
justify-content: space-evenly; /* 所有间隔相等 */

align-content: start;
align-content: end;
align-content: center;
align-content: stretch;
align-content: space-around;
align-content: space-between;
align-content: space-evenly;

/* 对齐项目(网格单元格内) */
justify-items: stretch; /* 默认值,拉伸填满单元格 */
justify-items: start; /* 起始位置对齐 */
justify-items: end; /* 结束位置对齐 */
justify-items: center; /* 居中对齐 */

align-items: stretch;
align-items: start;
align-items: end;
align-items: center;

项目属性

CSS
/* 网格项目位置 */
grid-column-start: auto; /* 默认值,自动放置 */
grid-column-start: 1; /* 数字网格线 */
grid-column-start: col1; /* 命名网格线 */
grid-column-start: span 2; /* 跨越2个轨道 */

grid-column-end: auto;
grid-column-end: 3;
grid-column-end: col3;
grid-column-end: span 2;

grid-row-start: auto;
grid-row-start: 1;
grid-row-start: row1;
grid-row-start: span 2;

grid-row-end: auto;
grid-row-end: 3;
grid-row-end: row3;
grid-row-end: span 2;

/* 简写形式:grid-column-start和grid-column-end */
grid-column: auto; /* 自动放置 */
grid-column: 1 / 3; /* 从第1条线到第3条线 */
grid-column: 1 / span 2; /* 从第1条线开始,跨越2个轨道 */
grid-column: col1 / col3; /* 从命名线col1到col3 */

/* 简写形式:grid-row-start和grid-row-end */
grid-row: auto;
grid-row: 1 / 3;
grid-row: 1 / span 2;
grid-row: row1 / row3;

/* 简写形式:grid-row和grid-column */
grid-area: auto;
grid-area: 1 / 1 / 3 / 3; /* row-start / column-start / row-end / column-end */
grid-area: header; /* 网格区域名称 */

/* 单个项目对齐 */
justify-self: auto; /* 默认值,继承容器的justify-items */
justify-self: stretch; /* 拉伸填满单元格 */
justify-self: start; /* 起始位置对齐 */
justify-self: end; /* 结束位置对齐 */
justify-self: center; /* 居中对齐 */

align-self: auto;
align-self: stretch;
align-self: start;
align-self: end;
align-self: center;

10. 响应式设计

响应式单位

CSS
/* 相对单位 */
width: 100%; /* 相对于父元素 */
width: 50vw; /* 相对于视口宽度 */
height: 50vh; /* 相对于视口高度 */
font-size: 2em; /* 相对于父元素字体大小 */
font-size: 2rem; /* 相对于根元素字体大小 */

/* 最小值/最大值限制 */
width: min(100%, 500px); /* 取较小值 */
width: max(50%, 300px); /* 取较大值 */
width: clamp(300px, 50%, 500px); /* 介于300px和500px之间,优先50% */

/* 视口单位 */
width: 100vw; /* 视口宽度的100% */
height: 100vh; /* 视口高度的100% */
font-size: 4vmin; /* 视口较小尺寸的4% */
font-size: 4vmax; /* 视口较大尺寸的4% */

响应式图像

CSS
/* 自适应图像 */
img {
  max-width: 100%;
  height: auto;
}

/* 根据分辨率提供不同图像 */
.background {
  background-image: url(image-low-res.jpg);
}

@media (min-resolution: 2dppx) {
  .background {
    background-image: url(image-high-res.jpg);
  }
}

11. 滤镜效果

CSS
/* 基本滤镜 */
filter: none; /* 无滤镜效果 */

/* 模糊 */
filter: blur(5px); /* 高斯模糊 */

/* 亮度 */
filter: brightness(0.5); /* 变暗50% */
filter: brightness(1.5); /* 变亮50% */

/* 对比度 */
filter: contrast(0.5); /* 降低对比度50% */
filter: contrast(1.5); /* 增加对比度50% */

/* 阴影 */
filter: drop-shadow(5px 5px 5px rgba(0,0,0,0.5)); /* 投影效果 */

/* 灰度 */
filter: grayscale(100%); /* 完全灰度 */
filter: grayscale(50%); /* 部分灰度 */

/* 色相旋转 */
filter: hue-rotate(90deg); /* 旋转90度 */

/* 反相 */
filter: invert(100%); /* 完全反相 */

/* 透明度 */
filter: opacity(50%); /* 50%透明度 */

/* 饱和度 */
filter: saturate(50%); /* 降低饱和度50% */
filter: saturate(150%); /* 增加饱和度50% */

/* 深褐色 */
filter: sepia(100%); /* 完全深褐色效果 */

/* 组合滤镜 */
filter: contrast(1.5) brightness(1.2) saturate(1.2);

/* SVG滤镜 */
filter: url(filters.svg#filter-id);

背景滤镜

CSS
/* 元素后方模糊(需要浏览器前缀) */
backdrop-filter: none;
backdrop-filter: blur(10px);
backdrop-filter: brightness(0.5);
backdrop-filter: contrast(0.5);
backdrop-filter: grayscale(0.5);
backdrop-filter: hue-rotate(90deg);
backdrop-filter: invert(0.5);
backdrop-filter: opacity(0.5);
backdrop-filter: saturate(0.5);
backdrop-filter: sepia(0.5);
backdrop-filter: url(filters.svg#filter-id);
backdrop-filter: blur(5px) brightness(0.8);

12. 媒体查询

CSS
/* 基本媒体查询 */
@media (max-width: 600px) {
  /* 视口宽度小于等于600px时应用的样式 */
}

@media (min-width: 600px) {
  /* 视口宽度大于等于600px时应用的样式 */
}

@media (min-width: 600px) and (max-width: 1200px) {
  /* 视口宽度在600px到1200px之间时应用的样式 */
}

/* 媒体类型 */
@media all { } /* 所有设备 */
@media print { } /* 打印模式 */
@media screen { } /* 屏幕设备 */
@media speech { } /* 语音合成器 */

/* 媒体特性 */
@media (orientation: portrait) { } /* 竖屏模式 */
@media (orientation: landscape) { } /* 横屏模式 */

@media (min-resolution: 2dppx) { } /* 高分辨率设备 */

@media (prefers-color-scheme: light) { } /* 浅色主题偏好 */
@media (prefers-color-scheme: dark) { } /* 深色主题偏好 */

@media (prefers-reduced-motion: reduce) { } /* 减少动画偏好 */

@media (hover: hover) { } /* 支持悬停的设备 */
@media (hover: none) { } /* 不支持悬停的设备 */

@media (pointer: fine) { } /* 精确指针设备(如鼠标) */
@media (pointer: coarse) { } /* 粗略指针设备(如触摸屏) */
@media (pointer: none) { } /* 无指针设备 */

/* 复杂媒体查询 */
@media screen and (min-width: 600px) and (max-width: 1200px) and (orientation: landscape) {
  /* 屏幕设备、横屏模式、宽度在600px到1200px之间 */
}

@media (max-width: 600px), (orientation: portrait) {
  /* 宽度小于等于600px或竖屏模式 */
}

/* 特性查询 */
@supports (display: grid) {
  /* 支持网格布局时应用的样式 */
}

@supports not (display: grid) {
  /* 不支持网格布局时应用的样式 */
}

@supports (display: grid) and (display: flex) {
  /* 同时支持网格和弹性盒子布局时应用的样式 */
}

@supports (display: grid) or (display: flex) {
  /* 支持网格或弹性盒子布局时应用的样式 */
}

13. CSS变量(自定义属性)

CSS
/* 定义变量 */
:root {
  --main-color: #3498db;
  --accent-color: #e74c3c;
  --font-size: 16px;
  --spacing: 1rem;
  --border-radius: 4px;
  --transition: all 0.3s ease;
}

/* 使用变量 */
.element {
  color: var(--main-color);
  font-size: var(--font-size);
  margin: var(--spacing);
  border-radius: var(--border-radius);
  transition: var(--transition);
}

/* 带默认值的变量 */
.element {
  color: var(--main-color, #3498db); /* 如果--main-color未定义,使用#3498db */
  background-color: var(--bg-color, #fff); /* 默认白色背景 */
}

/* 在JavaScript中访问和修改CSS变量 */
// 获取变量值
const color = getComputedStyle(document.documentElement)
  .getPropertyValue('--main-color');

// 设置变量值
document.documentElement.style
  .setProperty('--main-color', '#e74c3c');

/* 在媒体查询中修改变量 */
:root {
  --font-size: 16px;
  --spacing: 1rem;
}

@media (max-width: 600px) {
  :root {
    --font-size: 14px;
    --spacing: 0.5rem;
  }
}

/* 计算中使用变量 */
.element {
  --base-size: 10px;
  --multiplier: 2;
  font-size: calc(var(--base-size) * var(--multiplier));
  padding: calc(var(--spacing) / 2);
}

14. 最佳实践与性能优化

编写高效的CSS

CSS
/* 避免过度特定的选择器 */
/* 不好 */
body div#container ul.nav li a { }

/* 好 */
.nav a { }

/* 使用类选择器而不是标签选择器 */
/* 不好 */
div { }
ul { }

/* 好 */
.container { }
.menu { }

/* 避免使用通配选择器进行大量元素的匹配 */
/* 不好 */
* { box-sizing: border-box; }

/* 好 */
html { box-sizing: border-box; }
*, *:before, *:after { box-sizing: inherit; }

/* 减少重排和重绘 */
.element {
  /* 使用transform和opacity实现动画,它们不会触发重排 */
  transform: translateX(100px);
  opacity: 0.5;
  
  /* 避免在动画中修改这些属性,它们会触发重排 */
  /* width, height, padding, margin, top, left, etc. */
}

/* 使用will-change提示浏览器优化 */
.element {
  will-change: transform, opacity;
}

组织与维护

CSS
/* 使用一致的命名约定(如BEM) */
.block { }
.block__element { }
.block--modifier { }

/* 分组相关属性 */
.selector {
  /* 定位 */
  position: relative;
  top: 0;
  left: 0;
  z-index: 10;
  
  /* 盒模型 */
  display: block;
  width: 100%;
  height: 100%;
  padding: 1rem;
  margin: 0;
  border: 1px solid #ccc;
  
  /* 排版 */
  font-family: sans-serif;
  font-size: 1rem;
  line-height: 1.5;
  text-align: center;
  
  /* 视觉 */
  color: #333;
  background-color: #fff;
  border-radius: 4px;
  
  /* 其他 */
  cursor: pointer;
  transition: all 0.3s ease;
}

/* 使用注释分隔代码段 */
/* ==========================================================================
   主要头部样式
   ========================================================================== */

/* 头部容器 */
.header { }

/* 导航菜单 */
.nav { }

/* ==========================================================================
   主要内容样式
   ========================================================================== */

/* 文章容器 */
.article { }

/* 侧边栏 */
.sidebar { }

性能考虑

CSS
/* 减少CSS文件大小 */
/* 使用简写属性 */
margin: 10px 5px 10px 5px; /* 不好 */
margin: 10px 5px; /* 好 */

/* 删除不必要的单位和零值 */
padding: 0px; /* 不好 */
padding: 0; /* 好 */

margin: 10.0px; /* 不好 */
margin: 10px; /* 好 */

/* 压缩CSS */
/* 使用工具如CSSNano、UglifyCSS等 */

/* 关键CSS(Above-the-fold内容样式) */
/* 内联关键CSS,异步加载其余样式 */
<style>
/* 关键CSS代码 */
</style>
<link rel="preload" href="styles.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>

结论

CSS3提供了强大的样式功能,使开发人员能够创建丰富、动态和响应式的网页设计。这份速查表涵盖了CSS3的主要特性,从基本选择器到高级布局技术,可以作为日常开发的参考指南。

要充分利用CSS3,建议:

  1. 理解每个属性的浏览器支持情况,使用适当的回退方案
  2. 使用前缀确保跨浏览器兼容性(如-webkit-, -moz-, -ms-, -o-)
  3. 定期检查Can I Use (caniuse.com) 了解最新的浏览器支持情况
  4. 使用CSS预处理器(如Sass、Less)提高开发效率
  5. 采用模块化和可维护的代码组织方式

通过掌握这些CSS3特性,您将能够创建更加精美、功能丰富且性能优异的网页体验。