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 Cascade Layer That Silently Overrides Styles You Thought Had Higher Specificity

- 04.10.26 - ErcanOPAK

🎨 The Style With Higher Specificity That Still Lost

Cascade layers introduce a ranking that is applied before specificity is ever considered at all – rules in a layer declared later always beat rules in a layer declared earlier, no matter how specific either selector is, and any unlayered rule beats every layered rule entirely. A single, broadly-scoped selector sitting in a layer declared after your component’s own layer can override a precise, deeply specific selector in that component without using an importance flag or even a particularly strong selector, because layer order already decided the outcome before specificity ever entered into it.

🔎 The Problem

@layer base, components, utilities;

@layer components {
  .card__title {
    color: #1e293b; /* high specificity, feels like it should win */
  }
}

@layer utilities {
  [class] {
    color: inherit; /* almost no specificity, in a LATER layer */
  }
}

/* The utilities layer was declared after components in the @layer
   statement at the top, so EVERY rule in utilities outranks EVERY
   rule in components for any property both set - the title's color
   loses to the attribute selector's color, even though that selector
   has far lower specificity on its own. */

✅ Fix: Treat Layer Order as Deciding Before Specificity Even Starts

  • Treat the order inside the layer statement itself as the single most important decision in the cascade – list layers from least to most important and keep that order consistent across every file that declares or adds to those layers.
  • Before debugging a specificity mismatch as though cascade layers did not exist, check the computed styles panel in your browser’s developer tools for which layer each competing rule belongs to – a browser that supports layers shows this directly, and it settles the question immediately instead of guessing from selector specificity alone.
  • Keep broad, low-specificity utility selectors in a layer declared earlier than component-specific styles if you want component styles to override them normally – utilities meant to win over everything, as deliberate overrides, belong in a layer declared last, on purpose, not by accident of file order.

⚠️ Why This Is Easy to Miss

  • Every specificity rule you already know keeps working exactly as expected within a single layer, so the behavior only looks wrong once two competing rules for the same property live in two different layers – an easy detail to lose track of once a codebase has more than a couple of layer blocks spread across files.
  • The statement declaring the overall layer order is often written once, early, in a single base stylesheet – a rule added much later in a completely different file can land in an earlier-declared layer and lose to something that looks far less specific, with nothing at that later file’s own location suggesting why.

Specificity has always decided the tiebreaker inside one layer – a cascade layer decides which layer’s tiebreaker you are even allowed to have.

— UI Engineer

Related posts:

CSS: Fix a background-image url() Built From a Custom Property Never Loading Because CSS Has No Way ...

CSS: Fix a background-image url() Built From a Custom Property Never Loading Because CSS Has No Way ...

Stop Fighting Layouts — Use gap Instead of Margins

Stop Fighting Layouts — Use gap Instead of Margins

CSS: Master Grid Layout for Two-Dimensional Layouts

CSS: Master Grid Layout for Two-Dimensional Layouts

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

HTML5: Fix a contenteditable Element That Silently Strips Formatting When You Paste From Word or Google Docs
Ajax: Fix a Cross-Origin Request That Silently Drops Cookies Because credentials Was Never Set to include

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 562 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