The Truth About !important
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, code of the wicked.
I wake up because, nonsense.
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.
!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
I won’t explain the intricacies of
!important, either. Not here. My favorite Harry, 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)
I’m Jens, and I’m an engineering lead—currently manager for Developer Experience at LivePerson—and author. I’ve worked as a technical lead for Google, I’m close to W3C and WHATWG, and I write and review books for O’Reilly. I love trying things, sometimes including philosophy, art, and adventure. Here on meiert.com I share some of my views and experiences.
If you have a question or suggestion about what I write, please leave a comment (if available) or a message. Thank you!
Maybe this is interesting to you, too:
- Next: 180 Little Stories After Traveling the World for 18 Months
- Previous: New Book: “The Little Book of HTML/CSS Frameworks”
- More under Web Development, or from 2015
- Most popular posts
Looking for a way to comment? Comments have been disabled, unfortunately.
Get a good look at web development? Try The Web Development Glossary (2020). With explanations and definitions for literally thousands of terms from Web Development and related fields, building on Wikipedia as well as the MDN Web Docs. Available at Apple Books, Kobo, Google Play Books, and Leanpub.