CSS first-child和div with class name不起作用

时间:2017-06-20 14:38:32

标签: css css-selectors

我用HTML写了这个:

<div> hello</div>
<div class="name"><p>hello</p></div>
<div class="name"><p>hello</p></div>

这在CSS中:

div.name:first-child{
  color: red;
}

我想改变第二个'你好'的文字颜色。变红了。但它不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:2)

这是第二个孩子,而不是第一个孩子,使用nth-child

div.name:nth-child(2){
  color: red;
}
<div> hello</div>
<div class="name"><p>hello</p></div>
<div class="name"><p>hello</p></div>