CSS property as SASS mixin value [duplicate]

谁说我不能喝 提交于 2019-11-26 06:49:59

问题


I try to build some universal margin/padding mixin...

This is my code:

[class*=\"shift\"] {
  $sft-o: 10px;
  @mixin shift_stp($val) {
    &[class*=\"_sml\"]{ $val: $sft-o; }
    &[class*=\"_mid\"]{ $val: $sft-o * 2; }
    &[class*=\"_big\"]{ $val: $sft-o * 3; }
  }
  &[class*=\"_m\"]{
    @include shift_stp(margin);
  }
  &[class*=\"_p\"]{
    @include shift_stp(padding);
  }
}

Something is not right, so I wonder if it is possible to set some CSS property as a mixin value? Can anybody help?


回答1:


If you want to use variables as property names you need to use string interpolation - #{$var}.

So this should work:

[class*="shift"] {
  $sft-o: 10px;
  @mixin shift_stp($val) {
    &[class*="_sml"]{ #{$val}: $sft-o; }
    &[class*="_mid"]{ #{$val}: $sft-o * 2; }
    &[class*="_big"]{ #{$val}: $sft-o * 3; }
  }
  &[class*="_m"]{
    @include shift_stp(margin);
  }
  &[class*="_p"]{
    @include shift_stp(padding);
  }
}

DEMO

Just a note: for your attribute selectors ... *="_m" will also apply to the ones that have _mid in them (see here) ... so maybe you should rethink this a little.



来源:https://stackoverflow.com/questions/20050932/css-property-as-sass-mixin-value

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