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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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: 4 stars4 stars4 stars4 stars4 stars / 20
    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

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek