🎨 Fix a :has() Selector Never Matching an SVG Child
A <use> element’s referenced content – whatever is inside the <symbol> or <defs> it points to – is rendered through an internal, use-element shadow tree defined by the SVG and HTML specs, not through the light DOM of the <use> element itself. Every CSS selector, :has() included, can see the <use> element as a perfectly normal light-DOM child, but none of them can see past it into the referenced content it’s displaying – the same boundary that keeps a selector from reaching into a custom element’s shadow DOM applies here too.
🔎 The Problem
<svg class="icon-set">
<defs>
<symbol id="icon-alert">
<path class="danger" d="..."></path>
</symbol>
</defs>
<use href="#icon-alert"></use>
</svg>
<style>
/* Never matches - the referenced <path class=danger> lives inside the
<use> element's internal shadow tree, which :has() cannot see into,
the same way it can't match into a custom element's shadow DOM */
svg:has(path.danger) { outline: 2px solid red; }
</style>
✅ Fix: Target the <use> Element Instead
- Target the <use> element itself, or a relationship that stays entirely in the light DOM (svg:has(use[href=’#icon-alert’])), instead of trying to select into content that only exists inside the referenced symbol.
- If the visual state genuinely depends on something inside the referenced symbol, put that state on an attribute of the <use> element itself (a data attribute, or a class) and style from there, since the <use> element is the only part of that reference actually reachable by ordinary CSS selectors, :has() included.
- For an icon system where the same symbol needs different visual states, prefer duplicating the relevant <path> directly inline – skipping <use> for just that icon – over fighting the shadow-tree boundary, when the icon is reused rarely enough that the duplication cost is small.
🚨 Why This Is Dangerous
This isn’t a browser bug or a bleeding-edge quirk you’ll outgrow – the <use> element’s internal content has been outside the reach of ordinary CSS selectors since long before :has() existed, and adding a relational selector onto an already-unreachable target just means the obvious-looking fix silently matches nothing.
:has() can look past almost any descendant – except the ones a browser doesn’t consider part of the document you actually wrote.




