:has() alternatives
Hey, are there any polyfills or alternate methods for
:has()
for browsers that don’t support it? Thanks in advance11 Replies
depends on what you want to do, it is possible to do some nasty workarounds
one that is pretty trivial is
*:has(.example) .example .other
that can be easily changed to just removing the :has()
pseudo-class
if you have a concrete example, it will be impossible to answer
thinking about this, as long as the element is in the same level or deeper than the element in :has(), it should be trivial
for example: :has(.cookie) i
can be implemented as .cookie + i, .cookie i
but if the .cookie
is on a lower level, it seems to be impossibleHm those are interesting, so there’s not really a ‘good’ workaround? It’s just a feature you’ll have to lose some support using?
i cant answer without concrete examples
as ive explained, some are easily worked around, while others are impossible
What epic said, and ...
imo – a clean alternative involves JavaScript because vice versa
:has()
is our replacement for JavaScript.
If the missing rules wouldn't render the content unreadable then personally I would use "critical" selectors.
Further reading https://caniuse.com/?search=has, https://gs.statcounter.com/browser-market-shareFor example, giving a div a red background if it has a paragraph in-
thats impossible to work around without javascript
well... with a little trickery... and only in certain occasions... (not recommended)
but indeed, you'l need javascript
yeah it does seem like more hassle than it may be worth
i mean all major browsers support it, it's just those extra ones like opera mini and whatnot
Opera mini doesn’t even support grid 😄
Christ 😅
opera mini ... that's a name i havent heard in a decade