50

If I'm using compass for CSS and use a function or mixin like:

   @include background-image(linear-gradient(#a3cce0, #fff)); 

is there any easy way to have compass add !important to every line it generates?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
David
  • 2,834
  • 6
  • 26
  • 31
  • Unfortunately, that's not possible with Compass. There is no direct fix for this, but you could copy the mixin internals and add `!important` somewhere yourself. It's not ideal, but there is no other option for it yet. – Luwe Aug 22 '11 at 11:41

6 Answers6

88

You can include it inside the mixin like so:

@include border-radius(5px !important);

Compass will output the following:

-webkit-border-radius: 5px !important;
-moz-border-radius: 5px !important;
-ms-border-radius: 5px !important;
-o-border-radius: 5px !important;
border-radius: 5px !important;
naoufal
  • 1,181
  • 1
  • 8
  • 8
36

UPDATE: new versions of sass support this syntax now:

@include border-radius(5px !important);

Just do this (as noted in @naoufal answer).

--- old answer ---

You can not use !important with compass mixings, but the culprit is not compass, you should blame sass for this.

@include border-radius(5px) !important; #=> SASS Syntax Error
tothemario
  • 5,851
  • 3
  • 44
  • 39
  • 4
    Shouldn't it be `@include border-radius(5px !important);` which also does not work. The reason for this would be because the `5px` will show up as the value of each rule, so the !important needs to also show up next to the value of each rule. – ProLoser Jul 30 '12 at 22:35
  • 13
    @ProLoser I just tried `@include border-radius(5px !important);` and it works fine. Maybe they've fixed this since your comment though. Down-vote this answer (though it may have been right at the time). – Graeck Jul 16 '13 at 21:30
10

Actually you can use a @function to handle the !important while keeping the flexibility of the mixing itself. For example:

@function is-important($important){
  @return #{if($important, '!important', '')};
}

// by default we don't want the !important at the end
@mixin button-primary($important: false) {
  font-size: 14px;
  background: #fff is-important($important);
  color: #000 is-important($important);
}

Hope it helps!

WSD
  • 3,243
  • 26
  • 38
7

Just spent hours figuring this out but there is a quick trick you can do. At the top of your SASS file add the following:

$i: unquote("!important");

in your style do the following:

color: #CCCCCC $i;

output is:

color: #CCCCCC !important;

full sample:

$i: unquote("!important");

.some-style {
    color: white $i;
}

output:

.some-style {
    color: white !important;
}
Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
John Angelini
  • 71
  • 1
  • 2
4

This question came up in my search for a similar problem, it's spot on but I just wanted to add that Making a Sass mixin with optional arguments was another possible approach that I found useful.

Replace inset with important and pass !important in when you need it.

Community
  • 1
  • 1
Shane Maiolo
  • 171
  • 4
  • You get the +1 for helping me get rid of the errors in my IDE (webstorm). It works without the quotes as mentioned by naoufal above, but I hate having the "errors" in my project. I'm gonna submit a bug report to JetBrains for this. – kahmali Oct 20 '14 at 09:33
3

I had this problem last time and I overrided the compass style with a stronger selector. I just added an ID on my html element

span { @include border-radius(5px);}

span#no-radius { @include border-radius(0px); } // override
Bo Persson
  • 90,663
  • 31
  • 146
  • 203