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 Missing Matches After a DOM Swap

- 24.11.26 - ErcanOPAK

🎨 CSS: Fix a :has() Selector Missing Matches After a DOM Swap

:has() is a relational pseudo-class meant to be evaluated live against the current DOM tree, so its matches are supposed to update automatically whenever the DOM changes underneath it. A framework that swaps content via a wholesale innerHTML replacement, or a virtual-DOM diff that replaces a parent element entirely rather than patching its existing children in place, can detach and reattach a subtree in a way that doesn’t fire the style recalculation some engines expect before :has() is re-evaluated against the new children, so a style depending on it matching a freshly-inserted descendant doesn’t apply until some unrelated layout or paint event forces a recalculation.

🔎 The Problem

/* intended: highlight any .card that now contains a .badge--new,
   inserted dynamically after an AJAX update replaces the card's
   inner HTML wholesale instead of patching it in place */
.card:has(.badge--new) {
  border-color: #f59e0b;
}
/* After element.innerHTML = newCardHtml, the .badge--new span
   IS in the DOM, but the border doesn't apply until the user
   hovers, resizes, or triggers any other unrelated recalc */

✅ Fix: Patch Precisely, or Force the Recalculation

  • Prefer patching only the specific child that changed, with replaceChild or a templating library’s keyed diff, over wholesale innerHTML replacement of a parent, since a full subtree replacement is most likely to desync the relational match’s usual live-update timing.
  • If a wholesale replacement is unavoidable, force a style recalculation immediately after the swap by reading a layout property like offsetHeight on the changed element, which reliably triggers a re-evaluation of :has() against the new subtree.
  • Test the exact swap mechanism your framework actually uses, innerHTML versus appendChild versus a virtual-DOM patch, in each target browser engine, since this timing gap is engine- and swap-method-dependent rather than a documented, consistent rule.

🚨 Why This Is Dangerous

  • A :has()-based style that silently fails to apply only after a dynamic DOM update, never on initial page load, is exactly the kind of bug that passes every static visual review and only shows up for users who trigger the live update path.

A relational selector that matches the DOM perfectly on paper can still miss the moment that DOM actually changed underneath it.

— UI Engineer

Related posts:

How to apply two classes to a single element in CSS

How to apply two classes to a single element in CSS

CSS: Container Queries - The End of Media Queries?

CSS: Container Queries - The End of Media Queries?

CSS: Use Box Shadow for Depth and Dimension

CSS: Use Box Shadow for Depth and Dimension

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

JavaScript: Fix a WeakMap Key Surviving Longer Than Expected
The AI Prompt That Turns an API Response Into a Typed Model

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 @ 629 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