The Truth About ā!importantā
Published on MarĀ 10, 2015 (updated FebĀ 5, 2024), filed under development, css (feed). (Share this on Mastodon orĀ Bluesky?)
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development. If the optimization of CSS is of particular import to you, Iāve collected several concepts in a brief book: CSS Optimization Basics.
This was on my mind for the last eight thousand years, and I needed a quick cheeky cushion post between my latest (tech) book and upcoming closure of my one and a half years of traveling around the world. What are we waitingĀ for.
Sometimes I wake up at night, full of agony, tears in my eyes. The Holiest Alliance Against !important
is haunting me. I see their countless crusaders gallop at innocent web developers with merciless force, incessantly blowing their deafening horns to never ever ever ever use !important
, ever; !important
, code of the wicked.
I wake up because, nonsense.
Nonsense, because !important
is anāimportant part of the cascade. !important
is so important, it even has its own section in the spec:
CSS attempts to create a balance of power between author and user style sheets. By default, rules in an authorās style sheet override those in a userās style sheet
[ā¦].However, for balance, an ā!importantā declaration (the delimiter token ā!ā and keyword āimportantā follow the declaration) takes precedence over a normal declaration. Both author and user style sheets may contain ā!importantā declarations, and user ā!importantā rules override author ā!importantā rules. This CSS feature improves accessibility of documents by giving users with special requirements (large fonts, color combinations, etc.) control over presentation.
!important
, a legitimate feature of CSS, is a tool. Itās first and foremost a critical tool for users, for who itās what guarantees that their rules (when they ever put any in place) get applied. And itās an important tool for authors, us web developers, not just to test (admit, 99% here have used !important
for debugging purposes) but also to come up with sometimes incredibly elegant solutions.
To ban !important
, as my nightmare-widened eyes see much too many times, resembles dogmatic over-simplification. To ban !important
doesnāt show understanding of what itās doing and what itās there for, and with that often looks like convenience, convenience not to be bothered to contemplate what could be behind !important
.
I wonāt explain the intricacies of !important
, either. Not here. Harry Roberts, for example, attempts so already, sharing some of the issues, and what we can do instead, and likewise does Chris Coyier. I normally buy myself time by advising not to use !important
too liberally, and all the time. But the point of this post is simply to, once and for all, establish that:
!important
is important. !important
is legitimate. Itās useful. Itās not going to go away. And so instead of screaming āno !important
,ā we should relax, and use (and teach to use) !important
responsibly.
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a web developer, manager, and author. Iāve been working as a technical lead and engineering manager for companies youāve never heard of and companies you use every day, Iām an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OāReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)