Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

CSS selectors determine which elements a style rule applies to. Start with selectors for an element type, class, ID, or attribute; combine them to describe relationships; and use pseudo-classes and pseudo-elements for states, conditions, and parts of an element.

What is a CSS selector?

A CSS selector is a pattern that matches elements in a document. In a CSS rule, the selector comes before the declarations that style the matching elements:

p {
  color: rebeccapurple;
}

Here, p is the selector, and the declaration sets the text color for matching paragraph elements. The W3C describes selectors as “patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in a document” in its Selectors Level 4 specification.

Selectors are primarily used to target elements for CSS styling. JavaScript can also use CSS selector syntax to find nodes in the DOM, for example with document.querySelector() or document.querySelectorAll(); in that case, the selector identifies nodes for script code rather than applying styles.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Common CSS selector types

These basic selector types identify elements by their name, class, ID, or attributes. The MDN CSS selectors reference documents the broader set of selector syntax.

Type Example What it matches
Type p Elements with the named tag, such as paragraphs.
Class .notice Elements whose class list includes notice.
ID #main The element whose ID is main.
Attribute [disabled] Elements with a disabled attribute.
Attribute value pattern [href^="https"] Elements whose href attribute begins with https.
Universal * Elements broadly, subject to any other conditions in the selector.

Selectors can be combined to narrow a match. For example, p.notice matches paragraph elements that have the notice class. A more specific match can be useful, but selectors that depend on many details of the document structure may be harder to maintain when that structure changes.

How combinators describe relationships

Combinators connect selectors to express how matching elements relate to one another. The four common combinators are a space, >, +, and ~. MDN explains these relationships in its guide to CSS selectors and combinators.

Combinator Relationship Example
Space Descendant: the matching element occurs anywhere inside the earlier element. .card h2
> Direct child: the matching element is an immediate child of the earlier element. .card > h2
+ Next sibling: the matching element immediately follows the earlier element as a sibling. h2 + p
~ Subsequent sibling: the matching element follows the earlier element as a sibling, but need not be immediately next. h2 ~ p

For a complex selector, reason from the rightmost component—the element the rule ultimately matches. In .box h2 + p, the subject is a paragraph. It must immediately follow an h2, and that heading must be somewhere inside an element with class box.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pseudo-classes and pseudo-elements

Pseudo-classes and pseudo-elements add conditions or target parts that ordinary type, class, and attribute selectors cannot express.

Pseudo-classes match states and conditions

A pseudo-class begins with a single colon. It matches an element based on a state, interaction, or condition, including its position in the document tree. Examples include :hover for a hovered element, :checked for a checked form control, and structural pseudo-classes for relationships or positions in the tree. See MDN’s pseudo-class reference.

Pseudo-elements target a part or representation

A pseudo-element begins with two colons in modern CSS syntax. It targets a particular part of an element or a generated representation, such as ::first-line or ::marker. The original four pseudo-elements—::before, ::after, ::first-line, and ::first-letter—also accept their older single-colon spellings. MDN lists details in its pseudo-element reference.

Selector lists and functional pseudo-classes

A selector list uses commas to give alternatives that share the same declarations. An element matches if it matches at least one selector in the list:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
h1, h2, h3 {
  font-family: sans-serif;
}

Selector lists have been widely available across browsers since July 2015, according to MDN’s selector-list reference. That compatibility statement applies to selector lists, not every selector feature.

A regular selector list can become invalid as a whole if one of its selectors is invalid. Functional pseudo-classes such as :is() and :where() accept forgiving selector lists, which can ignore invalid members. They also affect specificity differently: :is() takes the specificity of its most specific argument, while :where() contributes zero specificity. This distinction matters when a rule competes with other rules in the cascade.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to choose and check a selector

  1. Identify the intended elements. Use a type selector for an element category, a class for a reusable group, an ID for an element identified by that ID, or an attribute selector for an attribute or value condition.
  2. Add a relationship only when it matters. Use a space for any descendant, > for a direct child, + for the next sibling, or ~ for a later sibling.
  3. Use pseudo-classes for conditions and pseudo-elements for parts. Confirm that the syntax expresses the intended state or target.
  4. Keep the match understandable. A deeply nested selector may be precise but brittle if the document structure changes; a broad selector may be easier to reuse but can affect unintended elements.
  5. Check compatibility for newer syntax. Selectors Level 4 includes established syntax as well as newer additions, so do not assume every feature in the specification is supported in every browser. Consult the compatibility information for the particular selector before using it in production.

The W3C Selectors Level 4 specification defines selector syntax; MDN’s selector references provide explanations and browser-compatibility information for individual features.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.