← All notes

CSS selectors: find the elements a rule should style

Answer: A CSS selector finds the elements a rule should style. A type selector, a class selector, and an ID selector each match an element in their own way. :nth-child() can select elements in the DOM by their index, and of S can pre-filter a set of child elements first.

Type, class, and ID

You use a CSS selector to find a specific element and apply a CSS rule.

Type selectors match an HTML element by its tag name, such as section.

Class selectors match any element that lists that name in its class attribute.

An HTML element with an id attribute should be unique on a page. Only one element should have that ID value.

Selecting by index with :nth-child()

Elements can be chosen from the DOM by their index with the :nth-child() pseudo-class.

The An+B microsyntax gives fine control over which elements you want to select.

If you pass of S, An+B then runs only on children that already match that selector list.

Pre-filter a set of child elements before applying An+B logic on it.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: web.dev, Selectors; Chrome Developers, More control over :nth-child() selections with the of S syntax. CC-BY-4.0 (https://web.dev/learn/css/flexbox). Copyright Google LLC. web.dev text is CC BY 4.0. CC-BY-4.0 (https://developers.google.com/site-policies). Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License. Copyright Google LLC. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: web.dev, Selectors; Chrome Developers, More control over :nth-child() selections with the of S syntax. CC-BY-4.0 (https://web.dev/learn/css/flexbox). Copyright Google LLC. web.dev text is CC BY 4.0. CC-BY-4.0 (https://developers.google.com/site-policies). Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License. Copyright Google LLC.

Cite this

Permanent ID: iil:note:9fj9q7wn

iinventedlight. 2026. "CSS selectors: find the elements a rule should style." iinventedlight Library, iil:note:9fj9q7wn, version of 2026-11-23. https://iinventedlight.com/library/notes/css-selectors/. License: CC-BY-4.0.

@misc{iil_note_9fj9q7wn,
  author = {{iinventedlight}},
  title = {{CSS selectors: find the elements a rule should style}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/css-selectors/},
  note = {Permanent ID iil:note:9fj9q7wn. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/css-selectors.md.