Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
CSS

CSS: Fix a :has() Selector Never Matching an SVG Child

- 28.11.26 - ErcanOPAK

🎨 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.

— UI Engineer

Related posts:

Create Smooth Page Transitions with CSS View Transitions API (No JavaScript)

Create Smooth Page Transitions with CSS View Transitions API (No JavaScript)

CSS: Fix a Flexbox Child's min-width Silently Overriding Its Set width Because the Default Was Never...

CSS: Fix a Flexbox Child's min-width Silently Overriding Its Set width Because the Default Was Never...

CSS Flex Layouts Break with Dynamic Content

CSS Flex Layouts Break with Dynamic Content

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Ajax: Fix a Polling Loop Doubling After a Tab Regains Focus
Windows 11: Fix Quick Settings Volume Lagging Behind Audio

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com