CSS -如何获得连续类的元素

uxhixvfz  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(182)

我有十里有相同的类和这之间有4个连续类。我怎么能得到连续类的第一,第二,第三和第四元素?与CSS

.abc {
  color: red;
}

.abc+.xyz {
  color: yellow !important;
}
<ul>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
</ul>`
hgc7kmma

hgc7kmma1#

在这种情况下,请尝试以下操作

.abc.xyz:nth-child(4n - 4) {    
 color: yellow !important;
}
.abc.xyz:nth-child(4n - 3) {
 color: green !important;
}
.abc.xyz:nth-child(4n - 2) {
 color: purple !important;
}
.abc.xyz:nth-child(4n - 1) {
 color: blue !important;
}
e0bqpujr

e0bqpujr2#

您差一点就成功了!只需删除+字符

.abc {
  color: red;
}

.abc.xyz {
  color: yellow !important;
}
<ul>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc xyz">abc xyz</li>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
  <li class="abc">abc</li>
</ul>

相关问题