Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding `!important` to a mixin

Tags:

css

sass

I want to add !important to a mixin. I tried both the following ways, and they return an error:

@include linear-gradient(hsl(68%, 94%, 90%), hsl(68%, 90%, 80%)); !important

@include linear-gradient(hsl(68%, 94%, 90%), hsl(68%, 90%, 80%)) !important;

Is there a way to do this correctly?

like image 432
sawa Avatar asked Jan 21 '13 08:01

sawa


People also ask

What is the purpose of using mixin in Sass?

Sass Mixins The @mixin directive lets you create CSS code that is to be reused throughout the website. The @include directive is created to let you use (include) the mixin.

How do you pass arguments to mixin?

Variable argument is used to pass any number of arguments to mixin. It contains keyword arguments passed to the function or mixin. Keyword arguments passed to the mixin can be accessed using keywords($args) function which return values mapped to String.

What is the use of mixin?

Mixins are a language concept that allows a programmer to inject some code into a class. Mixin programming is a style of software development, in which units of functionality are created in a class and then mixed in with other classes. A mixin class acts as the parent class, containing the desired functionality.

Which arguments are used to include arguments in the mixin?

Keyword arguments: The arguments are used to include in mixins. These types of arguments, if named, can be passed in any order and their default values can be skipped.


2 Answers

For some situations, I use a optional parameter named $important which I can pass in true.

Example:

my-mixin($important: true);

It would look something like that, with a helper function to avoid repetition on the properties that I have to toggle important:

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

@mixin my-mixin($important: false) {
  border-radius: 0; //whatever
  border: 1px solid #ccc if-important($important);
  background-color: #fff if-important($important);
  color: #000 if-important($important);
}
like image 154
Reuel Ribeiro Avatar answered Sep 21 '22 05:09

Reuel Ribeiro


!important cannot be used in a mixin. Refer the following links.

Adding !important using a Compass Mixin

https://github.com/cloudhead/less.js/issues/547

):

like image 22
Rajesh Omanakuttan Avatar answered Sep 24 '22 05:09

Rajesh Omanakuttan