This will be made possible with the upcoming :matches()
selector:
.container > :matches(h1, h2, h3):hover > .myicon {
display: inline;
}
While this is currently implemented internally as :any()
in multiple browsers, it is implemented with prefixes, rendering it pointless to use as it forces you to bloat your code even further in order to avoid the rule that unrecognized selectors must invalidate the entire ruleset:
/*
* Why do this when you can just compact your three selectors
* into a single rule as you're already doing?
*/
.container > :-moz-any(h1, h2, h3):hover > .myicon {
display: inline;
}
.container > :-webkit-any(h1, h2, h3):hover > .myicon {
display: inline;
}
.container > :matches(h1, h2, h3):hover > .myicon {
display: inline;
}
In the meantime, if you do not have access to a preprocessor that supports nesting of rules, and you cannot change your markup, you will need to stick with what you have.
Alternatively, you could also remove parts of your selector based on assumptions of your markup. For example, if .myicon
will only ever appear in .container > :matches(h1, h2, h3)
anyway, then you may not need to look for :matches(h1, h2, h3)
— you can just do this instead:
.container > *:hover > .myicon {
display: inline;
}
(The *
is for illustration but not necessary; :hover
by itself is valid CSS, just as .container
and .myicon
are.)