Keeping CSS Specificity Under Control
The cascade is one of CSS’s most useful features and one of its easiest places to lose confidence. When several declarations style the same element, the browser resolves them by origin, importance, cascade layer, specificity, and source order. Learning how to structure your CSS to avoid specificity wars means designing predictable rules, rather than appending another selector until the intended result appears.
Australian teams face several durable constraints. Canberra is home to federal agencies that maintain high-traffic services under the Australian Government Design System and public accessibility obligations. These sites often outlive a single vendor or design cycle, so clear cascade rules matter during later maintenance. Sydney and Melbourne’s larger agency and product markets bring another pressure: a build may inherit a Shopify theme, a component library, and a custom admin interface.
A workable approach starts with three agreements: what may set styles, where those styles live, and how a component owner overrides earlier rules. Cascade layers can express those agreements directly. Low-specificity defaults, explicit component selectors, and isolated utilities then make the result readable to the next person who opens the file.
Why Specificity Battles Begin
Selector specificity is usually represented as a three-column weight: IDs in the first, classes, attributes, and pseudo-classes in the second, then element and pseudo-element selectors in the last. A selector such as #account .field input outranks .field input, regardless of source order. Source order breaks ties only when competing declarations have the same importance and cascade context. Inheritance is separate: an inherited value yields to any direct declaration on the element, however weak that declaration is.
Most battles begin with reasonable exceptions. A component is restyled for a page, the exception grows a descendant chain, and a framework selector later wins. Sass or Less nesting can hide the resulting depth, while native CSS nesting can do the same when & is combined with an ancestor. A developer then raises specificity with another class, creating a rule that is difficult to remove because something may depend on it.
Useful warning signs include repeated class combinations, selectors ending in long descendant chains, and overrides that rely on placement at the bottom of a file. In a Sydney agency, a teammate may call a quick review a “squiz”, but a clean squiz still needs a shared definition of acceptable complexity. Otherwise, one reviewer flags a selector while another adds a stronger version during the same fix.
Build a Predictable Cascade Structure
Begin with a small architecture that assigns each kind of CSS a place. For example, @layer reset, tokens, base, components, utilities, overrides; creates an explicit order before any component rule is written. This order is more reliable than relying on import order across different build tools. Teams can reserve the final layer for brief, intentional exceptions and record why they are needed.
Keep design decisions in custom properties and defaults in :where() selectors. A rule such as :where(.card) h2 contributes no specificity through the .card part, allowing a deliberate component class to shape the heading without a fight. Use :is() carefully because it adopts the highest specificity among its arguments. It is useful for concise state lists, but it should not conceal an accidental increase in selector weight.
Give components their own selector boundary. Prefer .site-nav__item to .site-nav ul li a, and let a component own its internal elements through documented classes. This is not a demand for perfectly flat CSS: a short compound selector is easier to reason about than a distant descendant rule. The key is that every specificity increase should correspond to a real relationship in the interface.
Choose Selectors That Scale
Names should describe components and states, not visual accidents. A class such as .ticket-card--featured communicates intent and remains stable when the design changes. Avoid .red-large-box, which ties markup to a screenshot and encourages patches when the colour or size changes. A lightweight naming convention can be BEM, but the valuable part is consistency rather than adherence to one syntax.
Model temporary states as classes or data attributes on the same element, such as .is-open or [aria-expanded='true'], rather than reaching through parents to style a distant child. This keeps the selector close to the state it controls and reduces the number of ancestors involved. It also gives JavaScript a clear contract: toggling aria-expanded updates semantics, while the matching CSS selector handles presentation.
Utilities are the deliberate exception: a small utility can use a consistent, slightly stronger selector and live in its own layer. The same discipline applies when comparing component treatments from a curated style gallery: copying a visual treatment should not mean copying its brittle selector structure. Prefer the underlying pattern, then express it with project classes and tokens.
Test and Document the Cascade
Browser developer tools make hidden competition visible. The Computed panel shows the winning declaration, Chrome’s CSS Overview reveals which rules are active or overridden, and Firefox’s CSS Rules panel provides a similar view. A useful review starts by selecting the element, listing every matching selector, and removing declarations until one rule wins naturally. Coverage reports can expose unused CSS, although they should not be treated as permission to delete styles applied dynamically.
Automation should support that inspection rather than replace it. A Stylelint rule can cap selector specificity, reject !important, or flag deeply nested selectors. Visual-regression tests in Chromium and WebKit can catch changes caused by refactoring, while unit or integration tests can verify the state classes that JavaScript toggles. For a Brisbane City Council service or another Australian public-facing build, these checks fit neatly alongside accessibility testing for keyboard use, zoom, contrast, and assistive technology.
Document the cascade as part of the design system rather than as a separate technical appendix. A short diagram of layers, naming patterns, and permitted overrides helps contributors place new rules correctly. The same need for consistent classification appears in organising a digital museum, where stable labels and relationships help visitors understand artefacts that would otherwise become an opaque collection.
Enforce Conventions Across Teams
The conventions should be few enough to remember and strong enough to enforce in review. Put them in the project’s styling guide, configure them in linting, and assign ownership for shared components. When a product team needs a genuine exception, capture it beside the component instead of allowing a remote override to become normal practice.
- Establish a selector budget, such as no more than two class-level selectors in a component rule.
- Reserve IDs for in-page anchors or legacy contracts, and avoid using them for styling.
- Require a short explanation and local containment for every
!importantdeclaration. - Define cascade layers and import order once, then place new rules in the correct category.
- Run specificity and unused-rule lint checks in continuous integration, with exceptions reviewed.
- Test component states in supported browsers and record intended override behaviour.
Teams that apply these practices gradually reduce selector debt without a large rewrite. They can start with the most frequently edited component, measure the matching rules in developer tools, and replace compound overrides with component-owned states. Over time, the stylesheet becomes easier to search, review, migrate, and hand over—valuable qualities for Australian teams operating across government, council, and commercial environments.