liveBook · Manning

ID selector

As explained in Web Design Playground, Second Edition

An ID selector is a CSS selector that matches the element identified by a specified HTML id attribute value.

An ID selector targets the element whose HTML id attribute matches a specified value. Write the value with a hash symbol: #id-value. section 19.1 section 19.2

#id-value {
    property1: value1;
    property2: value2; 
    ...
}

Use this pattern in an internal or external stylesheet, replacing id-value with the element's case-sensitive id. section 19.2

Two individually styled sections

Give two div elements the ids section-quote and section-summary, then style them separately with #section-quote and #section-summary. One rule controls the quote's italic, right-aligned appearance; the other controls the summary's bold, centered appearance. section 19.2

Choose between IDs and classes with this quick rule:

  • Use an ID when the style belongs to one and only one element. section 19.2
  • Use a class when styles apply to multiple elements, or might expand to more elements later. section 19.2
  • Prefer classes in general because single-element ID rules can make CSS harder to maintain and troubleshoot. section 19.2

How Web Design Playground, Second Edition covers it

How deep it goes
Explains hash-prefixed syntax, case sensitivity, valid id characters, examples, and class-selection guidance without detailing specificity internals.
Examples
Uses section-quote and section-summary div elements to demonstrate two distinct ID-based style rules.
What it assumes
Assumes basic CSS selectors and HTML id attributes, then teaches hash-prefixed ID syntax from the selector level.
What you can do after
Apply ID selectors in internal or external stylesheets and choose classes when styles need reuse or easier maintenance.
How it connects
Connects ID selectors with HTML id attributes, class selectors, CSS specificity, and the cascade.

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 Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.