Selectors target elements by type, class, id, attributes, state, and structural relationships.
Selector examples
p {}\n.card {}\n#hero {}\ninput[type="email"] {}\nnav a:hover {}
Rendered output
Good selector strategy keeps styles reusable and predictable as UI grows.
Production checklist
- Favor class-based selectors for components.
- Avoid styling by generated IDs.
- Keep selector depth shallow.
Important interview questions and answers
- Q: Why avoid long chained selectors?
A: They are fragile, hard to override, and tightly coupled to DOM structure. - Q: Class vs ID selectors?
A: Classes are reusable; IDs are unique and carry stronger specificity. - Q: What is a state selector example?
A::hover,:focus-visible, or:disabled.
Challenge
Specificity score
- Style
.nav aand#nav adifferently. - Confirm which selector wins in DevTools.
Done when: ID selector overrides the class selector as expected.