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
- id: C1 claim: A CSS selector finds a specific element so a rule can style it. source: S1 evidence: "You use a CSS selector to find that specific element"
- id: C2 claim: A type selector matches an HTML element directly. source: S1 evidence: "A type selector matches a HTML element directly."
- id: C3 claim: :nth-child() can select elements in the DOM by their index. source: S2 evidence: "select Elements in the DOM by their index"
- id: C4 claim: The of S syntax pre-filters children before An+B logic runs. source: S2 evidence: "Pre-filter a set of child elements before applying An+B logic"
Sources
- [S1] Selectors. web.dev. https://web.dev/learn/css/selectors . License: CC-BY-4.0. Accessed 2026-11-23.
- [S2] More control over :nth-child() selections with the of S syntax. Chrome Developers. https://developer.chrome.com/docs/css-ui/css-nth-child-of-s . License: CC-BY-4.0. Accessed 2026-11-23.
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
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.},
}
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.
- css-selectors.schema.json (schema; CC-BY-4.0)
- css-selectors.table.json (table; CC-BY-4.0)
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.