reCAPTCHA WAF Session Token
Programming Languages

An Introduction to the :has() Selector in CSS — SitePoint

In this excerpt from Unleashing the Power of CSS, we take a deep dive into how to select elements with the CSS :has() selector.

Heralded as “the parent selector”, the :has() pseudo-class has far greater range than just styling an element’s ancestor. With its availability in Safari 15.4+ and Chromium 105+, and behind a flag in Firefox, it’s a great time for you to become familiar with :has() and its use cases.

As a pseudo-class, the basic functionality of :has() is to style the element it’s attached to — otherwise known as the “target” element. This is similar to other pseudo-classes like :hover or :active, where a:hover is intended to style the element in an active state.

However, :has() is also similar to :is(), :where(), and :not(), in that it accepts a a list of relative selectors within its parentheses. This allows :has() to create complex criteria to test against, making it a very powerful selector.

To get a feel for how :has() works, let’s look at an example of how to apply it. In the following selector, we’re testing if an

element has an element as a child:

<code class="css language-css"><span class="token selector">article<span class="token pseudo-class">:has</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code>

A possible result of this selector is shown in the image below. Three article elements are shown, two containing images and both having a palegreen background and different padding from the one without an image.

The selector above will apply as long as an element exists anywhere with the

element — whether as a direct child or as a descendant of other nested elements.

If we want to make sure the rule applies only if the is a direct (un-nested) child of the

element, we can also include the child combinator:

<code class="css language-css"><span class="token selector">article<span class="token pseudo-class">:has</span><span class="token punctuation">(</span><span class="token combinator">></span> img<span class="token punctuation">)</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code>

The result of this change is shown in the image below. The same three cards are shown, but this time only the one where the image is a direct child of the

has the palegreen background and padding.

The same three cards are shown, but this time only the one with the image as direct child has the palegreen background and padding

In both selectors, the styles we define are applied to the target element, which is the

. This is why folks often call :has() the “parent” selector: if certain elements exist in a certain way, their “parent” receives the assigned styles.

Note: the :has() pseudo-class itself doesn’t add any specificity weight to the selector. Like :is() and :not(), the specificity of :has() is equal to the highest specificity selector in the selector list. For example, :has(#id, p, .class) will have the specificity afforded to an id. For a refresher on specificity, review the section on specificity in CSS Master, 3rd Edition.

We can also select a target element if it’s followed by a specific sibling element using the adjacent sibling combinator (+). In the following example, we’re selecting an

element only if it’s directly followed by an

:

<code class="css language-css"><span class="token selector">h1<span class="token pseudo-class">:has</span><span class="token punctuation">(</span><span class="token combinator">+</span> h2<span class="token punctuation">)</span></span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code>

In the image below, two

elements are shown. In the first one, because the

is followed by an

, the

has a palegreen background applied to it.

Two articles are shown. The first, with an h1 followed by an h2, has a palegreen background applied to the h1

Using the general sibling combinator (~), we can check if a specific element is a sibling anywhere following the target. Here, we’re checking if there’s a

element somewhere as a sibling of the




Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock