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;
}

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