This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The :any()
pseudo-class lets you create a selector containing multiple options, any one of which will be matched. This is a simpler alternative to repeating an entire selector multiple times because one item inside it varies.
/* Select any h2 inside a section, article, aside, or nav */ /* Currently supported with -moz- and -webkit- prefixes */ :-moz-any(section, article, aside, nav) h2 { font-size: 4.5rem; } :-webkit-any(section, article, aside, nav) h2 { font-size: 4.5rem; }
:matches()
. It is likely that the syntax and name of :-vendor-any()
will be changed to reflect it in the near future.:-moz-any( <selector># ) :-webkit-any( <selector># )
selector
For example, the following CSS:
/* 3 deep (or more) unordered lists use a square */ ol ol ul, ol ul ul, ol menu ul, ol dir ul, ol ol menu, ol ul menu, ol menu menu, ol dir menu, ol ol dir, ol ul dir, ol menu dir, ol dir dir, ul ol ul, ul ul ul, ul menu ul, ul dir ul, ul ol menu, ul ul menu, ul menu menu, ul dir menu, ul ol dir, ul ul dir, ul menu dir, ul dir dir, menu ol ul, menu ul ul, menu menu ul, menu dir ul, menu ol menu, menu ul menu, menu menu menu, menu dir menu, menu ol dir, menu ul dir, menu menu dir, menu dir dir, dir ol ul, dir ul ul, dir menu ul, dir dir ul, dir ol menu, dir ul menu, dir menu menu, dir dir menu, dir ol dir, dir ul dir, dir menu dir, dir dir dir { list-style-type: square; }
Can be replaced with:
/* 3 deep (or more) unordered lists use a square */ :-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) ul, :-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) menu, :-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) dir { list-style-type: square; }
However, do not use the following: (See the section on performance below.)
:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) :-moz-any(ul, menu, dir) { list-style-type: square; }
This property is particularly useful when dealing with HTML5 sections and headings. Since <section>
, <article>
, <aside>
, and <nav>
can be nested, without :any()
, styling these to match one another can be tricky.
For example, without :any()
, styling all the <h1>
elements at different depths could be very complicated:
/* Level 0 */ h1 { font-size: 30px; } /* Level 1 */ section h1, article h1, aside h1, nav h1 { font-size: 25px; } /* Level 2 */ section section h1, section article h1, section aside h1, section nav h1, article section h1, article article h1, article aside h1, article nav h1, aside section h1, aside article h1, aside aside h1, aside nav h1, nav section h1, nav article h1, nav aside h1, nav nav h1, { font-size: 20px; } /* Level 3 */ /* ... don't even think about it*/
Using :-any()
, though, it's much easier:
/* Level 0 */ h1 { font-size: 30px; } /* Level 1 */ :-moz-any(section, article, aside, nav) h1 { font-size: 25px; } /* Level 2 */ :-moz-any(section, article, aside, nav) :-moz-any(section, article, aside, nav) h1 { font-size: 20px; } /* Level 3 */ :-moz-any(section, article, aside, nav) :-moz-any(section, article, aside, nav) :-moz-any(section, article, aside, nav) h1 { font-size: 15px; }
Bug 561154 tracks an issue with Gecko where the specificity of :-moz-any()
is incorrect. The current (as of Firefox 12) implementation puts :-moz-any()
in the category of universal rules, meaning using it as the rightmost selector will be slower than using an ID, class, or tag as the rightmost selector.
For example:
.a > :-moz-any(.b, .c)
is slower than:
.a > .b, .a > .c
and the following is fast:
:-moz-any(.a, .d) > .b, :-moz-any(.a, .d) > .c
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 12 -webkit- | ? | 4 -moz- | ? | ? | 5 -webkit- |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | ? | Yes -webkit- | ? | ? | ? | 5 -webkit- | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/:any