CSS Pseudo Classes
CSS
What is a Pseudo-class?
- A CSS pseudo-class is a keyword added to a selector that targets elements in a specific state.
- It starts with a colon (:).
Example:
:hover, :first-child
Common CSS Pseudo-classes
1. :hover
Styles an element when the mouse hovers over it.
2. :active
Styles an element when it’s being clicked.
3. :focus
Styles an element when it’s selected or focused (like an input field).
4. :first-child
Selects the first child of a parent.
5. :last-child
Selects the last child.
6. :nth-child(n)
Selects the n-th element (e.g., every 2nd row).
7. :checked
Targets a checked checkbox or radio button.
8. :disabled / :enabled
Styles form elements based on whether they’re disabled or enabled.
9. :not(selector)
Selects everything except the specified selector.
Example
Pseudo-class | What it targets |
---|---|
:hover | When user hovers with mouse |
:active | When element is being clicked |
:focus | When an input is focused |
:first-child | First child of a parent |
:nth-child(n) | Specific child based on index |
:checked | Checked input elements |
:disabled | Disabled form controls |
:not() | Excludes certain selectors |