-5

In Bootstrap tutorials, when people create their own Sass variables in a custom style.scss, I see lots of them using trailing !important.

If you want to see an example of what I mean, start at 13:45 here: https://www.youtube.com/watch?v=MDSkqQft92o&time_continue=24&app=desktop

.navbar {
  width: 100%;
  background none !important;

  @media(max-width: 34em) {
    background: black !important; 
  }
}

But then other code in the .scss file doesn't use !important. No explanation of why that is.

Anyone have some suggestions?

4thSpace
  • 43,672
  • 97
  • 296
  • 475

2 Answers2

2

!important means that the style preceding it will be "forced" onto the element, despite what other styles may set on it. For example:

.element {
  font-weight: bold;
}
.custom-class {
  font-weight: normal !important;
}

If you have an <span class="custom-class element"></span>, it will be displaying a font-weight normal, not bold.

Carlos Moreira
  • 321
  • 1
  • 11
1

! important guarantees (or at least it tries) that the rule you're trying to apply is more important than others. So it overwrites other rules that might interfere with the new one. ! important is not necessary if the rules are ordered correctly because the last rules prevail over previous ones. ! important must be avoided as much as possible. Use it only if it's really necessary.

Peristilo peris
  • 105
  • 1
  • 7