CSS selectors
The CSS selectors module defines the patterns to select elements to which a set of CSS rules are then applied along with their {{cssxref("specificity")}}. The CSS selectors module provides us with more than 60 selectors and five combinators. Other modules provide additional pseudo-class selectors and pseudo-elements.
In CSS, selectors are patterns used to match, or select, the elements you want to style. Selectors are also used in JavaScript to enable selecting the DOM nodes to return as a NodeList
.
Selectors, whether used in CSS or JavaScript, enable targeting HTML elements based on their type, attributes, current states, and even position in the DOM. Combinators allow you to be more precise when selecting elements by enabling selecting elements based on their relationship to other elements.
Reference
Combinators and separators
+
(Next-sibling combinator)>
(Child combinator)||
(Column combinator)~
(Subsequent sibling combinator)- " " (Descendant combinator)
|
(Namespace separator)
Selectors
- {{CSSXref(":active")}}
- {{CSSXref(":any-link")}}
- {{CSSXref(":autofill")}}
- {{CSSXref(":blank")}}
- {{CSSXref(":buffering")}}
- {{CSSXref(":checked")}}
- {{CSSXref(":current")}}
- {{CSSXref(":current", ":current()")}}
- {{CSSXref(":default")}}
- {{CSSXref(":defined")}}
- {{CSSXref(":dir", ":dir()")}}
- {{CSSXref(":disabled")}}
- {{CSSXref(":empty")}}
- {{CSSXref(":enabled")}}
- {{CSSXref(":first-child")}}
- {{CSSXref(":first-of-type")}}
- {{CSSXref(":focus")}}
- {{CSSXref(":focus-visible")}}
- {{CSSXref(":focus-within")}}
- {{CSSXref(":fullscreen")}}
- {{CSSXref(":future")}}
- {{CSSXref(":has", ":has()")}}
- {{CSSXref(":hover")}}
- {{CSSXref(":indeterminate")}}
- {{CSSXref(":in-range")}}
- {{CSSXref(":invalid")}}
- {{CSSXref(":is", ":is()")}}
- {{CSSXref(":lang", ":lang()")}}
- {{CSSXref(":last-of-type")}}
- {{CSSXref(":link")}}
- {{CSSXref(":local-link")}}
:matches()
(obsolete legacy selector alias for {{CSSXref( ":is", ":is()")}})- {{CSSXref(":modal")}}
- {{CSSXref(":muted")}}
- {{CSSXref(":not", ":not()")}}
- {{CSSXref(":nth-child", ":nth-child()")}}
- {{CSSXref(":nth-of-type", ":nth-of-type()")}}
- {{CSSXref(":nth-last-child", ":nth-last-child()")}}
- {{CSSXref(":nth-last-of-type", ":nth-last-of-type()")}}
- {{CSSXref(":only-child")}}
- {{CSSXref(":only-of-type")}}
- {{CSSXref(":optional")}}
- {{CSSXref(":out-of-range")}}
- {{CSSXref(":past")}}
- {{CSSXref(":paused")}}
- {{CSSXref(":picture-in-picture")}}
- {{CSSXref(":placeholder-shown")}}
- {{CSSXref(":playing")}}
- {{CSSXref(":read-only")}}
- {{CSSXref(":read-write")}}
- {{CSSXref(":required")}}
- {{CSSXref(":root")}}
- {{CSSXref(":scope")}}
- {{CSSXref(":seeking")}}
- {{CSSXref(":stalled")}}
- {{CSSXref(":target")}}
- {{CSSXref(":target-within")}}
- {{CSSXref(":user-invalid")}}
- {{CSSXref(":user-valid")}}
- {{CSSXref(":valid")}}
- {{CSSXref(":visited")}}
- {{CSSXref(":volume-locked")}}
- {{CSSXref(":where", ":where()")}}
:-webkit-
pseudo-classes- Attribute selectors
- Class selector
- ID selectors
- Type selectors
- Universal selectors
Terms
- {{glossary("Pseudo-class")}} glossary term
- Functional pseudo-classes
- Combinators
- Simple selector
- Compound selector
- Complex selector
- Relative selector
- Selector list
- Specificity
Guides
-
- : Overview of the different types of simple selectors and various combinators defined in the CSS selectors and the CSS pseudo modules.
-
- : Explanation of the structure of CSS selectors and the terminologies introduced in the CSS selectors module, ranging from "simple selector" to "forgiving relative selector list".
-
- : Lists the pseudo-classes, selectors that allow the selection of elements based on state information that is not contained in the document tree, defined in the various CSS modules and HTML.
-
- : Part of CSS building blocks, includes tutorials on Type, class, and ID selectors, Attribute selectors, Pseudo-classes and pseudo-elements, Combinators, Cascade, specificity, and inheritance, and Cascade layers.
-
Using the
:target
pseudo-class in selectors- : Learn how to use the {{CSSXref(":target")}} pseudo-class to style the target element a URL's fragment identifier.
-
- : Learn the different UI pseudo-classes available for styling forms in different states.
-
Locating DOM elements using selectors
- : The selectors API enables using selectors in JavaScript to retrieve element nodes from the DOM.
Related concepts
-
{{CSSXref(":popover-open")}} pseudo-class
-
{{CSSXref(":state","state()")}} pseudo-class
-
CSS nesting module
-
CSS scoping module
- {{CSSXref(":host")}} pseudo-class
- {{CSSXref(":host_function", ":host()")}} pseudo-class
- {{cssxref(":host-context", ":host-context()")}} pseudo-class
- {{CSSXref("::slotted")}} pseudo-element
-
CSS pseudo-element module (representing entities not included in HTML)
- {{CSSXref("::after")}}
- {{CSSXref("::before")}}
- {{CSSXref("::file-selector-button")}}
- {{CSSXref("::first-letter")}}
- {{CSSXref("::first-line")}}
- {{CSSXref("::grammar-error")}}
- {{CSSXref("::marker")}}
- {{CSSXref("::placeholder")}}
- {{CSSXref("::selection")}}
- {{CSSXref("::spelling-error")}}
- {{CSSXref("::target-text")}}
-
- {{CSSXref("::part")}} pseudo-element
-
- {{CSSxRef("::backdrop")}}
-
Other pseudo-elements
- {{CSSxRef("::cue")}}
-
{{CSSXref("@namespace")}} at-rule
-
{{cssxref("important", "!important")}}
-
{{domxref("Document.querySelector")}} method
-
{{domxref("Document.querySelectorAll")}} method
-
{{domxref("NodeList.forEach()")}} method