选择最后一个孩子,除非它也是第一个孩子

Dan*_*ams 2 css css-selectors

我为UL的最后一个LI标签设置了一条规则:

.className li:last-child {
  stuff: here;
}
Run Code Online (Sandbox Code Playgroud)

我能够设置一个规则,说:“应用此样式到最后LI标签,除非在CSS的last-child也是first-child”,也就是说,在OL唯一一个LI标签。

Sti*_*ers 5

您可以将其与:not()选择器一起使用。

CSS伪类:not(X)是一种功能表示法,采用简单的选择器X作为参数。它与参数未表示的元素匹配。

li:last-child:not(:first-child) {
  color: red;
}
Run Code Online (Sandbox Code Playgroud)
<ul>
  <li>item a</li>
  <li>item b</li>
  <li>item c</li>
</ul>

<ul>
  <li>only item</li>
</ul>
Run Code Online (Sandbox Code Playgroud)