Last week I explained about linking pages using the anchor tag. This week is about styling those links. For the basics of styling read my previous post Understanding CSS.
Target the anchor tag
In our CSS we can simply target the anchor tag using the a as the selector. So if we wanted our links to always be green and bold we could use
a {
color: green;
font-weight: bold;
}
If you wanted to have links in a div with the id of topmenu to be white (because perhaps the topmenu has a black background) but all other links to be in blue then we could have
a {
color: blue;
}
#topmenu a {
color: white;
}
Notice here, the first ruleset uses just a as the selector. This means all anchor links. However, the second ruleset uses #topmenu a as the selector, and this means all anchor links enclosed in the element with an id of topmenu i.e.
<div id="topmenu"> <a href="/">Home</a> | <a href="about.htm">About</a> | <a href="contact.htm">Contact</a> </div>
By putting the ‘#topmenu a’ ruleset second you automatically override the properties in the first ruleset with the second ruleset’s property values. This means that the colour is set in the first ruleset and the second ruleset overrides the colour for that particular selector. However, if you set perhaps the font weight to be bold in the first ruleset, if you didn’t specify anything for the font weight in the second ruleset then it would inherit the font weight setting from the first ruleset.
So to recap:
- Single tag selectors apply to every instance of that tag on a page eg. a, p, h1, div
- You can override some or all properties by specifying a second (anywhere later in the file), more targeted selector eg. #idname a, #content p
- Any properties not specified in the second ruleset will automatically be inherited from any rulesets already applied to that tag.
If, for example, you had a top menu with a black background colour and a side menu with a blue background colour, and you wanted white links in the top menu, yellow links in the side menu, and blue links everywhere else, but you wanted bold links for the side menu and the page links then you would use
a {
color: blue;
font-weight: bold;
}
#topmenu a {
color: white;
font-weight: normal;
}
#sidemenu a {
color: yellow;
}
As you can see here, the topmenu links have had both their colour and font weight over written, whereas the sidemenu just has the colour changed. Both the topmenu and the sidemenu will only inherit their rules from the first ruleset for the anchor tag. The sidemenu ruleset will not inherit anything from the topmenu ruleset as they are unrelated, so the order of these two rulesets has no bearing on the outcome.
Styling Link States
With page links we can go one step further and style the different states for a hyperlink. A hyperlink typically has 5 states which we target using pseudo-classes:
- :link
- The state for a link that has not yet been visited
- :visited
- The state of a visited link
- :hover
- The state of a link when the mouse is hovering over it
- :active
- The state of a link when it is being clicked
- :focus
- The state of a link when it is selected but hasn’t been clicked (this is unsupported in current versions of Internet Explorer)
These pseudo-classes are not just limited to the anchor tag in browsers that support them (such as Internet Explorer 7, Firefox and Opera).
So now we can specify what colour link to use before and after the link has been visited, and we can also have the link change to a third, different colour, when someone hovers their mouse over it. So, for example, we could have all links, in all states, in bold, and then we’d have non visited, visited and hover states in blue, purple and red respectively. To do this we’d use
a {
font-weight: bold;
}
a:link {
color: blue;
}
a:visited {
color: purple;
}
a:hover {
color: red;
}
Another common property with links is the text-decoration which is used to control whether a link is underlined or not. Often you’ll see sites with non underlined links until you hover over them. To achieve this we can use the following:
a {
font-weight: bold;
text-decoration: none;
}
a:link {
color: blue;
}
a:visited {
color: purple;
}
a:hover {
color: red;
text-decoration: underline;
}
The other values that the text-decoration property can have besides none and underline are overline, line-through, blink and inherit (although I wouldn’t ever recommend using blink as it’s just distracting).
You can set other properties on links such as a background colour, font size, font family and more. Next week I’ll explain more of these properties and how they can be used.













Logo Designer | November 20th, 2008 at 5:27 pm #
Thank you for the post there is a lot of stuff here I didnt really know. It is still a little beyond me as I am more a designer for print work than a web designer but I am begining to find my way around.
Play Games Win Prizes | November 21st, 2008 at 2:56 am #
Sometimes making things all fancy-like is killing it and it distracts your visitors..
-Mike
Sarah (Post Author) | November 21st, 2008 at 3:19 am #
@ Logo Designer - Hope it’s all starting to make sense for you
@ Mike - I’m not quite sure how setting a couple of colours on your links or doing a bit of basic CSS is ‘fancy’ but each to their own. I’m simply explaining how, what you want to do is your choice.
Caribbean Holidays | November 21st, 2008 at 8:38 am #
I started to learn CSS a few months back and have really come to like it, your post is really good and really basic to understand. I always used W3Schools to help me learn the basics of CSS but you go through it much more clearly in some sections. Great work!
Link Analysis | November 21st, 2008 at 9:27 am #
CSS makes me crazy. WWW has changed dramatically since 1995. I couldn’t follow new stuff anymore. What will be the next css ?
David Anderson | November 21st, 2008 at 10:30 am #
@ -Mike
So how come you changed your links to be “all fancy-like”?
Sarah (Post Author) | November 21st, 2008 at 10:33 am #
The web has changed a fair bit since 95, but if you could get to grips with learning HTML when no book existed on the topic, then advancing to strict (x)HTML and CSS shouldn’t be too hard. I made the major leap several years ago, and once I actually sat down and took the time to understand things it was pretty easy to pick up.
It’s changed for the better
Clare @ Top Christmas Toys | November 22nd, 2008 at 9:06 am #
Thanks so much for explaining all this - I’m perfectly happy with HTML but CSS often throws me for a loop. I tend to try and amend things by trial and error! Can anyone recommend a good beginner’s book I could pop on my Christmas list?
Sarah (Post Author) | November 22nd, 2008 at 9:27 am #
Hi Clare,
I’d recommend HTML Utopia - Designing without Tables, from SitePoint
However you can also get the Art and Science of CSS from SitePoint for free right now, as a PDF - http://twitaway.aws.sitepoint.com/
Dennis Edell | November 22nd, 2008 at 12:35 pm #
Hey Sarah, this is awesome.
I’m learning more and more of the inner workings. Not only for myself, but so I don’t have to keep pestering my designer with any minute little detail. LOL
Sarah (Post Author) | November 27th, 2008 at 1:49 pm #
Hey Dennis, glad you like it
Every Thursday I’m posting up a new post on the basics so keep an eye out for them.
Dennis Edell | November 27th, 2008 at 2:36 pm #
Oh I’m not goin’ anywhere.
I’ve passed out your own URL a bit too. If you ever wish to guest post some beginner type stuff, please feel free to contact me.