这是我的代码
<span></span>
<div class='box'>title</div>
<style>div.box:nth-child(1) { color: red; }</style>
当我删除span
标签或盒子上方的任何其他标签时它可以工作,但当我保持原样时它就不行了。为什么会这样?
这是我的代码
<span></span>
<div class='box'>title</div>
<style>div.box:nth-child(1) { color: red; }</style>
当我删除span
标签或盒子上方的任何其他标签时它可以工作,但当我保持原样时它就不行了。为什么会这样?
Use :nth-of-type
.
Basically :nth-child
counts ALL the siblings. Regardless of type of element. However :nth-of-type
takes into account the element selected.
Because the div is now the second child. Use :nth-of-type(n)
instead.
<style>
div.box:nth-of-type(1) { color: red; }
</style>
That will select the first div