search
HomeWeb Front-endCSS TutorialAn Introduction to the :has() Selector in CSS

This article explores the CSS :has() selector, a powerful tool for selecting elements based on their content. While relatively new, its support is growing across major browsers (Safari 15.4 , Chromium 105 , and behind a flag in Firefox).

An Introduction to the :has() Selector in CSS

The :has() pseudo-class styles the target element—the element to which it's attached—if specific conditions within its parentheses are met. This is similar to other pseudo-classes like :hover or :active. Unlike those, however, :has() accepts a list of relative selectors, enabling complex selection criteria.

For instance, article:has(img) styles any <article></article> element containing an <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173907547642572.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="An Introduction to the :has() Selector in CSS ">

To target only direct children, use the child combinator: article:has(> img)<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173907547642572.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="An Introduction to the :has() Selector in CSS ">:has() :has(#id, p, .class)To target only direct children, use the child combinator:

article:has(> img). The specificity of

is determined by the most specific selector within its parentheses (e.g., :has() inherits the specificity of an ID selector).h1:has( h2) <h1></h1>Sibling selectors can also be used with <h2></h2>. ~ styles an ul:has(~ p) element only if it's directly followed by an <ul></ul> element. The general sibling combinator (<p></p>) allows for broader sibling selection. For example,

styles a

if it has a An Introduction to the :has() Selector in CSS

sibling anywhere after it.

An Introduction to the :has() Selector in CSS

:has()article:has(> img:first-child):has(h1 h2)<article></article> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173907548383844.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="An Introduction to the :has() Selector in CSS "> selectors can be chained to create AND conditions. For example,

styles an

only if it has an An Introduction to the :has() Selector in CSS

An Introduction to the :has() Selector in CSS

A CodePen demo showcasing these examples is available. This article is an excerpt from Unleashing the Power of CSS: Advanced Techniques for Responsive User Interfaces, available on SitePoint Premium.

Frequently Asked Questions (FAQs) about CSS :has Selector (This section remains largely unchanged, as it accurately reflects the historical and current browser support for the selector.)

What is the CSS :has Selector and How Does it Work?

The CSS :has() selector is a powerful tool allowing you to select an element based on its children. It's a relational pseudo-class, meaning the selection depends on the relationship between elements.

Can I Use the CSS :has Selector in All Browsers?

Browser support for :has() is improving but isn't yet universal. Check sites like Can I Use for the most up-to-date information.

How Can I Use the :has Selector with jQuery?

While native browser support is growing, you can use :has() with jQuery for broader compatibility. The syntax is very similar to the CSS version.

What is the Difference Between the :has Selector and Other CSS Selectors?

:has() is unique in its relational nature; it selects based on the presence of child elements, unlike selectors that focus solely on attributes or position.

Are There Any Alternatives to the :has Selector?

Alternatives include JavaScript or more complex combinations of existing CSS selectors, but these often lack the elegance and conciseness of :has().

Why is the :has Selector Not Supported in All Browsers?

Initially, performance concerns regarding rendering speed hindered widespread adoption. However, browser support is improving as these concerns are addressed.

Can I Use the :has Selector in a CSS File?

Yes, but only in browsers that support it.

What is the Syntax for the :has Selector?

The basic syntax is :has(selector).

Can I Use the :has Selector with Other Selectors?

Yes, :has() can be combined with other selectors for more complex selections.

What is the Future of the :has Selector?

The future looks bright; :has() is becoming increasingly prevalent as browser support improves.

The above is the detailed content of An Introduction to the :has() Selector in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version