liveBook · Manning

ID selector

As explained in Tiny CSS Projects

An ID selector is a CSS selector that matches the element having a specified ID value.

An ID selector targets the element whose HTML id has a specified value. Write a hash (#) followed immediately by the ID, then add CSS declarations in braces. section 1.6.1

Use ID selectors carefully:

  • Keep each ID unique on the page; repeating an ID makes the HTML invalid. section 1.6.1
  • Prefer reusable class selectors when uniqueness is not essential, because ID-based rules are harder to reuse. section 1.6.1
  • Remember that ID selectors have specificity value 100, so their styles can be difficult to override. section 1.5

Styling a uniquely identified blockquote

For a blockquote with id="quote-by-author", write: #quote-by-author { background: lightgrey; padding: 10px; line-height: 1.75; } The rule applies these declarations specifically to that blockquote. section 1.6.1

Listing 1.7 ID selector
#quote-by-author {
  background: lightgrey;
  padding: 10px;
  line-height: 1.75;
}

View the complete ID-selector listing with background, padding, and line-height declarations. section 1.6.1

How Tiny CSS Projects covers it

How deep it goes
Defines ID syntax, uniqueness, reuse limits, and specificity, but does not develop deeper selector implementation internals.
Examples
Uses a blockquote with id="quote-by-author" and styles it with background, padding, and line-height declarations.
What it assumes
Assumes basic HTML IDs and CSS declarations while establishing ID uniqueness and hash-prefixed selector syntax.
What you can do after
Apply it via CSS to style a uniquely identified HTML element and judge when a reusable class is more maintainable.
How it connects
Connects ID selectors with class selectors, type selectors, CSS specificity, inheritance, and stylesheet precedence.

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 Tiny CSS Projects. The section links point to the parts of the book it draws on.