Pseudo-classes
What are Pseudo-classes?
A pseudo-class is used to define a special state
of an element.
For example, it can be used to:
- Style an element when a user mouses over it
- Style visited and unvisited links differently
Syntax
The syntax of pseudo-classes:
selector:pseudo-class {
property:value;
}
Selector | Example | Example description |
---|---|---|
:active | a:active | Selects the active link |
:checked | input:checked | Selects every checked <input> element |
:disabled | input:disabled | Selects every disabled <input> element |
:empty | p:empty | Selects every <p> element that has no children |
:enabled | input:enabled | Selects every enabled <input> element |
:first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
:focus | input:focus | Selects the <input> element that has focus |
:hover | a:hover | Selects links on mouse over |
:invalid | input:invalid | Selects all <input> elements with an invalid value |
:last-child | p:last-child | Selects every <p> elements that is the last child of its parent |
:link | a:link | Selects all unvisited links |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child 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 |
: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 |
:valid | input:valid | Selects all <input> elements with a valid value |
:visited | a:visited | Selects all visited links |