【问题标题】:Select first and second of class [duplicate]选择班级的第一名和第二名[重复]
【发布时间】:2017-11-25 20:48:40
【问题描述】:

我的 SCSS 如下所示:

.wrapper {
    .part {
        ...
        &.wheel {
            ... // code goes here
        }
     }
}

我的 html 看起来像这样:

<div class="wrapper">
    <img src="..." class="part wheel"/>
    <img src="..." class="part wheel"/>
</div>

如何使用 CSS 选择第一个和第二个 wheel?我想给他们不同的属性。

【问题讨论】:

  • 看看:nth-child 选择器,但要小心,因为它是一个元素选择器(而不是类),所以如果中间有任何其他元素,它将不起作用
  • &amp;:nth-of-type(1) { ... }&amp;:nth-of-type(2) { ... }

标签: css sass


【解决方案1】:

使用nth-of-type

.wrapper {
    .part {
        ...
        &.wheel {
            &:nth-of-type(1) { ... }
            &:nth-of-type(2) { ... }
        }
     }
}

【讨论】:

    【解决方案2】:
    .wheel:nth-of-type(1){...}
    .wheel:nth-of-type(2){...}
    

    .wheel:nth-child(1){...}
    .wheel:nth-child(2){...}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多