Quick tip: CSS link ordering

July 5th, 2007 · 9:27 am @ Dave Woods  -  5 Comments

From time to time I’ll come across a little tip or something useful that web designers should know or have easy reference to. When I’ve come across these bits of information in the past, I’ve usually added them to my browser bookmarks only to find that they become lost in a sea of favourites.However, so that these bits of information don’t get lost, I’m going to add them to my blog so that they’re much easier to reference.

To start this new feature, I’ll start off with CSS links. A lot of people may not be aware of this but it’s extremely important as to what order these appear in your stylesheet so that all the styles are applied.

Therefore links should be applied in the following order:

a,
a:link {
color: #000000;
text-decoration: none;
}
a:visited { color: #CCCCCC; }
a:visited:hover,
a:hover { color: #CC0000; }
a:active { color: #FF0000; }

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • Reddit
  • StumbleUpon
  • Twitter

5 Comments → “Quick tip: CSS link ordering”


  1. David Zemens

    3 years ago

    Great tip, Dave. I was aware of this, but wonder if you can shed some light on why the order needs to be so?


  2. Dave

    3 years ago

    The order should be like this due to link specificity. For example, moving the a, or a:link to the end of the CSS would stop the active and visited from working due to them being superseded by the a and a:link styles.

    Using CSS in this correct order will ensure that all the styles applied to each state will be rendered correctly.

  3. [...] tempo que quero falar sobre isso aqui no blog, mas hoje, vendo os meus Feeds, acabei achando uma referência legal que me fez criar coragem e escrever sobre o assunto. Quando vamos declarar no CSS os links, [...]


  4. John Porter

    2 years ago

    I found that even if you want to apply the same styles to two of the link types (i.e. a:link, a:hover), it is still better to do them all separately and not use the comma separator. Some browsers can mash this up and your styles will not be effective.

    Good piece of advice, I will also emphasize this with you…

    This is not a tip, it is a must!


  5. Dave

    2 years ago

    John, that’s true if you’re going to change the order but if you want to combine the styles and keep them in the same order then you shouldn’t have any problems using the comma as far as I’m aware.

    I would usually declare them all separately though apart from the a, a:link values.


Leave a Reply