当前位置: 首页 > news >正文

CSS 结构伪类选择器

目录

1. 基础结构伪类

1.1. :root

:empty

:not()

2. 子元素位置伪类

:first-child 和 :last-child

:nth-child()

:nth-last-child()

:only-child

3. 同类型元素位置伪类

:first-of-type 和 :last-of-type

:nth-of-type()

:nth-last-of-type()

:only-of-type

4. 实际应用示例

表格样式优化

列表样式处理

卡片布局

5. 高级技巧

组合使用

复杂选择器

6. 浏览器兼容性

7. 性能考虑

8. 最佳实践


结构伪类选择器是 CSS 中一类特殊的选择器,它们基于元素在文档树中的位置和结构关系来选择元素,而不需要为元素添加额外的类名或 ID。

1. 基础结构伪类

1.1. :root

选择文档的根元素(HTML 文档中就是 <html> 元素)

:root {--main-color: #333;--accent-color: #007bff;
}/* 等价于 html { ... },但优先级更高 */

:empty

选择没有任何子元素(包括文本节点)的元素

/* 隐藏空的div */
div:empty {display: none;
}/* 为p元素添加提示文本(如果为空) */
p:empty::before {content: "暂无内容";color: #999;
}

:not()

否定选择器,选择不匹配指定选择器的元素

/* 选择除了第一个div之外的所有div */
div:not(:first-child) {margin-top: 20px;
}/* 选择除了类名为special之外的所有p元素 */
p:not(.special) {color: #333;
}

2. 子元素位置伪类

:first-child 和 :last-child

选择父元素的第一个和最后一个子元素

/* 选择父元素的第一个子元素 */
:first-child {margin-top: 0;
}/* 选择父元素的最后一个子元素 */
:last-child {margin-bottom: 0;
}/* 选择父元素的第一个p子元素 */
p:first-child {font-weight: bold;
}

:nth-child()

选择父元素的第 n 个子元素

/* 基本用法 */
:nth-child(3) {/* 选择第3个子元素 */
}
:nth-child(2n) {/* 选择偶数位置的元素 */
}
:nth-child(2n + 1) {/* 选择奇数位置的元素 */
}
:nth-child(3n) {/* 选择第3、6、9...个元素 */
}/* 关键字 */
:nth-child(even) {/* 等价于 2n */
}
:nth-child(odd) {/* 等价于 2n+1 */
}
:nth-child(n) {/* 选择所有子元素 */
}/* 实际应用示例 */
/* 表格隔行变色 */
tr:nth-child(2n) {background-color: #f9f9f9;
}/* 选择前3个li元素 */
li:nth-child(-n + 3) {font-weight: bold;
}

:nth-last-child()

从末尾开始计数的 nth-child

/* 选择倒数第2个子元素 */
:nth-last-child(2) {
}/* 选择最后3个元素 */
:nth-last-child(-n + 3) {
}

:only-child

选择父元素中唯一的子元素

/* 只有一个子元素时应用样式 */
div:only-child {width: 100%;text-align: center;
}

3. 同类型元素位置伪类

:first-of-type 和 :last-of-type

选择父元素中同类型的第一个和最后一个元素

/* 选择第一个h2元素 */
h2:first-of-type {margin-top: 0;
}/* 选择最后一个p元素 */
p:last-of-type {margin-bottom: 0;
}

:nth-of-type()

选择父元素中同类型的第 n 个元素

/* 选择第3个h3元素 */
h3:nth-of-type(3) {color: red;
}/* 选择每个章节的第一个段落 */
p:nth-of-type(1) {font-size: 1.2em;
}

:nth-last-of-type()

从末尾开始计数的同类型元素选择器

/* 选择倒数第二个p元素 */
p:nth-last-of-type(2) {font-style: italic;
}

:only-of-type

选择父元素中唯一的该类型元素

/* 当只有一个h1元素时应用样式 */
h1:only-of-type {font-size: 2em;text-align: center;
}

4. 实际应用示例

表格样式优化

<table><thead><tr><th>姓名</th><th>年龄</th><th>职位</th></tr></thead><tbody><tr><td>张三</td><td>25</td><td>工程师</td></tr><!-- 更多行... --></tbody>
</table>
/* 表头样式 */
th:first-child {border-left: none;
}th:last-child {border-right: none;
}/* 表格行隔行变色 */
tr:nth-child(2n) {background-color: #f8f9fa;
}/* 表格最后一行 */
tr:last-child td {border-bottom: 2px solid #333;
}

列表样式处理

