Two columns Three columns Mixed cols and rows Gallery Archives Clean & Clear Dark or Black High Contrast/ Impact Blog Designs New Corporate/ Professional Unconventional Designs Liquid layouts Web Design Firms Our Favorites What's Popular Last 20 submissions
Horizontal Menus Vertical Menus Multiple/ Nested Levels

CSS Examples Pseudo-classes of CSS

Date: 01/16/2005 Pseudo-classes of CSS

Author: CSS Drive

Pseudo-class are an interesting group of selectors in CSS that apply to elements when they're in a certain state or condition, such as with a link, when the mouse is over it. This makes for some interesting and lightweight visual effects.

Pseudo-class (CSS2.1) Description Applies to
:link Applies to links that have not been visited. links
:visited Applies to links that have been visited. links
:hover Applies to an element which the mouse is currently over. all
:active Applies to an element currently being activated by the user (ie: the mouse is held down over). all
:focus Applies to an element while it has the user focus. all
:first-child Matches an element that is the first child of some other element. all
:first-letter Applies to the first letter of a paragraph. block level elements
:first-line Applies to the first formatted line of a paragraph. block level elements
:lang Applies to an element when it's in the designated language. all

The first four pseudo classes are typically used on links, even though 3rd and 4th apply to elements in general. This is because in IE (as of IE6), ":hover" and ":active" don't work on elements other than links.

CSS Example #1 (link pseudo-classes):

A:link    { color: red }    /* unvisited links */
A:visited { color: blue }   /* visited links   */
A:hover   { background-color: yellow } /* user hovers over link*/
A:active  { color: lime }   /* active links    */

This is a very common CSS definition to manipulate the look of links on the page. The order in which you define the four selectors is important, typically known as LVHA (link visited hover active). If you alter the order, some of your selectors will be suppressed and not take effect due to the way CSS handles cascade/ ordering.

CSS Example #2 (:first-child pseudo-class):

img:first-child{ border: 1px solid gray; }

This applies a border to any image that is the first child of another element.

CSS Example #3 (:lang pseudo-class):

:lang(fr) { font-weight: bold; }

This gives elements that are determined by the browser as French (using a variety of methods) a bold font.

Comments (35)

Heres a cool way to remember the order of the four selectors, "LVHA (link visited hover active)",....

Just think 'LOVE' (LV) and 'HATE' (HA)....

Works for me!

#1: slackerbabe on 11/15 at 07:20 PM

Slackerbabe, nice :-)

#2: NeboN on 03/25 at 07:13 PM

any ideas on using :first-child with IE? So far it doesn't work for me!

I am working on site that has three lists which require formatting of elements but I wish to make the first item look different. Forefox works well (of course) but dirty IE is refusing.

#3: Olmec Sinclair on 08/24 at 08:09 PM

this is a different view and it is nice really.

#5: safertr on 07/28 at 04:59 AM

Great work done!
bid directory

#6: Tom on 08/08 at 08:46 AM

Great example thanks for help!

#7: Mike on 08/08 at 09:48 AM

Thanks for this documentation on CSS2.1, well done!

#8: Guido on 08/09 at 01:19 PM

Very good css,great work.Thanks for sharing..

ad.comc4i.com

#9: hayek on 09/15 at 02:36 PM

looks very interesting.
thanks for article.

regards,
http://www.anarsist.org/

#10: felsefe on 09/29 at 12:30 PM

Post a Comment: Guest (Login or Register)

Name: (Required)

Email: (Required)

(NOT shown on page)

URL:

(Shown on page if entered)

Comments: (Required)

Remember my personal information
Notify me of follow-up comments?

Submit the word you see below:


CSS Forums

CSS Forums: latest threads Last Post Info
css frames layout w/o using iframes (1) 05/10/2008 04:57 am
By: blueory
please review my site (21) 05/09/2008 10:42 am
By: ExplainThat!
Please Review my site (1) 05/09/2008 10:04 am
By: ExplainThat!
Line coming in FF but not in IE6/7 (4) 05/06/2008 09:46 am
By: littlechris1977
req: Need help with the CSS of my WordPress Theme (2) 05/06/2008 04:40 am
By: OMorchoe