Selectors are patterns used to target HTML elements for styling. Understanding selectors is fundamental to writing effective CSS.
Basic Selectors
- Element Selector — targets all elements of a type:
p,h1,div - Class Selector — targets elements with a class attribute:
.classname - ID Selector — targets a single element with a specific ID:
#idname - Universal Selector — targets all elements:
* - Grouping Selector — applies the same styles to multiple selectors:
h1, h2, h3
Combinator Selectors
- Descendant —
div p(all p inside div) - Child —
div > p(direct children only) - Adjacent Sibling —
h1 + p(first p after h1) - General Sibling —
h1 ~ p(all p after h1)