Careeroza
Pseudo-classes
What are Pseudo-classes?
A pseudo-class selects elements in a special state or position, written with one colon: a:hover, li:first-child.
| Pseudo-class | Selects |
|---|---|
:hover, :active | element under the mouse / being clicked |
:focus, :focus-visible | element with keyboard focus |
:visited, :link | visited / unvisited links |
:first-child, :last-child | first / last child of its parent |
:nth-child(2n) | every 2nd child (even); odd, 3n+1... |
:not(.x) | elements that do not match .x |
:checked, :disabled, :invalid | form states |
Example
Example
Hover over the button and the list items:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
button { background: #04AA6D; color: white; border: 0; padding: 8px 16px; }
button:hover { background: #282A35; }
li:nth-child(odd) { background: #f1f1f1; }
li:hover { color: #04AA6D; }
li:last-child { font-weight: bold; }
input:invalid { border: 2px solid red; }
</style>
</head>
<body>
<button>Hover me</button>
<ul>
<li>One</li><li>Two</li><li>Three</li><li>Four (last)</li>
</ul>
<input type="email" value="not-an-email">
</body>
</html>Result