Attribute selectors target elements by attribute presence or value and are useful for stateful styling patterns.
Common forms
[disabled][type="email"][data-state="open"][class^="btn-"](prefix match)
Pitfall
Overusing broad attribute selectors can be slower to reason about and harder to maintain than clear class contracts.
Important interview questions and answers
- Q: Why use data-attributes for styling state?
A: Explicit component state hooks without fragile DOM traversal. - Q: Attribute selector vs class selector?
A: Attribute selectors are expressive; classes are often simpler for stable component APIs. - Q: Good use case for
[aria-expanded="true"]?
A: Styling toggle controls based on expanded/collapsed state.
Pitfall: Check cascade order—author stylesheet loses to inline styles and !important surprises.