CSS Syntax, Selectors & Specificity
The anatomy of a rule, the selector types, and how the cascade decides which style wins.
Anatomy of a CSS Rule
h1 {
color: blue;
font-size: 2rem;
}
| Part | Meaning |
|---|---|
| Selector | Targets the HTML element(s) to style |
| Property | The style attribute to change (e.g. color) |
| Value | The specific style to apply (e.g. blue) |
| Declaration | A property: value pair, separated by semicolons |
CSS Selectors
| Type | Example | Targets |
|---|---|---|
| Element | h1 { } | All <h1> tags |
| Class | .highlight { } | Elements with class="highlight" |
| ID | #header { } | The unique id="header" element |
| Attribute | input[type='email'] { } | Matches by attribute value (CSS3+) |
| Pseudo-class | a:hover { } | Styles based on element state |
| Parent (2025) | div:has(img) { } | The long-awaited parent selector — now baseline in all browsers |
CSS Specificity & the Cascade
When rules conflict, the more specific rule wins. From highest to lowest priority:
!important— overrides everything- Inline style
- ID selector (
#id) - Class / pseudo-class
- Element selector
2025 tip: use @layer instead of !important to manage specificity without conflicts.
Activity — complete the code
Complete the code correctly to mark this lesson as complete automatically.
Match each selector type to its example.
{ color: blue; } /* element */{ background: yellow; } /* class */{ font-size: 2rem; } /* id */a: { color: red; } /* state */