Hacker News new | past | comments | ask | show | jobs | submit login

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. 
See here for an example: http://codepen.io/gkey/pen/ONevEe

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.


Just curious: what happens if they start to use important, too?


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.


Sometimes I wish you could chain multiple `!important`s, then I realize the horrible idea I just had...


I disagree on !important. Good use of !important in high level css is outlined pretty nicely in this article: http://csswizardry.com/2016/05/the-importance-of-important/


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!!!!!


Your comment is one of the best I have ever read, thank you :D !


>>Whenever I see code with !important I instantly know that guy was lazy, unexperienced, wrong guided

Your narrow mindedness on the scope of issues that could be at play makes me doubt your abilities.

[EDIT] I am slightly jesting by reflecting your comment judging others based on few factors


!important is the only way to force a CSS rule to be more specific than an inline style.

As such, it has legitimates uses in rare cases (a responsive websites with some javascript style changes specific to a breakpoint per exemple.)

I would personally say that what divides good and bad guys is opinionated though.


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.


Surely, you mean:

    .buffalo.buffalo.buffalo
yes?


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.


TIL! Thanks!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: