Reputation: 181
I am trying to get a special styling for ul
li
a
elements. Here's the code:
<ul id="menu">
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">Three</a></li>
</ul>
I'd like the second link (Two) to have a different styling (color
) than the other two (One and Three).
This is what I've been trying, but it does not seem to work:
#menu li a:nth-child(even) {color:red;}
Any tips for getting this to work? Here is a fiddle all set up:
Upvotes: 5
Views: 22475
Reputation: 97672
Try
#menu li:nth-child(even) a {color:red;}
if you want the color on the li as well you'll need also
#menu li:nth-child(even) {color:red;}
You cant just have the li
selector because the colour property is not inherited by the a
tag.
Upvotes: 4
Reputation: 253308
:nth-child()
selects elements from amongst their siblings, in this case the a
elements have no siblings, so you'll need to employ the :nth-child()
pseudo-class to the li
instead:
#menu li:nth-child(even) a {color:red;}
Upvotes: 15