Style Sheets
  Home arrow Style Sheets arrow Page 3 - Learn CSS: Pseudo Classes
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Learn CSS: Pseudo Classes
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2005-07-11

    Table of Contents:
  • Learn CSS: Pseudo Classes
  • The :link and :visited Pseudo Classes
  • The :hover Pseudo Class
  • The :active Pseudo Class
  • The :focus Pseudo Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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

    More Style Sheets Articles
    More By Michael Youssef


     

    STYLE SHEETS ARTICLES

    - Swapping Column Positions in Web Page Layout...
    - Creating Web Page Layouts with Negative Marg...
    - Creating Gradients for Individual Containers...
    - Creating Gradients for Web Page Headers with...
    - SEO Scrolling Frames Problem Solved
    - Building Cross-Browser Background Effects wi...
    - CSS: Pseudo
    - Using PNG Images to Build Background Effects
    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification
    - CSS: Dimensions
    - CSS: Margins and Padding
    - CSS: Crossing the Border
    - CSS: Text, Fonts, and Tables
    - CSS: Working with Text







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway