Advertisement
  1. Web Design
  2. HTML/CSS
  3. HTML Templates

How to Use New CSS “:is()” for Easy Element Targeting

Scroll to top

The new CSS :is() pseudo-class is shaping up to become a great way to target elements, saving on lines and lines of code, and keeping CSS as readable as possible. It is the next stage of the evolution of :matches() and :any(), adding a little extra functionality and increasing how semantic and intuitive the language behind selectors is.

The new CSS is pseudo-classThe new CSS is pseudo-classThe new CSS is pseudo-class

In this quick tip we’ll make acquaintance with it. Let’s start by seeing how this pseudo-class, officially referred to in the spec as “matches-any”, is used in application. Then we’ll talk about how it’s replacing both :matches() and :any(), and why it makes more sense to use the word “is” instead.

Example Uses of :is()

You can probably intuit the functionality of :is() just by looking at it: it’s a means to test if an element is a certain thing, working essentially as a boolean like the if  you’ll find in many programming languages. For example, in its simplest form these two selectors are functionally the same:

1
article {...}
1
:is(article) {...}

In both cases the styling will activate if an element is an article.

It can also be used to detect multiple types of elements in a single declaration via a comma separated list. For example, again, these two selectors are functionally the same:

1
article, section, aside {...}
1
:is(article, section, aside) {...}

By itself this might not seem very interesting–an added complexity if anything–but it becomes especially helpful when used to target child elements. For example, let’s say you wanted to target h1 elements, but only when they are a child of an article, section or aside element. Usually you would need to write:

1
article h1,
2
section h1,
3
aside h1 {
4
    font-weight: 900;
5
}

However by instead using is:() this can be compressed to:

1
:is(article, section, aside) h1 {
2
    font-weight: 900;
3
}

It really starts to ramp up in efficiency when you have multiple parent and child elements you want to test for. What if, instead of just targeting child h1 elements, as in the above example, you want to target all child heading elements? Without :is(), (and not using classes for the sake of illustration), you would need to write:

1
article h1, article h2, article h3, article h4, article h5, article h6,
2
section h1, section h2, section h3, section h4, section h5, section h6,
3
aside h1, aside h2, aside h3, aside h4, aside h5, aside h6, {
4
    font-weight: 900;
5
}

But with :is() instead you only need:

1
:is(article, section, aside) :is(h1, h2, h3, h4, h5, h6) {
2
    font-weight: 900;
3
}

Why :is() Replaces :any() and :matches()

In previous drafts of the “matches-any” spec this pseudo-class used the name :matches() before subsequently being renamed to :is(). An older selector again is :any(), however unlike :is() or :matches() it doesn’t support complex selectors.

The primary reason it makes sense to instead use :is() comes from another boolean style pseudo-class that functions in the inverse way, that being :not(). With this “matches-none” pseudo-class you can apply styling to everything but a collection of selectors.

It makes good grammatical sense to pair is with notIt makes good grammatical sense to pair is with notIt makes good grammatical sense to pair is with not

For example, earlier we targeted all the headings that are children of article, section and aside elements. With :not() we can target all the headings that are not children of these elements:

1
:not(article, section, aside) :is(h1, h2, h3, h4, h5, h6) {
2
    font-weight: 400;
3
}

It makes good grammatical sense to pair :is() with :not(), hence making the combination logical and intuitive, and this is the primary reason it’s replacing :any() and :matches(). That said, the fact that it's shorter to type has also been cited as one of the reasons it is an improvement.

Current Status and Support

The :is() “matches-any” pseudo-class is currently at working draft status.

You can mostly try it out now in Firefox, Chrome and Safari, however all three browsers are still using the old :any() name, which must be used with vendor prefixes. For Firefox use :-moz-any() and for Chrome and Safari use :-webkit-any(). Chrome and Safari also support :matches().

Fallbacks

You can also use :any() and :matches() to create fallbacks for :is(). For example, a cross browser set of fallbacks might look like this:

1
:-webkit-any(article, section, aside) h1 {
2
    font-weight: 900;
3
}
4
5
:-moz-any(article, section, aside) h1 {
6
    font-weight: 900;
7
}
8
9
:matches(article, section, aside) h1 {
10
    font-weight: 900;
11
}
12
13
:is(article, section, aside) h1 {
14
    font-weight: 900;
15
}

The devs behind PostCSS are also looking at adding the ability to automate the addition of these fallbacks into your code.

Wrapping Up

The :is() pseudo-class is going to make it feasible to handle much more complex element targeting than before, using vanilla CSS where previously we would have most likely needed a pre-processor to handle the task. We can all look forward to it coming out of draft status and reaching full browser support.

For more information check out the links listed below:

Related Links

Learn More About CSS Selectors

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.