The problem in your case is the way :nth-child
works, basically you need to add it at the child/sibling level, not at the parent level (and since your parent
has no different, your selector is basically just like #parent
). With the selector in the way you have it, it taking it as if #parent is the first child you want to select, that's why it's all blue. Make a slight change and it'll be fine:
#parent div:nth-child(1) {
color: blue;
}
<html>
<body>
<div id="parent">
<div>first child</div>
<div>second child</div>
<div>third child</div>
</div>
</body>
</html>
Also, as George just mentioned below, this will apply to any first div occurrence that's a descendant of #parent
, in your case it makes no difference but it might with a different structure, so if you just want to limit it to direct descendants, you can use the more specific selector #parent > div:nth-child(1)