CSS pseudo-class applies CSS styles to the HTML elements based on some characteristics which cannot be specified using element attributes/classes/IDs.
Syntax:
Selector:pseudo-class {
property: value;
}
Note: 1) :link and :visited pseudo classes to be placed before :hover. Otherwise cascading rules will hide the ‘color’ property of the :hover
Following table explains list of available CSS Pseudo classes.
Pseudo-class | Description | Example |
---|---|---|
Dynamic pseudo classes | ||
:link | Selects all unvisited links. | a:link{color:blue;} |
:visited | Selects all visited links. | a:visited{color:red;} |
:active | Selects an element when mouse over on the element. | a:hover{color:green;} |
:focus | Selects an element when link element gets focus. | a:focus{color:gray;} |
Structural pseudo classes | ||
Selects the first child element of parent element when first child element is a specified element. |
|
|
Selects the last child element of parent element when last child element is a specified element. |
|
|
Selects the first child element of its type of parent element. |
|
|
Selects the last child element of its type of the parent element. |
|
|
Selects a specified element if it is the only child element of parent element. |
|
|
Selects a specified child element if it is only child element of its type under parent element. |
|
|
Selects the empty elements. |
|
|
Selects the root element. Root element refers the HTML element. |
|
|
Selects one or more child elements of parent element. |
|
|
Selects one or more child elements (count starts from bottom to top) of parent element. |
|
|
Selects one or more child elements based on its type of parent element. |
|
|
Selects one or more child elements (count starts from bottom to top) based on its type of parent element. |
|
|
UI element states pseudo classes | ||
Selects enabled elements. |
|
|
Selects disabled elements. |
|
|
Selects checked elements. |
|
|
Target pseudo class | ||
Selects an element which is specified in the referring URI (# followed by element id). |
|
|
Negation pseudo class | ||
Exclude specified element selection. |
|