Home arrow Style Sheets arrow Page 2 - Learn CSS: Pseudo Classes

Learn CSS: Pseudo Classes

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.

Author Info:
By: Michael Youssef
Rating: 4 stars4 stars4 stars4 stars4 stars / 38
July 11, 2005
  1. · Learn CSS: Pseudo Classes
  2. · The :link and :visited Pseudo Classes
  3. · The :hover Pseudo Class
  4. · The :active Pseudo Class
  5. · The :focus Pseudo Class

print this article

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">
    <link rel="stylesheet" href="P.css" type="text/css">
    <title>CSS Pseudo Classes</title>
          <a href="http://www.devarticles.com">devarticles Website</a>
          <a href="http://www.aspfree.com">aspfree Website</a>
          <a href="http://www.devshed.com">devshed Website</a> 
          <a href="http://www.seochat.com">SEOchat Website</a>

Here's the CSS code:

  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:

  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.

blog comments powered by Disqus

- CSS Padding Overview
- CSS: Margins Overview
- Top CSS3 Button Tutorials
- More Top CSS3 Tutorials for Animation and Im...
- CSS Mega Menus: Adding Extra Sections
- CSS Mega Menus
- CSS3 Accordian Menu: Horizontally Display Hy...
- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials