Home > Web Front-end > CSS Tutorial > How the CSS :is, :where and :has Pseudo-class Selectors Work

How the CSS :is, :where and :has Pseudo-class Selectors Work

Joseph Gordon-Levitt
Release: 2025-02-09 11:38:10
Original
598 people have browsed it

How the CSS :is, :where and :has Pseudo-class Selectors Work

Key Takeaways

  • The CSS pseudo-class selectors :is(), :where(), and :has() provide new ways to target HTML elements. The :is() selector allows for more efficient styling of multiple elements, reducing the need for verbose selector strings. The :where() selector operates similarly to :is(), but with zero specificity, making it useful for applying baseline styles without increasing specificity. The :has() selector targets an element containing a set of others, providing a long-awaited way to target parent elements.
  • The :is() and :where() pseudo-class selectors are supported in all modern browsers, but not in Internet Explorer. The :has() selector is newer with limited support currently, but is expected to be widely available by 2023. Using these selectors can simplify CSS syntax and reduce the need for CSS preprocessors.
  • The specificity of :is() and :where() selectors differ. The :is() selector has the same specificity as the most specific selector within its arguments, while the :where() selector has zero specificity. This can be practical for CSS resets, which apply a baseline of styles when no specific styling is available, allowing for override of any CSS reset style regardless of the specificity.
CSS selectors allow you to choose elements by type, attributes, or location within the HTML document. This tutorial explains three new options — :is(), :where(), and :has(). Selectors are commonly used in stylesheets. The following example locates all

paragraph elements and changes the font weight to bold:

<span>p {
</span>  <span>font-weight: bold;
</span><span>}
</span>
Copy after login
Copy after login
Copy after login
Copy after login
You can also use selectors in JavaScript to locate DOM nodes:
  • document.querySelector() returns the first matching HTML element
  • document.querySelectorAll() returns all matching HTML elements in an array-like NodeList
Pseudo-class selectors target HTML elements based on their current state. Perhaps the most well known is :hover, which applies styles when the cursor moves over an element, so it’s used to highlight clickable links and buttons. Other popular options include:
  • :visited: matches visited links
  • :target: matches an element targeted by a document URL
  • :first-child: targets the first child element
  • :nth-child: selects specific child elements
  • :empty: matches an element with no content or child elements
  • :checked: matches a toggled-on checkbox or radio button
  • :blank: styles an empty input field
  • :enabled: matches an enabled input field
  • :disabled: matches a disabled input field
  • :required: targets a required input field
  • :valid: matches a valid input field
  • :invalid: matches an invalid input field
  • :playing: targets a playing audio or video element
Browsers have recently received three more pseudo-class selectors…

The CSS :is Pseudo-class Selector

Note: this was originally specified as :matches() and :any(), but :is() has become the CSS standard. You often need to apply the same styling to more than one element. For example,

paragraph text is black by default, but gray when it appears within an

,
, or

The above is the detailed content of How the CSS :is, :where and :has Pseudo-class Selectors Work. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template