:first-child选中父元素下第一个子元素,:last-child选中最后一个子元素,二者均要求该元素满足选择器条件且位于对应位置,如p:first-child仅当p为首个子元素时生效,p:last-child同理,关键区别在于目标子元素的位置不同。

:first-child 和 :last-child 是 CSS 中的两个结构性伪类选择器,它们用于选中父元素下的第一个或最后一个子元素。虽然名称相似,但作用位置相反。
标签。
示例:
<div>
<p>我是第一个 p</p>
<p>我是第二个 p</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1531">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680227515332.png" alt="OpenGPT">
</a>
<div class="aritcle_card_info">
<a href="/ai/1531">OpenGPT</a>
<p>给AI不同提示词,立即创建属于自己的ChatGPT应用程序</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="OpenGPT">
<span>162</span>
</div>
</div>
<a href="/ai/1531" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="OpenGPT">
</a>
</div>
</div>
<style>
p:first-child {
color: red;
}
</style>
标签。
示例:
<div>
<p>第一个段落</p>
<p>最后一个段落</p>
</div>
<style>
p:last-child {
font-weight: bold;
}
</style>
基本上就这些。使用时注意结构布局是否符合预期,避免因前后标签干扰导致样式未生效。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号