惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

洛丽糖

"微博客"的重生与否之相关历程 小小甲虫拿捏 月季从盛放至凋零 “微博客”程序正式停止维护更新了 救命!这串李子看得我口水狂飙! 山野桃林闲游 枝头青桃,满是初夏的清甜念想 从别人家路过拍的,不知道叫什么? 一颗桃子的一生:从青涩到“爆甜”的旅程 田地变虾塘,农村人的前路该往哪走 雪豹速清app 很久之前拍的:青黄小樱桃,酸甜正撩人 “XmBooks”主题更新日志动态 网站 SSL 证书过期,火速续上免费三月证书 Typecho走心评论精选功能实现代码教程 本站也实现了走心评论精选功能 老家插秧季,一田新绿入夏来 稻田里撞见,一场新生的蜕变
p:last-child 与 p:last-of-type 的核心区别(AI教程版)
寻梦xunm · 2026-05-31 · via 洛丽糖

这么说明不就一目了然,很多文档当中写得模糊不清,都不知具体有什么区别。

p:last-childp:last-of-type 的核心区别

这两个 CSS 伪类最核心的区别

  • p:last-child:必须是父元素的最后一个子元素同时这个元素是 <p> 标签才生效
  • p:last-of-type:只要求是父元素下所有 <p> 标签中的最后一个,不管它是不是父元素的最后一个子元素

1. 直观代码对比

我们用同一个 HTML 结构,分别测试两个选择器,一眼看懂区别:

<div class="parent">
  <h2>标题</h2>
  <p>第一段</p>
  <p>第二段</p>
  <span>其他标签</span>
</div>

p:last-child

/* 不生效!因为父元素最后一个子元素是 span,不是 p */
p:last-child {
  color: red;
}

生效条件

  1. 是父元素的最后一个子元素
  2. 标签名必须是 <p>

p:last-of-type

/* 生效!选中了最后一个 p 标签(第二段)*/
p:last-of-type {
  color: blue;
}

生效条件
只需要是父元素里最后一个 <p>,不管后面有没有其他标签


2. 详细图解

父元素 div
├─ h2  (不是p,不影响)
├─ p   (第一个p)
├─ p   【最后一个p】 →  :last-of-type 选中它
└─ span(最后一个子元素,不是p) →  :last-child 找不到任何p

3. 什么时候用哪个?

选择器用法场景
:last-child确定最后一个子元素就是这个标签时使用
:last-of-type只想选最后一个同类型标签,不管后面有没有其他元素