PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

CSS Basics: Styling Links

Posted by on 20th Nov 2008 CSS 14 comments

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

[sourcecode language="css"]a {
color: green;
font-weight: bold;
}[/sourcecode]

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

[sourcecode language="css"]a {
color: blue;
}

#topmenu a {
color: white;
}[/sourcecode]

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.

[sourcecode language="html"]

[/sourcecode]

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

[sourcecode language="css"]a {
color: blue;
font-weight: bold;
}

#topmenu a {
color: white;
font-weight: normal;
}

#sidemenu a {
color: yellow;
}[/sourcecode]

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

[sourcecode language="css"]a {
font-weight: bold;
}
a:link {
color: blue;
}
a:visited {
color: purple;
}
a:hover {
color: red;
}[/sourcecode]

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:

[sourcecode language="css"]a {
font-weight: bold;
text-decoration: none;
}
a:link {
color: blue;
}
a:visited {
color: purple;
}
a:hover {
color: red;
text-decoration: underline;
}[/sourcecode]

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.

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

14 comments - Leave a reply
  • Posted by Logo Designer on 20th Nov 2008

    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.

  • Posted by Play Games Win Prize on 20th Nov 2008

    Sometimes making things all fancy-like is killing it and it distracts your visitors..

    -Mike

  • Posted by Sarah on 20th Nov 2008

    @ 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.

  • Posted by Caribbean Holidays on 21st Nov 2008

    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! :)

  • Posted by Link Analysis on 21st Nov 2008

    CSS makes me crazy. WWW has changed dramatically since 1995. I couldn't follow new stuff anymore. What will be the next css ?

  • Posted by David Anderson on 21st Nov 2008

    @ -Mike

    So how come you changed your links to be "all fancy-like"?

  • Posted by Sarah on 21st Nov 2008

    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 :)

  • Posted by Clare @ Top Christma on 22nd Nov 2008

    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?

  • Posted by Sarah on 22nd Nov 2008

    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/

  • Posted by Dennis Edell on 22nd Nov 2008

    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

  • Posted by Sarah on 27th Nov 2008

    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.

  • Posted by Dennis Edell on 27th Nov 2008

    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. :)