Chapter 1 deck Chapter 2 deck Chapter 3 deck Chapter 4 deck Chapter 5 deck Chapter 6 deck Chapter 7 deck Chapter 8 deck Chapter 9 deck Chapter 10 deck Chapter 11 deck Chapter 12 deck Chapter 13 deck Chapter 14 deck

CSS Syntax, Selectors & Specificity

5 slides · use ←/→ or Space to navigate, F for fullscreen.

Reading view

← Back to the full Chapter 3 deck

CSS Syntax, Selectors & Specificity
Fullscreen for presentation · Print / PDF for a handout
1 / 5
Lesson

CSS Syntax, Selectors & Specificity

The anatomy of a rule, the selector types, and how the cascade decides which style wins.

CSS Syntax, Selectors & Specificity

Anatomy of a CSS Rule

h1 {
  color: blue;
  font-size: 2rem;
}
PartMeaning
SelectorTargets the HTML element(s) to style
PropertyThe style attribute to change (e.g. color)
ValueThe specific style to apply (e.g. blue)
DeclarationA property: value pair, separated by semicolons
CSS Syntax, Selectors & Specificity

CSS Selectors

TypeExampleTargets
Elementh1 { }All <h1> tags
Class.highlight { }Elements with class="highlight"
ID#header { }The unique id="header" element
Attributeinput[type='email'] { }Matches by attribute value (CSS3+)
Pseudo-classa:hover { }Styles based on element state
Parent (2025)div:has(img) { }The long-awaited parent selector — now baseline in all browsers
CSS Syntax, Selectors & Specificity

CSS Specificity & the Cascade

When rules conflict, the more specific rule wins. From highest to lowest priority:

  1. !important — overrides everything
  2. Inline style
  3. ID selector (#id)
  4. Class / pseudo-class
  5. Element selector

2025 tip: use @layer instead of !important to manage specificity without conflicts.

CSS Syntax, Selectors & Specificity

Activity — complete the code

Match each selector type to its example.

{ color: blue; } /* element */
{ background: yellow; } /* class */
{ font-size: 2rem; } /* id */
a: { color: red; } /* state */