tags:

views:

386

answers:

2

Hi, I have this HTML

<div id="contentheader">
    <div class="cat">
     <ul>
      <li>
       <a href="http://127.0.0.1/test/" title="Home">Home</a>
      </li>
      <li>
       <a href="http://127.0.0.1/test/option1/" title="Option 1">Option 1</a>
      </li>
      <li>
       <a href="http://127.0.0.1/test/option2/" title="Option 2">Option 2</a>
      </li>
     </ul>
    </div>
</div>

And I apply the styles with this classes

#contentheader .cat {
border-top:3px solid #ccc;
background-color:#eee;
overflow:hidden;
}

#contentheader .cat li a {
color:#999;
font-weight:700;
text-decoration:none;
font-size:13px;
display:block;
border-right:1px solid #fff;
float:left;
padding:10px;
}

#contentheader .cat li a:hover {
background-color:#E9E9E9;
}

The problem is that I need the first <li> (Home) to be different, maybe making the text in other color, or maybe making the font wider. I try assigning a class directly to the <li> or the <a> tags, but it doesn't works, it always take the styles in the #contentheader .cat li a class. What is the best way to do this? Thanks

+3  A: 

You can use the :first-child selector in css:

#contentheader .cat ul li:first-child a {
    color: red;
}

That finds the first li child of the ul. Just make sure it comes after the general li case so that it takes precedence or use the ! important modifier on the color/size definitions.

geofflane
Hardly a surprise, but IE6 doesn't support the first-child pseudo selector. http://kimblim.dk/css-tests/selectors/ has a great overview of which browsers supports what.
Eystein
Thanks, yes it can be a problem if it is not supported in all the actual most used web browsers.
IE 6 barely supports CSS though. :)
geofflane
It is supported in IE 7 and IE 8 though.
geofflane
+1  A: 

You need to increase the specificity of that selector to have it override a rule with a lower selector specificity.

So if you assign the class to the li element, use #contentheader .cat li.class a, for the a element, use #contentheader .cat li a.class.

Gumbo