CSS selectors

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

The CSS selectors module defines the patterns to select elements to which a set of CSS rules are then applied along with their 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.

توجه: This page introduces a CSS module. To find an exhaustive list of all selectors defined by CSS specifications, see the selectors reference page.

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](/en-US/docs/Web/API/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

The CSS selectors module also introduces the column combinator (||). Currently, no browsers support this feature.

Selectors

  • {{CSSXref(":active")}}
  • {{CSSXref(":any-link")}}
  • {{CSSXref(":autofill")}}
  • {{CSSXref(":buffering")}}
  • {{CSSXref(":checked")}}
  • {{CSSXref(":default")}}
  • {{CSSXref(":defined")}}
  • {{cssxref(":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()")}}
  • {{CSSXref(":hover")}}
  • {{CSSXref(":in-range")}}
  • {{CSSXref(":indeterminate")}}
  • {{CSSXref(":interest-source")}}
  • {{CSSXref(":interest-target")}}
  • {{CSSXref(":invalid")}}
  • {{cssxref(":is()")}}
  • {{cssxref(":lang()")}}
  • {{CSSXref(":last-child")}}
  • {{CSSXref(":last-of-type")}}
  • {{CSSXref(":link")}}
  • :matches() (obsolete legacy selector alias for {{CSSXref( ":is", ":is()")}})
  • {{CSSXref(":modal")}}
  • {{CSSXref(":muted")}}
  • {{cssxref(":not()")}}
  • {{cssxref(":nth-child()")}}
  • {{cssxref(":nth-of-type()")}}
  • {{cssxref(":nth-last-child()")}}
  • {{cssxref(":nth-last-of-type()")}}
  • {{CSSXref(":only-child")}}
  • {{CSSXref(":only-of-type")}}
  • {{CSSXref(":open")}}
  • {{CSSXref(":optional")}}
  • {{CSSXref(":out-of-range")}}
  • {{CSSXref(":past")}}
  • {{CSSXref(":paused")}}
  • {{CSSXref(":picture-in-picture")}}
  • {{CSSXref(":placeholder-shown")}}
  • {{CSSXref(":playing")}}
  • {{CSSXref(":popover-open")}}
  • {{CSSXref(":read-only")}}
  • {{CSSXref(":read-write")}}
  • {{CSSXref(":required")}}
  • {{CSSXref(":root")}}
  • {{CSSXref(":scope")}}
  • {{CSSXref(":seeking")}}
  • {{CSSXref(":stalled")}}
  • {{CSSXref(":target")}}
  • {{CSSXref(":user-invalid")}}
  • {{CSSXref(":user-valid")}}
  • {{CSSXref(":valid")}}
  • {{CSSXref(":visited")}}
  • {{CSSXref(":volume-locked")}}
  • {{cssxref(":where()")}}
  • [:-webkit- pseudo-classes](/en-US/docs/Web/CSS/Reference/Webkit_extensions#pseudo-classes)
  • Attribute selectors
  • Class selector
  • ID selectors
  • Type selectors
  • Universal selectors

The CSS selectors module also introduces the {{CSSXref(":blank")}}, {{CSSXref(":current")}}, and {{CSSXref(":local-link")}} pseudo-classes. Currently, no browsers support these features.

Terms

Guides

  • CSS selectors and combinators
  • : Overview of the different types of simple selectors and various combinators defined in the CSS selectors and the CSS pseudo modules.
  • CSS selector structure
  • : 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".
  • Pseudo classes
  • : 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.
  • [Using the :target pseudo-class in selectors](/en-US/docs/Web/CSS/Guides/Selectors/Using_:target)
  • : Learn how to use the {{CSSXref(":target")}} pseudo-class to style the target element a URL's fragment identifier.
  • [Privacy and the :visited selector](/en-US/docs/Web/CSS/Guides/Selectors/Privacy_and_:visited)
  • : Explores the style limitations set on the :visited class for user privacy.
  • CSS building blocks: CSS selectors
  • : Introduction to basic CSS selectors, including tutorials on Type, class, and ID selectors, Attribute selectors, Pseudo-classes and pseudo-elements, and Combinators.
  • Learn: UI pseudo-classes
  • : Learn the different UI pseudo-classes available for styling forms in different states.

Related concepts

  • {{CSSXref(":state","state()")}} pseudo-class
  • {{CSSXref(":xr-overlay")}} pseudo-class
  • CSS nesting module
  • [& nesting selector](/en-US/docs/Web/CSS/Reference/Selectors/Nesting_selector)
  • CSS scoping module
  • {{CSSXref(":host")}} pseudo-class
  • {{cssxref(":host()")}} pseudo-class
  • {{cssxref(":host-context()")}} pseudo-class
  • {{CSSXref(":has-slotted")}} pseudo-class
  • {{CSSXref("::slotted")}} pseudo-element
  • CSS overflow module
  • {{cssxref("::scroll-button()")}}
  • {{cssxref("::scroll-marker")}}
  • {{cssxref("::scroll-marker-group")}}
  • {{cssxref(":target-current")}}
  • CSS paged media module
  • {{CSSXref(":left")}} pseudo-class
  • {{CSSXref(":right")}} pseudo-class
  • {{CSSXref(":first")}} pseudo-class
  • :blank pseudo-class
  • 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")}}
  • CSS animations
  • [<keyframe-selector>](/en-US/docs/Web/CSS/Reference/Selectors/Keyframe_selectors)
  • {{CSSXref("@namespace")}} at-rule
  • {{domxref("Document.querySelector")}} method
  • {{domxref("Document.querySelectorAll")}} method
  • {{domxref("NodeList.forEach()")}} method

Specifications

{{Specifications}}

See also