CSS Selectors Basics
What are Selectors?
Selectors are patterns used to target HTML elements you want to style. Think of them as the "address" that tells CSS which elements to apply styles to.
1. Element Selector
The element selector targets all elements of a specific type. Use the element name directly.
2. Class Selector
The class selector targets elements with a specific class
attribute. Use a dot (.) followed by the class name.
Classes are reusable - multiple elements can have the same
class.
3. ID Selector
The ID selector targets a single unique element with a
specific id attribute. Use a hash (#) followed
by the ID name. IDs must be unique on a page.
Selector Comparison
| Selector | Syntax | Use Case |
|---|---|---|
| Element | p { } |
All elements of that type |
| Class | .highlight { } |
Reusable styles |
| ID | #header { } |
Unique elements |
Quick Quiz
Which selector is best for styling multiple elements with the same style?
Enjoying these tutorials?