CSS Pseudo-classes
CSS pseudo-classes are used to add special effects to some selectors.
Syntax
The syntax of pseudo-classes:
CSS classes can also use pseudo-classes:
Anchor Pseudo-classes
In browsers that support CSS, different states of links can be displayed in different ways.
Example
a:link {color:#FF0000;} /* Unvisited links */
a:visited {color:#00FF00;} /* Visited links */
a:hover {color:#FF00FF;} /* Mouse over links */
a:active {color:#0000FF;} /* Selected links */
Note: In CSS definitions, a:hover must come after a:link and a:visited to be effective.
Note: In CSS definitions, a:active must come after a:hover to be effective.
Note: Pseudo-class names are not case-sensitive.
Pseudo-classes and CSS Classes
Pseudo-classes can be used in conjunction with CSS classes:
a.red:visited {color:#FF0000;}
<a class="red" href="css-syntax.html">CSS Syntax</a>
If the link in the above example has been visited, it will appear 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 <!DOCTYPE> for :first-child to work.
Matching the First <p> Element
In the following example, the selector matches any <p> element that is the first child of any element:
Example
p:first-child
{
color:blue;
}
Matching All <i> Elements in the First <p> Element
In the following example, the selector matches all <i> elements in <p> elements that are the first child of any element:
Example
p > i:first-child
{
color:blue;
}
Matching All <i> Elements in <p> Elements That Are the First Child
In the following example, the selector matches all <i> elements in <p> elements that are the first child of any element:
Example
p:first-child i
{
color:blue;
}
CSS - :lang Pseudo-class
The :lang pseudo-class allows you to define special rules for different languages.
Note: IE8 must declare <!DOCTYPE> to support the :lang pseudo-class.
In the following example, the :lang class defines the type of quotation marks for q elements with the attribute value "no":
Example
q:lang(no) {quotes: "~" "~";}
More Examples
Add Different Styles to Hyperlinks
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 a value within a specified range |
| :invalid | input:invalid | Selects all invalid elements |
| :last-child | p:last-child | Selects all <p> elements that are the last child of their parent |
| :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 every element that is not a <p> element |
| :nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
| :nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second-to-last child of its parent |
| :nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second-to-last <p> element of its parent |
| :nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
| :only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
| :only-child | p:only-child | Selects every <p> element that is the only child of its parent |
| :optional | input:optional | Selects <input> elements with no "required" attribute |
| :out-of-range | input:out-of-range | Selects <input> elements with a value outside a specified range |
| :read-only | input:read-only | Selects <input> elements with a "readonly" attribute specified |
| :read-write | input:read-write | Selects <input> elements with no "readonly" attribute |
| :required | input:required | Selects <input> elements with a "required" attribute specified |
| :root | root | Selects the document's root element |
| :target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
| :valid | input:valid | Selects all <input> elements with a valid value |
| :link | a:link | Selects all unvisited links |
| :visited | a:visited | Selects all visited links |
| :active | a:active | Selects the active link |
| :hover | a:hover | Selects links on mouse over |
| :focus | input:focus | Selects the <input> element that has focus |
| :first-letter | p:first-letter | Selects the first letter of every <p> element |
| :first-line | p:first-line | Selects the first line of every <p> element |
| :first-child | p:first-child | Selects every <p> element that is the first child of its parent |
| :before | p:before | Inserts content before every <p> element |
| :after | p:after | Insert content after every <p> element |
| :lang(language) | p:lang(it) | Select a starting value for the lang attribute of <p> elements |