Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use !important keyword in a mixin?

Tags:

sass

I can't get Sass to output the !important keyword with a mixin, I've tried:

@include font-size($font-size-sml) !important;

And:

@include font-size($font-size-sml !important);

It always throws an error.

EDIT

I ended up with this which works fine:

@mixin font-size($font-size, $sledge-hammer: "") {
    font-size: $font-size #{$sledge-hammer};
    font-size: ($font-size / $base-font-size)+rem #{$sledge-hammer};
    line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
}
like image 626
Chris Pearce Avatar asked Jan 02 '13 07:01

Chris Pearce


People also ask

Can we use mixin inside mixin?

As mentioned in the other answers, you can include mixins in other mixins. In addition, you can scope your mixins. Scoping mixins means that you can have multiple mixins named the same in different scopes without conflicts arising.


1 Answers

You can't add !important to whole mixin in SASS (It is possible in LESS I think) like you're trying to do in first example.

Second example works for me (you can pass !important with a parameter), I mean, if you use $font-size-sml directly as a property value it works, so maybe check your syntax.

But if it's really not working for you, you can do something with flag, set a important_flag as a mixin parameter and then use if-else statement in mixin. Something like this:

@mixin large-text($prop, $is_imp: false) {
    @if $is_imp == false {
        font-size: $prop;
    } @else {
        font-size: $prop !important;
    }
}

Maybe it's not a glamorous way to do it, but it works ;-)

like image 190
Adam Wolski Avatar answered Nov 23 '22 20:11

Adam Wolski