Home arrow Style Sheets arrow Page 5 - Learn CSS: Pseudo Classes
STYLE SHEETS

Learn CSS: Pseudo Classes


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.

Author Info:
By: Michael Youssef
Rating: 4 stars4 stars4 stars4 stars4 stars / 38
July 11, 2005
TABLE OF CONTENTS:
  1. · Learn CSS: Pseudo Classes
  2. · The :link and :visited Pseudo Classes
  3. · The :hover Pseudo Class
  4. · The :active Pseudo Class
  5. · The :focus Pseudo Class

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Padding Overview
- CSS: Margins Overview
- Top CSS3 Button Tutorials
- More Top CSS3 Tutorials for Animation and Im...
- CSS Mega Menus: Adding Extra Sections
- CSS Mega Menus
- CSS3 Accordian Menu: Horizontally Display Hy...
- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials