Skip to the workspace
Pebellslearn by building
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.

css / Selectors and Declarations

Explore each scene at your own pace.

01 · The rule

The rule

Selectors and Declarations

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

Separate the elements a rule selects from the changes its declarations request.

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.