html - ":last-child" 和 ":not(:last-child)" 之间的不同行为
问题描述
我有以下html代码:
<nav>
<ul>
<li>Text1</li>
<li>Text2</li>
<li>Text3</li>
</ul>
</nav>
使用:
nav :last-child {
text-transform: uppercase;
}
结果是:
- 文本1
- 文本2
- 文本3
使用:
nav li:last-child {
text-transform: uppercase;
}
结果是:
- 文本1
- 文本2
- 文本3
使用时:
nav :not(:last-child) {
text-transform: uppercase;
}
结果是:
- 文本1
- 文本2
- 文本3
为什么 ":not(:last-child)" 不需要 "li" 来定位最后一个孩子?谢谢。
解决方案
nav :last-child
包括您的单个<ul>
元素。由于只有一个,它是一个:last-child
。所以它适用text-transform: uppercase;
于所有如果它的内容,在这种情况下,所有三个<li>
元素。它也被应用到最后<li>
,因为它也是一个:last-child
。为了更清楚地看到这一点,这里有一个包含两个<ul>
元素的示例。
nav :last-child {
text-transform: uppercase;
}
<nav>
<ul>
<li>This is not a last-child, and parent not a last-child</li>
<li>This is not a last-child, and parent not a last-child</li>
<li>Last-child of li in this section</li>
</ul>
<ul>
<li>Parent ul of these li's is a last-child</li>
<li>So all three li's</li>
<li>Are uppercase</li>
</ul>
</nav>
nav li:last-child
特定于 only li
,因此它只设置最后一个<li>
.
nav li:last-child {
text-transform: uppercase;
}
<nav>
<ul>
<li>Only applies to li's</li>
<li>So ul has no style applied</li>
<li>But this li is a :last-child</li>
</ul>
<ul>
<li>Only applies to li's</li>
<li>So ul has no style applied</li>
<li>But this li is a :last-child</li>
</ul>
</nav>
最后,nav :not(:last-child)
适用于:不是最后一个孩子的任何事情。
nav :not(:last-child) {
text-transform: uppercase;
}
<nav>
<ul>
<li>This ul is <b>not</b> a :last-child</li>
<li>So all of its content</li>
<li>will be uppercase</li>
</ul>
<ul>
<li>This ul <b>is</b> a :last-child</li>
<li>But these first two li's are not</li>
<li>So they are uppercase</li>
</ul>
</nav>
推荐阅读
- jupyter-notebook - 如何更新 IntSlider 和 InsSlider 框以自动适应样本大小
- python - 为什么此轴上多个直方图的代码不起作用?
- react-native - React Native 发布应用程序崩溃并出现 FATAL EXCEPTION: create_react_context
- python - 无法使用owlready python中的搜索方法从我的本体中获取我的个人
- python - keras不平衡数据训练权重调整
- reactjs - 实际上redux-saga如何在api请求中放置标头?
- c# - 如何记录屏幕绘画的时间
- java - 我的 ResultSet 只用 .next() 做一行,但我确实有 3 个
- reactjs - 如何使用线性渐变中倒在数组上的颜色十六进制代码?
- c++ - 在 Visual Studio 2019 中编译 ATL 项目时,propidlbase.idl 中的重新定义错误