Combine extend and mixin in with same rules

浪子不回头ぞ 提交于 2019-11-30 10:03:09

问题


Okey! I have couple of extends in sass like

%heading
%paragraph
%gutter

and so on...

I want to reuse thouse in media queries, but that doesnt work. I know that.

Then i came up with the idea to have all my extends as mixins too. So when i want them in a media query i simply use mixin. for example

.my-widget {
    @extend %gutter;
    @media.... {
        @include gutter-other;
    }
}

and because i dont want to write all my rules again. How do i write my sass then?

I tried

%my-extend, @mixin my-extend {
   ...
}

but that didnt work.

Any ideas how to work with this?


回答1:


No, you can't combine them that way. You'll have to write a mixin that is invoked by your extend class and anything inside of a media query.

@mixin my-extend {
    background: yellow;
}

%my-extend {
    @include my-extend;
}

.foo {
    @extend %my-extend;
}

.bar {
    @extend %my-extend;
}

.baz {
    @media (min-width: 30em) {
        @include my-extend;
    }
}

Output:

.foo, .bar {
  background: yellow;
}

@media (min-width: 30em) {
  .baz {
    background: yellow;
  }
}


来源:https://stackoverflow.com/questions/16583004/combine-extend-and-mixin-in-with-same-rules

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