Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS A discussion about !important

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #42632

    As we all know, !important is looked down upon like some kind of ungodly, unspeakable abomination.

    But is it really all that bad?

    In the site I’m working on currently, I’ve found that sometimes, using !important saves code. I don’t use it to fix specificity issues that I don’t understand—rather, I know why there is a specificity problem, and I’m using !important to rectify it in the most efficient way.

    I’m not talking about using it frivolously, just when you know for a fact that you ALWAYS want a value to be true.

    The example that inspired me to post this thread:

    I have a search box that is given a column class to control its width. The search box div has 16px of padding in order to show its background. It should always have this padding.

    Meanwhile, I have a media query that, at a certain screen width, gets rid of the padding on columns that are direct children of the main wrapper, provided they’re not of one specific class, ‘main-posts’. Like so:

    @media screen and ( max-width: 700px ) {

    .main-grid-module, .content-wrap > [class*=’col-‘]:not(.main-posts) {
    width: 100%;
    float: none;
    padding: 0
    }
    Now, since my search box is also a column, this gets rid of its padding.

    I could combat it with another rule:

    .content-wrap > [class*=’col=’].search

    But that seems like an extremely unnecessary amount of code for something that !important can accomplish.

    .search {
    padding: 16px !important
    }

    Again, I always want this search box to have 16px of padding. So is it really that bad to use !important to make sure of it?

    Any thoughts would be appreciated.

    **EDIT: Since posting this I’ve simplified that selector a lot…seems as if I momentarily forgot to take advantage of the cascade. But the question remains valid. Is it excusable to use !important if a) you know you never want a value to change, and b) it simplifies the code?**

    #124026
    Kitty Giraudel
    Participant

    I use it time to time as well when I want a value to be always the same no matter what. No big deal if you know what you are doing.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘CSS’ is closed to new topics and replies.