【发布时间】:2021-12-31 05:54:40
【问题描述】:
我正在尝试使用不同的类名设置divs 列表的样式。我希望 divs 与 item 类交替显示为灰色和红色。
上面写着nothing 的那一行只是一个例子。该列表可以散布任意数量的nothing 元素。我只想用这些背景颜色设置class="item"s div 的样式。
以下是我认为使用 nth-of-type 会起作用的方法,但没有:
<html>
<head>
<style>
.item:nth-of-type(even) {
background: red;
}
.item:nth-of-type(odd) {
background: gray;
}
</style>
</head>
<body>
<div class="items">
<div class="item">first</div>
<div class="not-item">nothing</div>
<div class="item">second</div>
<div class="item">third</div>
</div>
</body>
</html>
我也试过nth-child,也没有用。
【问题讨论】:
标签: css css-selectors