<ul><li>第一个项目</li><li>第二个项目</li><li>第三个项目</li><li>第四个项目</li><li>第五个项目</li>
</ul>
/* 第一个和最后一个列表项特殊样式 */
li:first-child,
li:last-child {font-weight: bold;
}/* 前三个列表项添加图标 */
li:nth-child(-n + 3)::before {content: "★ ";color: gold;
}/* 偶数列表项背景色 */
li:nth-child(2n) {background-color: #f0f0f0;
}

卡片布局

<div class="card-container"><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>
.card-container {display: grid;grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));gap: 20px;
}/* 第一行卡片 */
.card:nth-child(-n + 3) {grid-row: 1;
}/* 第一个卡片特殊样式 */
.card:first-child {grid-column: span 2;background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);color: white;
}/* 最后一个卡片 */
.card:last-child {background-color: #f8f9fa;border: 2px dashed #ccc;
}

5. 高级技巧

组合使用

/* 选择第一个div类型的子元素,且是父元素的第三个子元素 */
div:first-of-type:nth-child(3) {background-color: yellow;
}/* 选择最后一个p元素,但不是唯一的p元素 */
p:last-of-type:not(:only-of-type) {border-bottom: 1px solid #ccc;
}

复杂选择器

/* 选择article中第一个h2标题 */
article h2:first-of-type {margin-top: 0;
}/* 选择section中第三个div子元素 */
section > div:nth-child(3) {transform: scale(1.1);
}/* 选择父元素中唯一的一个span */
div span:only-of-type {color: red;
}

6. 浏览器兼容性

大部分结构伪类选择器都有很好的浏览器支持:

  • :first-child - IE7+
  • :last-child - IE9+
  • :nth-child() - IE9+
  • :first-of-type 系列 - IE9+
  • :not() - IE9+(简单选择器),IE11+(复杂选择器)

7. 性能考虑

结构伪类选择器的性能从高到低:

  1. :first-child / :last-child(性能较好)
  2. :nth-child() 简单数字(如 :nth-child(3)
  3. :nth-child() 复杂数学表达式(如 :nth-child(3n+1)
  4. :nth-of-type() 系列(需要遍历同类型元素)

8. 最佳实践

  1. 优先使用简单选择器:如 :first-child:nth-child(1) 性能更好
  2. 合理组合:结合使用多个伪类可以实现复杂效果
  3. 注意兼容性:在需要支持老版本 IE 时要特别注意
  4. 语义化:结构伪类使 CSS 更具语义性,减少对额外类名的依赖

结构伪类选择器是现代 CSS 的重要组成部分,它们让开发者能够基于文档结构创建灵活、动态的样式,而无需添加额外的类名或 JavaScript 代码。

http://www.dtcms.com/a/351022.html

相关文章:

  • 【BUG排查】调试瑞萨RH850F1KMS1时候随机出现进入到unused_isr
  • 一款基于 .NET 开源、功能强大的 Windows 搜索工具
  • GD32VW553-IOT开发板测评 搭建环境到电灯(QA分享)
  • 使用提供的 YAML 文件在 Conda 中创建环境
  • Conda的配置
  • 实时平台Flink热更新技术——实现不停机升级!
  • Caddy + CoreDNS 深度解析:从功能架构到性能优化实践(上)
  • webrtc音频QOS方法一.1(NetEQ之音频网络延时DelayManager计算补充)
  • 设计模式学习笔记-----抽象策略模式
  • 【Ansible】Ansible部署K8s集群--准备环境--配置网络
  • 主流的 AI Agent 开发框架
  • 论文阅读(四)| 软件运行时配置研究综述
  • 游戏玩家批量多开挂机如何选择:云手机还是模拟器
  • LabVIEW 场效应晶体管仿真实验平台
  • 工业自动化系统架构-(多动子磁悬浮生产流水线 规划调度执行与协调)
  • 从下载到运行:MySQL 详细安装配置完整教程
  • 【Vue3】Cesium实现卫星及无人机轨迹跟踪
  • 大模型入门实战 | 基于 YOLO 数据集微调 Qwen2.5-VL-3B-Instruct 的目标检测任务
  • 数字IC前端设计——DC综合篇(生成filelist.f)
  • ADB 安装教程:如何在 Windows、 Linux 上安装 Android Debug Bridge
  • Java数据结构速成【1】
  • 项目设计文档——爬虫项目(爬取天气预报)
  • Qt——界面美化 QSS
  • 无人机三维路径规划首选算法:RRT_
  • 基于大模型的智能占卜系统实战-Qwen-VL、RAG、FastAPI
  • 【算法--链表题1】2. 两数相加:通俗详解
  • Linux系统网络管理学习.2
  • Spring面试题及详细答案 125道(26-45) -- Spring AOP篇
  • PLC通讯中遇到的实际场景
  • 8.25作业