views:

70

answers:

3

Sometimes !important is useful, for example if I have defined common style for all links on the site (selector, say, a), but when I want to override some rules I have the following choices:

  • Use more specific (longer) selector
  • Use !important

Which way is better and may be there are some guidelines?

A: 

What i can say is that you should try to avoid it as much as you can because it means that there are redundant code, and give an indication that the CSS and styles are not well structured.

But in some cases its a must to use it and no way to avoid.

So just do your best.

Amr ElGarhy
A: 

Avoid it when you can as this is what most advocate but at the same time it is important.

Have a look at:

The importance of !important in CSS

Sarfraz
+2  A: 

Use !important very, VERY sparingly -- it overrides just about everything, including even the user's custom style sheet if there is one. If he has stuff set to really-really-big because he can't see well, and you've decided it's so !important that your words be exactly 10 pixels high because.... well.... arrogance would be the only reason i can come up with. Anyway, now the user can't read anything on your site.

Aside from that, if you use it everywhere, then you end up in the same situations you'd be in without it (difficulty in specifying elements specifically enough to override other styles), but you also don't have !important anymore cause everything else is using it too.

cHao