CKTIS / CSS
CSS selectors
CSS selectors determine which elements a style rule matches. They combine conditions about an element’s type, class, ID, attributes, or state with relationships in the document tree.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
selector [, selector …] { property: value; }A selector is a pattern matched against document elements. In selector { property: value; }, it selects elements to which the declarations may apply. Matching alone does not guarantee that a declaration wins: the cascade determines the result.
Basic selectors include the type p, class .notice, ID #direct, attribute [disabled], and universal selector *. A pseudo-class such as :hover describes a condition or state; a pseudo-element such as ::before targets an abstract element rather than an ordinary document node.
The compound selector p.notice combines conditions on one element. A space means descendant, > direct child, + immediately following sibling, and ~ subsequent sibling. A comma creates a list of alternatives. Consequently, .panel > p.notice does not match a paragraph nested inside an additional div.
Specificity is compared component by component: IDs first, then classes, attributes, and pseudo-classes, then types and pseudo-elements. The components usually count selectors in these groups, but :where() and its arguments contribute zero. Instead of counting as ordinary pseudo-classes, :is(), :not(), and :has() each contribute the specificity of the most specific selector in their argument list. Specificity does not override earlier cascade criteria such as origin and importance; with all other criteria equal, the later declaration wins. An editor’s draft is not a guarantee of browser support.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Selectors</title>
<style>
p { color: rgb(0, 0, 0); }
.panel > p.notice { color: rgb(0, 128, 0); }
</style>
<section class="panel">
<p class="notice" id="direct">Direct</p>
<div><p class="notice" id="nested">Nested</p></div>
</section>
</html>Result
Direct: rgb(0, 128, 0) Nested: rgb(0, 0, 0)
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.