liveBook · Manning

ID selector

As explained in CSS in Depth, Second Edition

A CSS selector that matches an element by its ID attribute.

An ID selector matches an element by its ID attribute, using a hash-prefixed name such as #page-title. In the cascade, one ID outweighs any number of class selectors and outranks tag selectors. section 1.1.3

Listing 1.6 Selectors with increasing specificities
html body header h1 {     #1
  color: blue;
}

body header.page-header h1 {     #2
  color: orange;
}

.page-header .title {     #3
  color: green;
}

#page-title {      #4
  color: red;
}

Compare selectors from least to greatest specificity: four tag names, one class with tags, two classes, then the single ID selector #page-title. section 1.1.3

Resolving a highlighted navigation link

A broad #main-nav a rule can keep links teal while #main-nav .featured overrides only the featured link with an orange background. This targeted combination is preferable to adding !important, which forces the declaration outside ordinary specificity decisions. section 1.1.3

How CSS in Depth, Second Edition covers it

How deep it goes
Defines ID precedence, compares selector counts, and stops short of broader cascade details beyond specificity.
Examples
Uses #page-title, navigation links, .featured, and competing selectors to demonstrate specificity and targeted overrides.
What it assumes
Assumes basic CSS selector and declaration knowledge while teaching specificity comparisons through concrete rules.
What you can do after
Diagnose CSS conflicts by comparing IDs, classes, and tags, then choose a targeted selector instead of defaulting to !important.
How it connects
Connects CSS specificity with the cascade, class selectors, type selectors, pseudo-classes, and attribute selectors.

Also explained in

Compare how 3 books explain ID selector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.