Home arrow Style Sheets arrow Page 3 - Learn CSS: Pseudo Classes
STYLE SHEETS

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Learn CSS: Pseudo Classes - The :hover Pseudo Class
(Page 3 of 5 )

The :hover pseudo class is very interesting. It applies the styling rules only when the user hovers the mouse pointer over the area of the element. It removes these styling rules (and as a result it will apply the previous styling rules of the element, if there are any) when the mouse pointer goes out of the element's area.

This pseudo class is supported by IE 5.5 and 6, Mozilla 1.7 and Opera 7.5, but note that with IE it can only be applied to hyperlinks. In the following example, we will stay with the above hyperlinks, but this time we will add the :hover class styles. When the user hovers the mouse point over the hyperlinks, we need the links to have a blue text with a cyan background, and we need to have the same styles for both the visited hyperlinks and the unvisited hyperlinks for color consistency. Having three styles (visited, unvisited and hover hyperlinks styles) complicates background and text color management -- that is why we are creating one style for both visited and unvisited hyperlinks.

a:link
{
  background-color: red;
  color: white;
  font-family: Arial;
  font-size: .8em;
}
a:visited
{
  background-color: red;
  color: white;
  font-family: Arial;
  font-size: .8em;
}
a:hover
{
  background-color: cyan;
  color: blue;
  font-family: Arial;
  font-size: .8em;
}

or we could write it as:

a:link, a:visited
{
  background-color: red;
  color: white;
  font-family: Arial;
  font-size: .8em;
}
a:hover
{
  background-color: cyan;
  color: blue;
  font-family: Arial;
  font-size: .8em;
}

Save the above CSS rules in the CSS file and reload the page, then hover the mouse over a hyperlink. I know that this is not the menu you want but I don't want to introduce new CSS properties now; there are two articles about creating menus with CSS coming in the series, so please wait.

As we said, IE applies the :hover pseudo class only to hyperlinks, but Mozilla 1.7 and Opera 7.5 apply it to any element. Let's look at an example. In the following example I have added an input field to the HTML code. I also added a CSS rule to the CSS file that applies a black background with a white text when the mouse pointer hovers over an input field with the class attribute's value ChangeColors. Let's take a look:

<!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> 
    <input type="text" class="ChangeColors" name="textBox1"/>
  </body>
</html>
 

Add the following CSS Rule to the CSS file:

input.ChangeColors:hover
{
  background-color: black;
  color: white;
}


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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