how to translate similar codes to a function in compass sass function?

前端 未结 2 1589
北海茫月
北海茫月 2020-12-12 05:45

I want to add some common vars in my scss file, such as mgb10 for margin-bottom:10px; pdl20 for padding-left:20px;
my method is very stupid. how to improve my code usin

2条回答
  •  隐瞒了意图╮
    2020-12-12 06:17

    Here is a Sass mixin (you can change it to Scss) that uses the @for directive to generate all the placeholder selectors:

    =shorthander($property: margin, $shorthand: mg)
      @for $i from 0 through 4
        $multiple: $i*5
        %#{$shorthand}#{$multiple}
          #{$property}: $multiple*1px
    
    +shorthander()
    +shorthander(margin-top, mgt)
    +shorthander(margin-right, mgr)
    +shorthander(margin-bottom, mgb)
    +shorthander(margin-left, mgl)
    
    div
      @extend %mgt20
    

    Demo

提交回复
热议问题