css - 将 css 添加到第一个 child 和下一个 child 样式?

标签 css css-selectors pseudo-class

我喜欢这个标记

<div class="sprite player" style="top: 28%; left: 26%;"></div>
<div class="sprite player" style="top: 10%; left: 37%;"></div>
<div class="sprite player" style="left: 36%; top: 28%;"></div>

每个元素都有内联样式,但我想把它放在 CSS 中,我不想像新类一样放置然后添加样式,这也许可以通过伪类第一个子类然后第二个等等来完成。

当我得到新的 css 时,我需要这样的东西

.player:first-child{
top: 28%; left: 26%;
}
.player:first-child + .player{
top: 10%; left: 37%;
}

等等,有没有可能做成那样?

最佳答案

试试这个可能对你有帮助

.player:nth-child(1){
top: 28%; left: 26%;
}
.player:nth-child(2){
top: 10%; left: 37%;
}
.player:nth-child(3){
left: 36%; top: 28%;
}

关于css - 将 css 添加到第一个 child 和下一个 child 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38604376/

相关文章:

javascript - 按顺序获取数组值并为图像分配链接

html - 灵活 div 的背景渐变填充,使用 css 还是图像?

css - 是否有 "previous sibling"选择器?

html - 如何在第三行和第四行中选择第二个div

html - 为什么我的第 nth-of-type 伪类不选择第一个 a,并将其视为第 4 个?

css - float 在绝对定位的 div 内

html - 如何使用重叠的 float div?

css - 悬停时定位不同的 div?

html - CSS :not is not working

html - 为什么第 nth-of-type/nth-child 不适用于嵌套元素?