liveBook · Manning

CSS selector

As explained in Web Design Playground, Second Edition

A CSS selector is a pattern that identifies the web page elements to which a style rule applies.

A CSS selector is the part of a style rule that chooses which web page elements receive the declarations inside the braces. Think of it as the browser’s “which ones?” instruction. In `h1 { font-size: 36px; }`, `h1` selects every `<h1>` element on the current page. section 2.3

Selectors can become more precise as your page grows:

  • Use an element selector such as `h1` to target every element of that type. section 2.3
  • Combine an element and class, such as `p.footnote`, to target only paragraphs assigned the `footnote` class. section 19.8
  • Use descendant and child combinations, such as `p.footnote a.external` or `p.footnote > a`, to reach links inside matching paragraphs. section 19.8
  • Separate selectors with commas when several targets share one rule, as in `.pullquote, .sidebar`. section 19.8

A heading with two styles

```css h1 { font-size: 36px; text-align: center; } ``` The selector `h1` tells the browser to style every main heading. The declarations then make those headings larger and centered. Add a second selector when different groups should receive the same treatment: ```css .pullquote, .sidebar { color: #444; background-color: #ccc; } ``` section 2.3 section 19.8

Table 19.1 Some ways to combine selectors (view table figure)

Example

Description

<div class="sidebar alert">

Applies both the class named sidebar and the class named alert to the div element

p.footnote {styles}

Applies a rule to those p elements that have been assigned the class named footnote

p.footnote > a {styles}

Applies a rule to a elements that are the children of those p elements that have been assigned the class named footnote

p.footnote a.external {styles}

Applies a rule to a elements that have been assigned the class named external and that are the descendants of those p elements that have been assigned the class named footnote

#payables-table li:nth-child(even) {styles}

Applies a rule to the even-numbered li elements in the list that have been assigned the ID payables-table

Browse the selector-combination table for examples using classes, descendants, child elements, IDs, and even-numbered list items. section 19.8

How Web Design Playground, Second Edition covers it

How deep it goes
Explains element, class, descendant, child, ID, and comma-separated selectors with examples, but defers broader selector techniques.
Examples
Uses h1 headings, pull quotes, sidebars, footnotes, external links, and even-numbered list items.
What it assumes
Assumes basic page elements and CSS declarations, while defining selector terminology and syntax from the beginning.
What you can do after
Apply CSS selectors to style broad groups or precisely targeted page elements.
How it connects
Connects selectors to CSS properties, declarations, style rules, classes, descendants, child elements, and IDs.

Also explained in

Compare how 8 books explain CSS 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.