Learn CSS: Pseudo Classes - The :link and :visited Pseudo Classes
(Page 2 of 5 )
The :link pseudo class applies styling rules only to the unvisited hyperlinks, and the :visited Pseudo Class applies to the visited hyperlinks. So what's the difference between using the element selector a to apply the style and using the pseudo class selector :link? Actually there's a big difference. Using the :link pseudo class applies the style only to those <a> elements that have the href attribute and not document links (those <a> elements with the name attribute). On the other hand, using the element selector a applies the style to all <a> elements.
The :link and :visited pseudo classes are supported by IE 5.5, 6 and Mozilla 1.7 and Opera 7.5 too. Let's take a look at an example of using the :link pseudo class. We need our hyperlinks to have a white text with a YellowGreen background and Arial font with a size of .8em.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="P.css" type="text/css">
<title>CSS Pseudo Classes</title>
</head>
<body>
<table>
<tr>
<td>
<a href="http://www.devarticles.com">devarticles Website</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.aspfree.com">aspfree Website</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.devshed.com">devshed Website</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.seochat.com">SEOchat Website</a>
</td>
</tr>
</table>
</body>
</html>
Here's the CSS code:
a:link
{
background-color: YellowGreen;
color: white;
font-family: Arial;
font-size: .8em;
}

As you can see, three out of four hyperlinks have been styled; why? On my machine I have visited only www.devarticles.com. I didn't visit the other websites. What happened here is that the browser has used the default style for the visited websites and our style for the unvisited websites. I will delete the history and see what the result will be.

Now these hyperlinks are unvisited. Let's add some code to differentiate the visited links. We need the visited links to have the same style as the unvisited links, except for the text color, which will be black. Add the following CSS rule to the CSS file:
a:visited
{
background-color: YellowGreen;
color: black;
font-family: Arial;
font-size: .8em;
}
Now open the Web page and visit the first two websites; you will have the following result. Before we leave this section you need to know that the order in which you write the pseudo class styles in the CSS file is very important. We will discuss this in the article about Cascading.

Next: The :hover Pseudo Class >>
More Style Sheets Articles
More By Michael Youssef