We met pseudo elements in the last article. In this article we will discuss pseudo classes, which give you the ability to apply certain styling rules on specific states of an element. In this part we discuss the following pseudo classes: :link, :visited, :hover, :active and :focus, :root, :first-child, and :last-child. Note that with CSS 3 there are many new pseudo classes. Not all of them are implemented, however, so I will discuss only the pseudo classes that have been implemented by the major browsers.
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.
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:
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.