Skip to content
FORGENO ADS GAMESUnlock all $8

CSS · Unit 1: Styling basics · Lesson 2 of 5

Class and id selectors

A dot selects by class: .price { } styles every element with class="price".

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

A dot selects by class: .price { } styles every element with class="price". A hash selects by id: #contact { } styles the one element with id="contact".

Prefer classes for styling. They can be reused and they are easy to override later.

You can combine selectors: p.note picks only <p> elements with the note class, and nav a picks links inside a <nav>.

Worked example

CSS

.note {
  color: firebrick;
}

#tickets {
  color: navy;
}

The HTML it styles

<p class="note">Bring water.</p>
<p>Parking is free.</p>
<h2 id="tickets">Tickets</h2>

Key terms

Class selector
A dot and a class name, like .price. It styles every element with that class.
Id selector
A hash and an id, like #contact. It styles one element and is very specific.

Practise it in Code Academy

It has 4 exercises (multiple choice, fill in the blank, fix the broken code and write code from a prompt) and takes about 6 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.