Common CSS Selectors
Choose which HTML elements a CSS rule styles
1. Element selector
Example: h3
Targets every element with that tag name.
2. Class selector
Example: .selector-class-demo
Targets every element carrying that class. Classes are the usual choice for reusable styles.
3. Descendant selector
Example: .selector-descendant-demo h6
Targets matching elements anywhere inside a chosen ancestor.
4. Attribute selector
Example: input[type="email"]
Targets elements whose attribute matches the requested value.
5. Grouped selectors
Example: .selector-note, .selector-tip
Separate selectors with commas when they share declarations.
Grouping keeps the stylesheet DRY.
Reading a selector
- Find the selector before the opening brace.
- Match it against elements in the HTML.
- Apply the declarations only to those matches.
- Prefer simple classes when a style should be reusable.