CSS comes with a wide variety of pseudo-classes that allow us to target elements based on their state, structure, and user interaction. Below is a categorized summary of commonly used pseudo-classes, complete with brief descriptions and learning priority levels (based on my personal opinion).
ποΈ Element Display State Pseudo-classes
These pseudo-classes target elements based on their display state.
| Pseudo-class | Description | Priority |
|---|---|---|
| :fullscreen | Targets elements in fullscreen mode | ββ |
| :modal | Targets an open <dialog> element displayed as a modal |
ββββ |
| :picture-in-picture | Targets <video> elements in picture-in-picture mode |
β |
| :open | Targets open elements like <details>
|
βββ |
| :popover-open | Targets an element currently open as a popover | ββββ |
β¨οΈ Input Pseudo-classes
Used to target input elements based on their state, validation, and user interaction.
| Pseudo-class | Description | Priority |
|---|---|---|
| :autofill | Targets form fields filled in automatically by the browser | β |
| :enabled | Targets input elements that are enabled | ββ |
| :disabled | Targets input elements that are disabled | βββ |
| :read-only | Targets input elements that are read-only | ββ |
| :placeholder-shown | Targets input elements that show placeholder text | βββ |
| :default | Targets default form elements (like a default button) | β |
| :checked | Targets selected checkboxes or radio buttons | βββββ |
| :blank | Targets completely empty input fields | β |
| :valid | Targets inputs that pass HTML5 validation | ββ |
| :user-valid | Targets valid inputs after user interaction | βββ |
| :invalid | Targets inputs that fail HTML5 validation | ββ |
| :user-invalid | Targets invalid inputs after user interaction | βββ |
| :in-range | Targets inputs with values within a valid range | β |
| :out-of-range | Targets inputs with values outside the valid range | β |
| :required | Targets required input fields | βββ |
| :optional | Targets optional input fields | β |
π Location Pseudo-classes
Target links or elements based on the URL or fragment identifier.
| Pseudo-class | Description | Priority |
|---|---|---|
| :any-link | Targets all <a> elements with an href, whether visited or not |
βββ |
| :link | Targets links that have not been visited | βββββ |
| :visited | Targets links that have been visited | βββββ |
| :local-link | Targets links pointing to the same page (internal links) | β |
| :target | Targets elements that match the URL fragment identifier (e.g., #section) |
βββ |
π³ Tree-structural Pseudo-classes
Used to target elements based on DOM structure.
| Pseudo-class | Description | Priority |
|---|---|---|
| :root | Targets the root element of the document (usually <html>)βgreat for global CSS vars |
βββββ |
| :empty | Targets elements with no children (including text/comments) | βββ |
| :first-child | Targets the first child of its parent | ββββ |
| :first-of-type | Targets the first sibling of its type | βββ |
| :last-child | Targets the last child of its parent | ββββ |
| :last-of-type | Targets the last sibling of its type | βββ |
| :nth-child() | Targets elements based on their position among siblings | βββββ |
| :nth-last-child() | Targets elements from the end based on their position | ββββ |
| :nth-of-type() | Targets siblings of the same type based on order | ββββ |
| :nth-last-of-type() | Targets same-type siblings from the end | βββ |
| :only-child | Targets elements that are the only child of their parent | βββββ |
| :only-of-type | Targets elements that are the only one of their type among siblings | ββ |
π±οΈ User Action Pseudo-classes
These target elements based on user interaction like hovering, clicking, or focusing.
| Pseudo-class | Description | Priority |
|---|---|---|
| :hover | Targets elements when hovered | βββββ |
| :active | Targets elements during active state (click or tap) | βββββ |
| :focus | Targets elements receiving focus | βββββ |
| :focus-visible | Focus styles shown only when navigating with keyboard/tab | ββββ |
| :focus-within | Targets an element that contains a focused child element | βββ |
π§© Functional Pseudo-classes
These pseudo-classes use parentheses () to create more dynamic and flexible selectors.
| Pseudo-class | Description | Priority |
|---|---|---|
| :is() | Simplifies writing complex selectors. Great for targeting multiple elements at once | βββββ |
| :where() | Like :is(), but doesnβt add specificity. Perfect for resets or default styling |
ββββ |
| :has() | A powerful parent selector that targets elements based on their children or content | βββββ |
| :not() | Excludes elements that match a certain selector | βββββ |
These four pseudo-classes are powerful and highly recommended for writing expressive and maintainable CSS selectors.
Disclaimer: The priority ratings above are entirely based on my personal opinion. Feel free to adjust based on your own needs and experience.
If you found this cheat sheet helpful, feel free to π save it, π¨οΈ leave a comment, or π share it with other web developers!
See you in the next post π
Top comments (0)