// TODO: write actual code

CSS Specificity Wars

2012-09-22 | Alex Kucherenko | 2 min read

It started, as these things always do, with a simple request. "Can you make that button blue?" Sure. One line of CSS. How hard can it be?

Forty-five minutes later, I'm three levels deep in a specificity calculator, my stylesheet has grown by two hundred lines, and the button is still stubbornly green. Somewhere in the depths of a third-party component library, a selector with the specificity of a small nation is overriding everything I write. The selector looks like someone rolled their face across a keyboard: div.container > section.main-content .widget-wrapper:not(.disabled) button.btn.btn-primary.

The !important Temptation

Every CSS developer knows the siren call of !important. It sits there in the specification, promising a quick fix, whispering sweet nothings about how "just this once" won't hurt. It's the duct tape of the web -- ugly, effective, and a sign that something has gone structurally wrong.

I've seen codebases where !important appears more often than semicolons. Where developers wage escalating wars of specificity, each one adding another !important to override the previous one, until the stylesheet reads like a shouting match between people who all believe they're right. And technically, they all are. That's the tragedy.

Learning to Let Go

The real lesson of CSS specificity isn't about selectors or cascading order. It's about humility. It's about accepting that a language designed to style documents in 1996 is now responsible for pixel-perfect responsive layouts across twelve screen sizes, and that maybe -- just maybe -- the abstractions we've built on top of it are the problem, not the solution.

I made the button blue eventually. I used a class with a single-level selector, removed three layers of unnecessary nesting, and deleted forty lines of dead CSS that nobody had touched since 2010. The fix was subtraction, not addition. As it usually is. But nobody writes blog posts about deleting code. It's not dramatic enough.

Except, apparently, I just did.