Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS nth-child misses element

Tags:

html

css

Seems like css nth-child missed its target, any thoughts?

jsFiddle source

HTML:

<a href="#">red</a>
<br />
<a href="#">none</a>
<br />
<a href="#">gray</a>

CSS:

a:nth-child(1) {
    color:red;
}
a:nth-child(3) {
    color:gray;
}
like image 357
dsel Avatar asked Feb 26 '14 12:02

dsel


1 Answers

Use nth-of-type() instead of nth-child() , it will work perfectly if you are removed the <br/> tag , because as @FritsvanCampen comment it counts as a child

a:nth-of-type(1) {
    color:red;
}
a:nth-of-type(3) {
    color:gray;
}

for better understanding refer : http://css-tricks.com/the-difference-between-nth-child-and-nth-of-type/

Fiddle

like image 145
Pranav C Balan Avatar answered Nov 01 '22 12:11

Pranav C Balan