Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Simple Selectors  ›  Lesson

Id Selectors

id 12 minA hash followed by a name selects the one element with that id, and in specificity a single id outranks any number of classes.

Id Selectors

The "Visit us" section needs a soft green background, and it is the only one of its kind. It already has id="visit" so the nav can link to it. You can select it through that id, but ids carry a weight in the cascade that deserves respect.

The rule

A hash means id: #name picks one element, with heavy weight.

#visit {
  background-color: honeydew;
}

Every example uses the Harbour Café page from the first lesson of the course: a header with an h1 and a nav of two links, a section#menu holding an h2 and two article.dish cards (the second is also .special), each with an h3, a p.price and a description p, then a section#visit with an h2 and one p, and a footer with one p.

#visit matches the single element whose id is visit. Ids are case-sensitive and must be unique on the page, so an id selector points at one element at most.

The weight is the important part. An id scores (1,0,0). Specificity is compared column by column from the left, so one id outranks any number of classes. #menu p (1,0,1) beats .dish.special .price (0,3,0).

That power is exactly why many teams avoid ids in stylesheets:

  • To override an id-based rule later, you need another id or !important, and the weights keep escalating.
  • An id cannot be reused, so the style cannot be reused either.

The common approach: keep ids for what needs a unique name, such as in-page links (href="#visit"), form labels and scripts, and style with classes. If you must reach an element that only has an id, [id="visit"] selects it with class-level weight (0,1,0).

The shortcut

Before writing #, ask "will I ever want to override this?" If yes, and you usually will, give the element a class and style that instead.

Why won't this heading turn maroon?Step 1 of 4
  1. The rules

    #menu h2 { color: navy; } and, later, main section h2 { color: maroon; }.

Drills

Answer fast — trust the rule.

quick drill

Which selector matches <section id="visit">?

quick drill

What is the specificity of section#menu?

quick drill

#menu .price { color: navy; } comes first; .dish.special .price { color: orange; } comes later. What colour is the lassi's price?

quick drill

Why do many teams style with classes instead of ids?

Remember

  • #name selects the one element with that id.
  • An id is worth (1,0,0), more than any number of classes.
  • Use ids for links and scripts; style with classes.

Practice — level up

0/1 solved

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

ExplainThe heavy selector

A stylesheet styles the menu heading with #menu h2 { color: navy; }. A new designer adds .section-title { color: maroon; } and puts the class on that heading, but it stays navy. Explain why, and suggest a better way to write the original rule.