CSS Pseudo-classes (Pseudo-classes)
CSS pseudo-classes are used to add special effects to some selectors.
Syntax
Syntax of pseudo-classes:
selector:pseudo-class {property:value;}
CSS classes can also use pseudo-classes:
selector.class:pseudo-class {property:value;}
anchor pseudo-classes
In browsers that support CSS, the different states of a link can be displayed in different ways
Example
a
:link
{
color:
#FF0000
;
}
/*
unvisited link
*/
a
:visited
{
color:
#00FF00
;
}
/*
visited link
*/
a
:hover
{
color:
#FF00FF
;
}
/*
link on mouse hover
*/
a
:active
{
color:
#0000FF
;
}
/*
selected link
*/
Try it »
Note: In the CSS definition, a:hover must be placed after a:link and a:visited to be valid.
Note: In the CSS definition, a:active must be placed after a:hover to be valid.
**Note:** The names of pseudo-classes are case-insensitive.
Pseudo-classes and CSS classes
Pseudo-classes can be used together with CSS classes:
a
.red
:visited
{
color:
#FF0000
;
}
<
a
class
="
red
"
href
="
css-syntax
.html
">
CSSSyntax
a
If the link in the example above has been visited, it will be displayed in red.
CSS :first-child pseudo-class
You can use the :first-child pseudo-class to select the first child element of a parent element.
**Note:** In versions prior to IE8, you must declare for :first-child to take effect.
Matches the first
Element
In the following example, the selector matches the
element:
Example
p
:first-child
{
color:
blue
;
}
Try it »
Matches all
The first element in the Element
In the following example, selects all matching
first element in the element:
Example
p
i
:first-child
{
color:
blue
;
}
Try it »
Matches all
elements that are the first child among all Element
In the following example, the selector matches all
elements that are the first child among all element:
Example
p
:first-child
i
{
color:
blue
;
}
Try it »
CSS - :lang pseudo-class
The :lang pseudo-class gives you the ability to define special rules for different languages
**Note:** IE8 requires a declaration ofto support the ;lang pseudo-class.
In the following example, the :lang class defines the type of quotation marks for the q element with the attribute value no:
Example
q
:lang
(
no
)
{
quotes:
"~" "~"
;
}
Try it »
More examples
Adding different styles to hyperlinks
This example demonstrates how to add other styles to hyperlinks.
Using :focus
This example demonstrates how to use the :focus pseudo-class.
All CSS pseudo-classes/elements
| Selector | Example | Example Description |
|---|---|---|
| :checked | input:checked | Selects all checked form elements |
| :disabled | input:disabled | Selects all disabled form elements |
| :empty | p:empty | Selects all p elements with no children |
| :enabled | input:enabled | Selects all enabled form elements |
| :first-of-type | p:first-of-type | Selects every p element that is the first p element of its parent |
| :in-range | input:in-range | Selects elements with values within a specified range |
| :invalid | input:invalid | Selects all invalid elements |
| :last-child | p:last-child | Selects the last child of all p elements |
| :last-of-type | p:last-of-type | Selects every p element that is the last p element of its parent |
| :not(selector) | :not(p) | Selects all elements except p elements |
| :nth-child(n) | p:nth-child(2) | Selects the second child of the parent of all p elements |
| :nth-last-child(n) | p:nth-last-child(2) | Selects the second-to-last child of all p elements |
| :nth-last-of-type(n) | p:nth-last-of-type(2) | Selects the second-to-last p child of all p elements |
| :nth-of-type(n) | p:nth-of-type(2) | Selects the second p child of all p elements |
| :only-of-type | p:only-of-type | Selects all elements whose only child is a p element |
| :only-child | p:only-child | Selects all p elements that have only one child |
| :optional | input:optional | Selects elements without the "required" attribute |
| :out-of-range | input:out-of-range | Selects elements with attribute values outside a specified range |
| :read-only | input:read-only | Selects elements with the readonly attribute |
| :read-write | input:read-write | Selects elements without the readonly attribute |
| :required | input:required | Selects elements with the "required" attribute specified |
| :root | root | Selects the root element of the document |
| :target | #news:target | Selects the currently active #news element (the name of the anchor when the URL is clicked) |
| :valid | input:valid | Selects all elements with valid values |
| :link | a:link | Selects all unvisited links |
| :visited | a:visited | Selects all visited links |
| :active | a:active | Selects the currently active link |
| :hover | a:hover | the state when the mouse is over a link |
| :focus | input:focus | Selects an element that has focus after input |
| :first-letter | p:first-letter | Select each
element. |
| :first-line | p:first-line | Select each
The first line of an element |
| :first-child | p:first-child | The selector matches the
Element |
| :before | p:before | Insert content after each
Inserts content before an element |
| :after | p:after | Insert content after each
Inserts content after an element |
| :lang(language) | p:lang(it) | is
element whose lang attribute begins with a specified value |