0

这是我的代码

<span></span>
<div class='box'>title</div>
<style>div.box:nth-child(1) { color: red; }</style>

当我删除span标签或盒子上方的任何其他标签时它可以工作,但当我保持原样时它就不行了。为什么会这样?

4

2 回答 2

5

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.

于 2013-09-14T14:55:20.653 回答
2

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

于 2013-09-14T14:56:02.600 回答