DEV Community

Cover image for Understanding :is() by Examples
Sampad Sarker
Sampad Sarker

Posted on

Understanding :is() by Examples

The :is() a functional CSS pseudo-class. Originally it was named :matches() and :any(), now this selector is renamed to :is().

What does it do?
It takes a selector or a collection of selector(separated by comma) as argument and select any single element or group of elements based on the argument.

Let's go to clarify the concept through examples.

Example 1
normal nesting. Make a group of child element.

Image description

Image description

⚠ Be careful about space between main and :is().If there is no space, nesting will not work. What will happen when without space, it will be discussed after few moment later.

Example 2
Apply the same styling to more than one element. That is the beauty of :is().Make a group of parent element.

Image description

Image description

Example 3
Multiple use of :is() for complex nesting. Here parents elements and child elements are different but combine them in a single line.

Image description

Image description

Example 4
It is also possible to same lavel styling using :is().There is no space before :is().

Image description

Image description

🤞Hope, with this example, you may have clear idea when to put space or not before :is().

Example 5
Use combinator(>, +, ~) with :is().

Image description

Image description

Here, anchore tag(<a>) doesn't change the color, because it is not a direct child. But other child works well because :is() has "forgiving selector" feature.

⚠⚡:is() does not work with pseudo-elements.

:is(::before, ::after) {...} is invalid.

Specificity of :is()
The specificity of :is() will be the highest specificity of the selector from selector list in its argument.

:is(selector1, selector2, .className)--> specificity is 10 beacuse .className.
:is(selector1, #idName, selector2, .className)--> specificity is 100 because #idName.

All examples' live code

That's all 🎉
Thanks for reading till the end.👍

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More