Zyd*_*nar 0 html css css-selectors
用什么计算的规则:nth-child()?这只是一个错误吗?
.info_row:nth-child(even) {
background: orange
}Run Code Online (Sandbox Code Playgroud)
<div>
<h2>Title</h2>
<div class="info_row">
<div>Category:</div>
<div>data</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
如果我用div.info_rowother换行<div>或删除<h2>它很明显,那.info_row:nth-child(even)是在<h2>没有.info_row类的情况下计算的。
您正在使用:nth-child()选择器,而您应该使用:nth-of-type()选择器:
.info_row:nth-of-type(even) {
background: orange
}Run Code Online (Sandbox Code Playgroud)
<div>
<h2>Title</h2>
<div class="info_row">
<div>Category:</div>
<div>data</div>
</div>
<div class="info_row">
<div>Category 2:</div>
<div>data</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
所述:nth-of-type()选择器选择一个父元素的第n子对应于所述特定类型的(即,div标记)。另一方面,:nth-child()选择器选择父元素的第 n 个子元素,而不考虑 type,因此也计算h2元素。
| 归档时间: |
|
| 查看次数: |
1136 次 |
| 最近记录: |