--- layout: post title: "The Importance of !important: Forcing Immutability in CSS" date: 2016-05-08 23:35:38 categories: Web Development meta: "Forcing immutability in our utility classes by using !important" --- There’s a piece of advice I give out to clients and workshop attendees that is often met with shock and horror: **I advise the use of `!important`.** For the most part, I’m glad that the initial reaction is disgust—`!important` usually _is_ bad news—however, there’s a little more to it than that… As with most things, there are exceptional circumstances in which following the rules would actually be a pretty bad idea, and it’s usually context and discretion that inform our decision to break them. For example, in the UK, the speed limit is 70mph (unfortunately we do still use miles). You are not allowed to drive faster than 70mph. It’s illegal. You just can’t do it. _However…_ if your friend is dying in the back seat and you’re trying to get them to hospital before they bleed out everywhere, by all means, drive faster than 70mph! In 90% of situations, the rule is a good one. By and large, we’re much better off following it than not following it. But there will definitely be situations that fall outside of that 90%. Similarly, as we mature as developers, we need to recognise that the rules we live by will also have exceptions, given the correct circumstances. With age (or time, I guess) comes wisdom, so with that it mind I would always tell junior developers not to use `!important` at all. But, once developers start to grow and learn that things aren’t always that black and white, we can get a little more detailed and nuanced, and throw in some caveats. But before we look at the exceptions to the rule, let’s look at the rule itself. ## Never Use `!important` in Anger Using `!important` reactively is the most heavy-handed, nuclear, [all-the-way-up-to-11](https://www.youtube.com/watch?v=uMSV4OteqBE&t=78s) option we have at our disposal. Using `!important` to get yourself out of a problem with some existing CSS is most certainly inadvisable. It will have knock-on effects whose only solution will be to use another `!important`, then another, then another, ad infinitum. Do not use `!important` reactively. Do not use `!important` to solve a specificity issue. Do not use `!important` in anger. ### Hacking Specificity If we do find ourselves in a situation where existing styles are overriding our current work, we have much safer methods of altering their specificity. If we need to bump the specificity of a single class up, we can chain it with itself (e.g. `.btn.btn {}`). If we need to bring the specificity of an ID down, we can rewrite it as an attribute selector (e.g. `[id="header"] {}`). You can read about that in much more detail in my [Hacks for Dealing with Specificity](/2014/07/hacks-for-dealing-with-specificity/) article. Most of the time, there is no need to turn to an `!important`. Right. When can we use it? ## Forcing Immutability with `!important` The idea of immutability is one that really fascinates and resonates with me. The idea that something can never be changed after it’s been created sounds amazing! How much more confidence would we have if we knew that something always looked and behaved the same no matter where we put it? I like that idea a lot! This is typically quite hard to achieve in CSS because it’s designed around an inheritance-based model which makes heavy use of mutations. But, there is a specific type of rule that can actually make great use of immutability, and do so very safely: utility classes. Utility classes are tiny little classes that carry out very specific, very explicit jobs. Classes like: ``` .u-text-center { text-align: center; } .u-float-left { float: left; } .u-text-large { font-size: 48px; } ``` They all begin with a `u-` in order to tell the next developer [what their purpose is](/2015/03/more-transparent-ui-code-with-namespaces/), and they all carry out just one simple piece of styling. All of the declarations in the rulesets above are defined without an `!important` on them, but they really, really ought to be. Here’s why: By using a class like `u-text-center` in our HTML, we are making a definite, clear, unambiguous decision that we want some text to be centrally aligned. There is absolutely no question about that. However, the selector `.u-text-center {}` has a relatively low specificity, so there’s a chance that another selector could accidentally override it. Take the following HTML and CSS: ``` .sub-content h2 { ... text-align: left; } .u-text-center { text-align: center; } ``` ```
``` Here we can clearly see a relationship between two distinct and separate strands of styling. Let’s say, however, that one specific testimonial needs to have a much larger `margin-bottom` applied to it. It’s not a treatment that all testimonials need, and it only needs the larger `margin-bottom` when it’s in this exact part of the page. This is a very implementation-specific design change. Here we’d reach for a utility class: ```![]()
...
``` We use a utility here because the extra `margin-bottom` doesn’t have anything to do with the testimonial specifically; it’s a very context-sensitive and temporary treatment, so we didn’t ought to hard code that change into our testimonial’s CSS. On the flip side. If we have a style of testimonial that has a larger font size, and any testimonial anywhere on the site could adopt this variation, we would not use a utility. This is a non-temporary treatment that belongs specifically to the testimonial, so we should codify and encapsulate it using a BEM Modifier: `````` So, as a general rule: **If it’s permanent styling, formalise it and code it right into your CSS. If it’s short-term or one-off styling, use a utility class.** Utility classes are probably my ‘least favourite’ type of class because they are really only one step away from inline styles. Use them sparingly, and to target very temporary or implementation-specific changes.