Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Rules  ›  Lesson

Selectors and Declarations

Selector 12 minA CSS rule is a selector that picks elements plus a block of property-value declarations that style them.

Selectors and Declarations

Every stylesheet, from a ten-line hobby page to a design system with thousands of rules, is made of one repeated shape. Learn to see that shape and you can read any CSS file, find where a style comes from, and spot a broken rule at a glance.

The rule

The selector picks; the declarations decide.

A rule has two parts:

.dish h3 {
  color: navy;
  font-size: 1.25rem;
}
  • The selector, .dish h3, chooses which elements the rule applies to: here, every h3 inside an element with class dish.
  • The declaration block, everything between { and }, says what to do to them.

Inside the block sit declarations. Each declaration is one instruction with a fixed shape:

property : value ;

color: navy; is one declaration. font-size: 1.25rem; is another. The colon separates the property from its value; the semicolon ends the declaration so the next one can start. The semicolon after the last declaration is optional, but always writing it means you can add a line later without breaking anything.

Whitespace and line breaks are yours to choose. .dish h3{color:navy} is the same rule squeezed onto one line. Most people put one declaration per line for readability.

The shortcut

Read every rule as a sentence: "For every [selector], set [property] to [value], and [property] to [value]." If you cannot read it that way, a piece is missing.

Reading a broken ruleStep 1 of 4
  1. Say the sentence

    .price { color: green font-weight: bold; } reads as "for every .price, set color to green font-weight: bold".

The browser never shows an error for broken CSS. It skips what it cannot understand and carries on, so reading rules as sentences is your main line of defence.

Drills

Answer fast — trust the rule.

quick drill

In .price { color: green; }, which part is the selector?

quick drill

How many declarations are in h2 { color: navy; font-size: 2rem; margin: 0; }?

quick drill

What separates a property from its value?

quick drill

Which declarations from .price { color: green font-weight: bold; } take effect?

Remember

  • A rule is a selector plus a declaration block of property: value; declarations.
  • Read each rule as "for every X, set P to V".
  • A missing semicolon can silently swallow the next declaration.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainLabel the rule

Take the rule .dish h3 { color: navy; font-size: 1.25rem; } and name each part: selector, declaration block, declarations, properties and values. Then explain what happens if the first semicolon is deleted.