Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing css property:value as sass mixin argument

Tags:

css

sass

Is something like the following dummyexample possible in sass/scss? I need something like this to prevent the endless mediaquery repeats for different devices.

// The dummy mixin
@mixin iphone_rules($webapp_portrait:null){


  @if($webapp_portrait != null){
    // Portrait (webapp)
    @media screen and (max-height: 460px){

     // The following line is just a dummy 
     eg. echo $webapp_portrait;
    }
  }
}

// How I want to use it
.mySelector{

   margin-left:10px;
   padding:0px;

   @include iphone_rules('margin-left:20px; padding:2px;');
}
like image 411
Bernhard Avatar asked Jan 21 '13 21:01

Bernhard


People also ask

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.

Which arguments are used to include arguments in the mixins in SASS?

The keyword arguments are used to include in mixins. It specifies that the named arguments can be passed in any order and the default value of arguments can be omitted.

Can I use mixin in CSS?

CSS mixins​Any CSS stylesheet, class or element that is defined in a Stylable CSS file can be used as a mixin source. You can use either a local class or element, or import the mixin from a different stylesheet. In the following example, a locally defined class is used as a mixin in the same stylesheet.


1 Answers

Sass does not allow the use of arbitrary strings in place of the property/value syntax.

For most mixins, the @content directive is your best bet for passing in styling information:

@mixin iphone_rules {
    @media screen and (max-height: 460px){
        @content;
    }
}

.mySelector {
    @include iphone_rules {
        margin-left:10px;
        padding:0px;
    }
}

Otherwise, styling information can be passed in as a mapping (or list of lists for Sass 3.2 and older):

@mixin iphone_rules($styles: ()) {
    @media screen and (max-height: 460px){
        @each $p, $v in $styles {
            #{$p}: $v;
        }
    }
}

.mySelector {
    margin-left:10px;
    padding:0px;

    @include iphone_rules(('margin-left': 20px, 'padding': 2px));
}
like image 171
cimmanon Avatar answered Oct 10 '22 08:10

cimmanon