Style Sheets
  Home arrow Style Sheets arrow Page 5 - 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 :focus Pseudo Class


    (Page 5 of 5 )

    The :focus Pseudo Class applies the styling rules to the element that currently has the focus, and the element returns to its previous style (or the default style) after moving the focus out. Actually, not all elements can get the focus, like the <p> elements. You move (set the focus, I mean) between elements by using the tab key; the text doesn't get the focus. The :focus pseudo class is not implemented yet in IE, but it is implemented in Mozilla 1.7 and Opera 7.5. In the next CSS rule we will have a blue background color with white text when the input field receives the focus. Add the following CSS rule to the CSS code file:

    input:focus
    {
      background-color: blue;
      color: white;
    }

    Structural Pseudo Classes

    CSS 2 introduced one structural pseudo class, the :first-child, and CSS 3 introduced more pseudo classes, but most of them are not yet supported by major browsers, and Microsoft IE doesn't support any of them yet. Structural pseudo classes (such as :first-child, :last-child and :root) apply the styling rules to the element based on where the element exists in the document; that's why we call them structural pseudo classes.

    For example, the :first-child pseudo class applies the styling rules to the element if it's the first child of its parent. The :last-child applies the styles if the element is the last child of its parent. The :root pseudo class applies the styles to the root element, which may not make sense in HTML, because we could use the html element selector -- but it does make perfect sense in XML because we don't know the root element, and it could be anything.

    The next example explains the use of both the :first-child and :last-child pseudo classes.

    In the next HTML code there are two input elements. The first is the first child of the parent <form> element and the second is the last child of the same parent.

    <!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>
        <form>
          <input type="text" />
          <p>This is a Paragraph element between the first child and the
    last child input elements</p>
          <input type="text" />
        </form>
      </body>
    </html>

     

    The following CSS code set the styles of the input element if it's the first-child (or the last-child) of its parent

    input:first-child
    {
      color: white;
      background-color: blue;
    }
    input:last-child
    {
      color: white;
      background-color: red;
    }
     

      


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    STYLE SHEETS ARTICLES

    - Creating Three-Column Web Page Layous with N...
    - 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







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