csstwitter-bootstrapzurb-foundationcss-frameworkscascade-framework

Why do CSS Frameworks use !important tags unnecessarily?


This is more of a debate than a question but I feel that there isn't a lot on the internet that covers this topic.

For example foundation comes with hundreds of !important tags for things that in my eyes do not need them:

.text-center { text-align: center !important; } 

There is loads of css that is simular to this which in my point of view is bad practise and the question I'd like to answer is why do css frameworks use them at all? Bootstrap and Foundation are two main css frameworks that both use them.

I've always been told that using important tags in css is very bad practise and should only be used for IE.


Solution

  • If you write your own CSS you have the freedom to add more specific rules whenever needed:

    .center        { text-align: center; }
    .foobar        { text-align: left; }
    .foobar.center { text-align: center; }
    

    However, the CSS framework cannot predict how you will arrange your HTML. So it is easier to do !important instead of generating thousands of combinations of more specific rules. Example:

    .center               { text-align: center; }
    .foobar               { text-align: left; }
    .barbaz               { text-align: right; }
     /*
      * assuming .center must be centered regardless of other rules and
      * !important must be avoided at the same time, we need to do this
      */
    .foobar.center        { text-align: center; }
    .barbaz.center        { text-align: center; }
    .foobar.barbaz.center { text-align: center; }