CSS 选择器完整指南
📋 选择器分类概览
CSS 选择器是 CSS 的核心组成部分,用于选择需要样式化的 HTML 元素。本指南将选择器分为以下几类:
- 🔍 基础选择器 - 最常用的选择器类型
- 🔗 组合选择器 - 组合多个选择器进行更精确的选择
- 🎯 伪类选择器 - 基于元素状态的动态选择
- ✏️ 伪元素选择器 - 选择元素的特定部分
- 🎪 属性选择器 - 基于属性值的选择
🔍 基础选择器
1. 通用选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 通用选择器 | * | 选择所有元素 | * { margin: 0; padding: 0; } |
2. 元素选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 元素选择器 | 元素名 | 选择指定类型的元素 | div { color: red; } |
3. 类选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 类选择器 | .类名 | 选择具有指定类的元素 | .button { background: blue; } |
4. ID 选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| ID 选择器 | #id名 | 选择具有指定 ID 的元素 | #header { height: 60px; } |
🔗 组合选择器
1. 后代选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 后代选择器 | A B | 选择 A 元素内的所有 B 元素 | div p { color: blue; } |
2. 子元素选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 子元素选择器 | A > B | 选择 A 元素的直接子元素 B | ul > li { list-style: none; } |
3. 相邻兄弟选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 相邻兄弟选择器 | A + B | 选择紧接在 A 元素后的 B 元素(只修改B样式) | h1 + p { margin-top: 0; } |
4. 通用兄弟选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| 通用兄弟选择器 | A ~ B | 选择 A 元素之后的所有 B 元素 | h1 ~ p { color: gray; } |
🎯 伪类选择器
1. 链接状态伪类
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| :link | :link | 选择未访问的链接 | a:link { color: blue; } |
| :visited | :visited | 选择已访问的链接 | a:visited { color: purple; } |
| :hover | :hover | 鼠标悬停时的元素 | button:hover { background: darkblue; } |
| :active | :active | 元素被激活时(如点击) | button:active { transform: scale(0.95); } |
| :focus | :focus | 元素获得焦点时 | input:focus { border-color: blue; } |
2. 表单状态伪类
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| :checked | :checked | 选择被选中的表单元素 | input:checked { background: green; } |
| :disabled | :disabled | 选择被禁用的表单元素 | input:disabled { opacity: 0.5; } |
| :enabled | :enabled | 选择可用的表单元素 | input:enabled { border: 1px solid; } |
| :required | :required | 选择必填的表单元素 | input:required { border-color: red; } |
| :optional | :optional | 选择可选的表单元素 | input:optional { border-color: gray; } |
| :valid | :valid | 选择验证通过的表单元素 | input:valid { border-color: green; } |
| :invalid | :invalid | 选择验证失败的表单元素 | input:invalid { border-color: red; } |
| :placeholder-shown | :placeholder-shown | 占位符可见时的输入框 | input:placeholder-shown { color: gray; } |
3. 结构伪类
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| :first-child | :first-child | 父元素的第一个子元素 | li:first-child { font-weight: bold; } |
| :last-child | :last-child | 父元素的最后一个子元素 | li:last-child { border-bottom: none; } |
| :nth-child(n) | :nth-child(n) | 父元素的第 n 个子元素 | li:nth-child(2n) { background: #f5f5f5; } |
| :nth-last-child(n) | :nth-last-child(n) | 父元素的倒数第 n 个子元素 | li:nth-last-child(2) { color: red; } |
| :first-of-type | :first-of-type | 父元素中同类型的第一个元素 | p:first-of-type { text-indent: 2em; } |
| :last-of-type | :last-of-type | 父元素中同类型的最后一个元素 | p:last-of-type { margin-bottom: 0; } |
| :nth-of-type(n) | :nth-of-type(n) | 父元素中同类型的第 n 个元素 | p:nth-of-type(odd) { background: #f9f9f9; } |
| :nth-last-of-type(n) | :nth-last-of-type(n) | 父元素中同类型的倒数第 n 个元素 | p:nth-last-of-type(2) { font-style: italic; } |
| :only-child | :only-child | 父元素的唯一子元素 | div:only-child { width: 100%; } |
| :only-of-type | :only-of-type | 父元素中同类型的唯一元素 | img:only-of-type { display: block; } |
| :empty | :empty | 没有子元素的元素 | div:empty { display: none; } |
4. 其他伪类
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| :root | :root | 文档的根元素(通常是 <html>) | :root { --main-color: #333; } |
| :not(selector) | :not(selector) | 不匹配指定选择器的元素 | div:not(.special) { opacity: 0.8; } |
| :target | :target | URL 片段标识符指向的元素 | section:target { background: yellow; } |
| :focus-within | :focus-within | 元素或其子元素获得焦点时 | form:focus-within { border-color: blue; } |
| :lang(language) | :lang(language) | 指定语言的元素 | p:lang(en) { font-family: Arial; } |
✏️ 伪元素选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| ::before | ::before | 在元素内容前插入内容 | p::before { content: "→ "; } |
| ::after | ::after | 在元素内容后插入内容 | p::after { content: " ←"; } |
| ::first-letter | ::first-letter | 选择元素的第一个字母 | p::first-letter { font-size: 2em; } |
| ::first-line | ::first-line | 选择元素的第一行文本 | p::first-line { font-weight: bold; } |
| ::selection | ::selection | 用户选中的文本部分 | ::selection { background: yellow; } |
| ::placeholder | ::placeholder | 输入框的占位符文本 | input::placeholder { color: #999; } |
🎪 属性选择器
| 选择器 | 语法 | 功能描述 | 示例 |
|---|---|---|---|
| [attr] | [属性名] | 选择具有指定属性的元素 | [title] { border-bottom: 1px dotted; } |
| [attr=value] | [属性名=值] | 选择属性值等于指定值的元素 | [type="submit"] { background: blue; } |
| [attr~=value] | [属性名~=值] | 选择属性值包含指定单词的元素 | [class~="button"] { padding: 10px; } |
| [attr|=value] | [属性名|=值] | 选择属性值以指定值开头的元素 | [lang|="en"] { font-family: Arial; } |
| [attr^=value] | [属性名^=值] | 选择属性值以指定值开头的元素 | [href^="https"] { color: green; } |
| [attr$=value] | [属性名$=值] | 选择属性值以指定值结尾的元素 | [src$=".jpg"] { border: 1px solid; } |
| [attr*=value] | [属性名*=值] | 选择属性值包含指定值的元素 | [class*="icon"] { background: url(icon.png); } |
🎨 选择器优先级规则
优先级计算规则
优先级由四个部分组成,从左到右比较:
- 内联样式 -
style属性(优先级最高) - ID 选择器 -
#id - 类/属性/伪类选择器 -
.class,[attr],:hover - 元素/伪元素选择器 -
div,::before
优先级示例
css
/* 优先级:0,0,0,1 */
div { color: red; }
/* 优先级:0,0,1,0 */
.container { color: blue; }
/* 优先级:0,1,0,0 */
#header { color: green; }
/* 优先级:1,0,0,0 */
<div style="color: purple;">内联样式</div>重要规则
!important声明具有最高优先级- 相同优先级时,后定义的样式覆盖先定义的
- 继承的样式优先级最低
💡 实用技巧与最佳实践
1. 选择器性能优化
css
/* 避免使用通用选择器 */
* { margin: 0; } /* 性能差 */
/* 使用更具体的选择器 */
.container * { margin: 0; } /* 性能更好 */
/* 避免深层嵌套 */
div ul li a { color: blue; } /* 性能差 */
.nav-link { color: blue; } /* 性能好 */2. 常用选择器模式
css
/* 奇数/偶数行 */
tr:nth-child(odd) { background: #f9f9f9; }
tr:nth-child(even) { background: white; }
/* 前三个元素 */
li:nth-child(-n+3) { font-weight: bold; }
/* 最后三个元素 */
li:nth-last-child(-n+3) { color: red; }
/* 排除特定元素 */
div:not(.hidden) { display: block; }3. 响应式设计选择器
css
/* 移动端优化 */
@media (max-width: 768px) {
.desktop-only { display: none; }
.mobile-only { display: block; }
}
/* 高分辨率屏幕 */
@media (min-resolution: 2dppx) {
img { image-rendering: -webkit-optimize-contrast; }
}📚 浏览器兼容性参考
CSS3 选择器兼容性
- 现代浏览器:Chrome、Firefox、Safari、Edge(几乎全支持)
- IE 浏览器:IE9+ 支持大部分选择器,IE8 支持有限
- 移动端:iOS Safari、Android Browser 支持良好
兼容性注意事项
:focus-within:IE 不支持::placeholder:需要浏览器前缀- 属性选择器:IE7+ 基本支持
🔧 实用工具函数
1. 常用选择器组合
css
/* 清除浮动 */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* 隐藏元素但保持可访问性 */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
/* 禁用状态样式 */
button:disabled,
input:disabled {
opacity: 0.6;
cursor: not-allowed;
}最后更新:2024年
本指南适用于 CSS 初学者和中级开发者
提示:在实际开发中,建议使用 CSS 预处理器(如 Sass、Less)来简化选择器的使用和管理。