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