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 :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.
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: