HOME

CSS Basic
CSS HOME
CSS Introduction
CSS Syntax
CSS How To
CSS Background
CSS Text
CSS Font
CSS Border
CSS Margin
CSS Padding
CSS List

CSS Advanced
CSS Dimension
CSS Classification
CSS Positioning
CSS Pseudo-class
CSS Pseudo-element
CSS Media Types

Examples/Quiz
CSS Examples
CSS Quiz Test

References
CSS2 Reference
CSS2 Print
CSS2 Aural
CSS Units
CSS Colors
CSS Colorvalues
CSS Colornames
Proposal Kit
Please visit our sponsors !

CSS Pseudo-classes

Previous Next

Pseudo-classes are used in CSS to add different effects to some selectors, or to a part of some selectors.


Examples

Hyperlink
This example demonstrates how to add different colors to a hyperlink in a document.

Hyperlink 2
This example demonstrates how to add other styles to hyperlinks.


Syntax

The syntax of pseudo-classes:

selector:pseudo-class {property: value}

CSS classes can also be used with pseudo-classes:

selector.class:pseudo-class {property: value}


Anchor Pseudo-classes

A link that is active, visited, unvisited, or when you mouse over a link can all be displayed in different ways in a CSS-supporting browser:

a:link {color: #FF0000}     /* unvisited link */
a:visited {color: #00FF00}  /* visited link */
a:hover {color: #FF00FF}   /* mouse over link */
a:active {color: #0000FF}   /* selected link */

Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!!

Note: a:active MUST come after a:hover in the CSS definition in order to be effective!!

Note: Pseudo-class names are not case-sensitive.

Note: IE 4 and higher supports the anchor pseudo-class. NN 4.5 and Netscape 6 support the anchor pseudo-class only partially.


Pseudo-classes and CSS Classes

Pseudo-classes can be combined with CSS classes: 

a.red:visited {color: #FF0000}
<a class="red" href="css_syntax.asp">CSS Syntax</a>

If the link in the example above has been visited, it will be displayed in red.


CSS2 - The :first-child Pseudo-class

The :first-child pseudo-class matches a specified element that is the first child of another element.

In this example, the selector matches any p element that is the first child of a div element, and indents the first paragraph inside a div element:

div:first-child p
{
text-indent:25px
}

This selector will match the first paragraph inside the div in the following HTML:

<div>
<p>
First paragraph in div.
This paragraph will be indented.
</p>
<p>
Second paragraph in div.
This paragraph will not be indented.
</p>
</div>

but it will not match the paragraph in this HTML:

<div>
<h1>Header</h1>
<p>
The first paragraph inside the div.
This paragraph will not be indented.
</p>
</div>

In this example, the selector matches any em element that is the first child of a p element, and sets the font-weight to bold for the first em inside a p element:

p:first-child em
{
font-weight:bold
}

For example, the em in the HTML below is the first child of the paragraph:

<p>I am a <em>strong</em> man.</p>

In this example, the selector matches any a element that is the first child of any element, and sets the text-decoration to none:

a:first-child
{
text-decoration:none
}

For example, the first a in the HTML below is the first child of the paragraph and will not be underlined. But the second a in the paragraph is not the first child of the paragraph and will be underlined:

<p>
Visit <a href="http://www.w3schools.com">W3Schools</a>
and learn CSS!
Visit <a href="http://www.w3schools.com">W3Schools</a>
and learn HTML!
</p>


CSS2 - The :lang Pseudo-class

The :lang pseudo-class allows the author to specify a language to use in a document or to use in a specified element.

In the example below, the rule sets the type of quotation marks for an HTML document that is in Norwegian:

html:lang(no)
{
quotes: '« ' ' »'
}

In the next example, the rule sets the type of quotation marks for blockquote elements:

blockquote:lang(no)
{
quotes: '« ' ' »'
}


Pseudo-classes

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Pseudo-classes NN IE W3C Purpose
active   4.0 CSS1 Adds special style to a selected link
hover   4.0 CSS1 Adds special style to a link when you mouse over  it
link 4.0 3.0 CSS1 Adds special style to an unvisited link
visited 4.0 3.0 CSS1 Adds special style to a visited link
:first-child     CSS2 Adds special style to an element that is the first child of some other element
:lang     CSS2

Allows the author to specify a language to use in a specified element



Previous Next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

Domain Name Registration
Domain Name
Registration & More!



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search