Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass - Mixins which create dynamic property and its valuse

Tags:

css

sass

I'm trying to create a dynamic css property using SASS by using mixins.

@mixin setProperty($property,$value,$unit:null){

 #{$property} :$value$unit;

}
.class{
  @include setProperty(position,relative);
}

This creates an output

.class {
   position: relative;
}

I'm fine with this. But when i create some property for margin or padding we should include PX. So i tried something like this

.class{
  @include setProperty(margin,10,px);
 }

which creates output with a space in the middle as follows. How do i get rid of these space.

.class{
  margin: 10 px
}
like image 918
Santhosh Kumar Avatar asked Apr 20 '17 06:04

Santhosh Kumar


People also ask

Can SCSS variable change dynamically?

Sadly, there is not any way to "make Sass variables dynamic", because Sass itself is pre-compiled, while CSS is interpreted by the browser (dynamically) at runtime.

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.

What is the difference between a mixin and inheritance in Sass?

@mixin is used to group css code that has to be reused a no of times. Whereas the @extend is used in SASS to inherit(share) the properties from another css selector. @extend is most useful when the elements are almost same or identical. The main difference between the two is in their compiled CSS file.

Which argument are used to include arguments in the mixins?

Keyword ArgumentsExplicit keyword argument can be used to include in mixins. The arguments, which are named can be passed in any order and the default values of argument can be omitted.


1 Answers

You should use interpolation to concatenate the values instead of adding, you can try this:

@mixin setProperty($property,$value,$unit:null){

   #{$property} :#{$value}$unit;

}

When two distinct values are next to one another, Sass always adds a whitespace between them. With the interpolation it does not happen, Sass try to parse everything as an unquoted string.

like image 152
Manuel Avatar answered Nov 16 '22 04:11

Manuel