Great article. It should emphasize the part about !important even more.
!important should be banned. !important divides the good guys from the bad guys. Whenever I see code with !important I instantly know that guy was lazy, unexperienced, wrong guided - you name it. And it also makes my life hard if I have to continue to work on that same css.
I also miss one of the most useful methods to be more granular with the cascading. To apply a new rule, instead of using a fresh new class or to hammer into some new style definitions with an id/!important just use the class twice in your css. More of the same class means more important.
.button.button.button outranks .button.button which outranks .button.
It's not beautiful and if you're doing css right you won't need it most of the time. But try this before using the big bad boys like an #ID or the !important statement.
>> just use the class twice in your css. More of the same class means more important
This is even crazier than using an "!important" once in a while. You're creating obfuscated CSS that technically respects the concept of cascades and specificity. Yet your reason for doing this is essentially just to avoid "!important", while not actually fixing your cascading/specificity problem. I'd rather see the rare "!important" as a visible warning sign that something was lazily patched, rather than a poor workaround that is invisible.
This kind of solution sounds a lot like I get in trouble for using "!important", so I'll use an alternative dirty hack that is hidden well enough that I probably won't get caught.
No way. You are not considering the places where the developer doesn't have access to all the CSS that is happening on the page. !important is a way to override styles you don't have access to. For example, I've integrated plenty of third party social media widgets in micro-sites, and they often have the capacity to customise the design by adding new CSS, not replacing the untouchable CSS.
Various other scenarios similar to above is not uncommon for frontend developers to find themselves dealing with in the workplace.
Well of course it's not cool when that happens, and while generally most people agree !important is to be avoided, I was responding to the idea it should be banned completely, or the article where it says it's only good for debugging. It's a last resort that needs to be there, and has probably saved a lot more time than it's wasted.
From that article:
"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 [avoid !important] 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%."
Sooooo, I guess 10% of the time he's driving dying people to a hospital. DON'T TAKE DRIVING ADVICE FROM AMBULANCES!!!!!
Wow, I have to admit I got here to the very wrong guys.
When you're already reflecting about !important, well then you're happy to use it, because you think and know about all the consequences. HN readers reflect a lot. So you're clearly the wrong audience.
I'm sorry for my harsh 'lazy and not so experienced' expression. But sometimes you have to exaggerate to be heard.
Those developers I'm talking about just don't think about the consequences. !important is not a tool for them, it's their last resort to fix something. And then? Well it's a dead end and the rise of all the problems with !important we are talking about. If they would know about chaining, they would have used chaining before using !important. It would be a mess too. Yes. But at least it's not a dead end for them and for any other developer who have to contribute to the same codebase.
I do a lot of WordPress programming. You'd be surprised at the number of WordPress plugin developers who use inline styles. !important is pretty much the only way to override these inline styles, aside from duplicating a plugin (which presents it's own set of problems).
Being old enough to remember the Bill and Ted movies, this makes me want to write a website where there's just varying numbers of a ".station" class on elements.
!important should be banned. !important divides the good guys from the bad guys. Whenever I see code with !important I instantly know that guy was lazy, unexperienced, wrong guided - you name it. And it also makes my life hard if I have to continue to work on that same css.
I also miss one of the most useful methods to be more granular with the cascading. To apply a new rule, instead of using a fresh new class or to hammer into some new style definitions with an id/!important just use the class twice in your css. More of the same class means more important.
See here for an example: http://codepen.io/gkey/pen/ONevEeIt's not beautiful and if you're doing css right you won't need it most of the time. But try this before using the big bad boys like an #ID or the !important statement.