Pseudo-classes style elements in particular states or structural conditions.
Common pseudo-classes
:hover,:focus-visible,:active:first-child,:last-child,:nth-child():not(),:is(),:where()
A11y warning
Do not rely on :hover alone for critical interactions; keyboard and touch users need equivalent feedback.
Important interview questions and answers
- Q: Why
:focus-visibleover:focussometimes?
A: It provides focus ring where appropriate without forcing it on all pointer interactions. - Q: What does
:not()do?
A: Excludes matching selectors from style application. - Q: What is
:nth-child(2n)used for?
A: Alternating patterns like zebra rows.
Pitfall: Check cascade order—author stylesheet loses to inline styles and !important surprises.