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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

洛丽糖

"微博客"的重生与否之相关历程 小小甲虫拿捏 月季从盛放至凋零 “微博客”程序正式停止维护更新了 救命!这串李子看得我口水狂飙! 山野桃林闲游 枝头青桃,满是初夏的清甜念想 从别人家路过拍的,不知道叫什么? 一颗桃子的一生:从青涩到“爆甜”的旅程 田地变虾塘,农村人的前路该往哪走 雪豹速清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只想选最后一个同类型标签,不管后面有没有其他元素