Selectors and colour

Pick which elements to style, and change their colour.

Your CSS is drawn by your own browser, inside this tab. Nothing to install.

A CSS rule

CSS changes how a page looks. The page for these lessons is a small bakery page: a main heading, two paragraphs, a list with the id menu, and a box with the class card.

A rule has a selector, which says which elements it applies to, and declarations between curly brackets. Each declaration is a property, a colon, a value and a semicolon.

Load the example and press Run. Every paragraph on the page turns grey.

Example
p {
  color: gray;
}

Your turn: colour the heading

The selector for an element is its tag name.

Practice task

Make the main heading, the h1, red.

Your answer is run and checked in the lesson. Try it now.

Choosing by class

An element selector styles every element of that kind. To style only some, the page gives them a class, and CSS selects the class with a dot.

The first paragraph on the page has class="intro". The selector .intro matches it and leaves the other paragraphs alone.

Many elements can share one class.

Example
.intro {
  color: green;
}

Your turn: one paragraph only

Use the class, not the tag.

Practice task

Make only the paragraph with the class intro blue. The other paragraphs must stay as they are.

Your answer is run and checked in the lesson. Try it now.

Ids and backgrounds

An id names one single element. CSS selects it with a hash sign: #menu matches the list whose id is menu.

background-color fills the area behind an element. color only changes its text.

Colours can be written as names, such as orange, or as codes, such as #ffa500.

Example
#menu {
  background-color: lightyellow;
}

Your turn: a background

Colour the area behind the special offer.

Practice task

Give the element with the class card a yellow background.

Your answer is run and checked in the lesson. Try it now.