Module 1 · Getting Started · Lesson 2 of 5

CSS Syntax & Selectors

Beginner ⏱ 20 min2 sections · 1 exercise · 4-question quiz

Selectors are how you aim. Element, class, ID, attribute, combinators — each targets differently and carries different specificity weight. Master a handful and you can style anything without touching the HTML twice.

1. The core selectors

SelectorMatchesExample
pEvery element of that typep { }
.cardEverything with that class.card { }
#heroThe one element with that id#hero { }
[type="email"]By attribute valueinput[type="email"] { }
*Everything* { margin: 0; }

Day to day, classes do 90% of the work: they're reusable, composable, and carry moderate specificity. Reserve IDs for anchors and JavaScript hooks, not styling.

2. Combinators: selecting by relationship

CSS — four combinators
article p     { }  /* descendant: any p inside article  */
article > p   { }  /* child: only direct children       */
h2 + p        { }  /* adjacent: the p right after an h2 */
h2 ~ p        { }  /* general sibling: every p after h2 */

Combinators let structure do the targeting, so you don't need a class on every element. Grouping helps too: h1, h2, h3 { font-family: ... } styles all three in one rule.

⚠️ Long descendant chains like .nav ul li a span are fragile and hard to override. Prefer a single meaningful class.

3. Try it yourself

Only the paragraphs with class note should turn amber, and only the direct-child paragraph of .box should be bold. Add the two selectors.

exercise.html — editable

4. Quiz — check your understanding

CSS Syntax & Selectors Quiz

4 questions · pass at 60%

Q1. Which selector targets elements with class "active"?

A leading dot selects by class; a leading hash selects by id.

Q2. ul > li selects…

The > child combinator only matches direct children.

Q3. Which is generally best for reusable styling?

Classes are reusable, composable, and moderately specific — the workhorse of CSS.

Q4. h2 + p matches…

The adjacent-sibling combinator matches only the element that comes immediately after.
🏆
Quiz passed? You're one step closer to your certificate.

Every lesson quiz you pass (60%+) is saved in your browser and counts toward the CSSmatic Certificate of Completion.

Track my progress →