你可能不知道的CSS(1)

原创
前端路迹
2024-7-25 08:39
编辑于 2024-7-25 15:35

在 :nth-child 中可以使用 of 关键字,of 后面跟一个选择器,表示了前面匹配参数的范围。

:nth-child(<nth> [of <complex-selector-list>]?) {
  /* ... */
}

示例如下。

<style>
    .row1 span:nth-child(2n + 1) {
        color: red;
    }
    .row2 span:nth-child(2n + 1 of .t) {
        color: red;
    }
</style>

<div class="row1">
    <span class="t">1</span>
    <span class="t">2</span>
    <span>3</span>
    <span class="t">4</span>
    <span class="t">5</span>
    <span>6</span>
    <span>7</span>
    <span>8</span>
    <span>9</span>
    <span class="t">10</span>
</div>
<div class="row2">
    <span class="t">1</span>
    <span class="t">2</span>
    <span>3</span>
    <span class="t">4</span>
    <span class="t">5</span>
    <span>6</span>
    <span>7</span>
    <span>8</span>
    <span>9</span>
    <span class="t">10</span>
</div>

转载请注明出处。本文地址: https://www.qinshenxue.com/article/css-you-might-not-know-part-1.html
关注我的公众号