Sass - Mixins which create dynamic property and its valuse

女生的网名这么多〃 提交于 2021-02-08 14:09:10

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/43512321/sass-mixins-which-create-dynamic-property-and-its-valuse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!