合并mixins中的选择器

ttj*_*ttj 2 css sass compass-sass

我试图在一个单独的mixin文件中包含一般样式/技巧,可以在需要时应用于任何项目.其中一些样式需要多个元素才能协同工作.

例如:

_mixins.scss
====================
@mixin footer_flush_bottom {
    html {
        height: 100%;
    }

    body {
        min-height: 100%;
        position: relative;
    }

    #footer {
        position: absolute;
        bottom: 0;
    }
}

main.scss
====================
@import "mixins";

@include footer_flush_bottom;

html {
    background-color: $bg;
    //More stuff
}

body {
    margin: 0 auto;
    //More stuff
}

footer.scss
====================
#footer {
    height: 40px;
}
Run Code Online (Sandbox Code Playgroud)

就这样,mixin可以工作,但是生成的css将mixin与主代码分开,即使它们的选择器是相同的.当我开始包含更多这些时,它的缺点是丑陋的CSS和更大的文件大小.

/* line 14, ../../sass/modules/_mixins.scss */
html {
  height: 100%; }

/* line 18, ../../sass/modules/_mixins.scss */
body {
  min-height: 100%;
  position: relative; }

/* line 22, ../sass/modules/_mixins.scss */
#footer {
  position: absolute;
  bottom: 0; }

/* line 19, ../../sass/modules/main.scss */
html {
  overflow-y: scroll; }

/* line 37, ../../sass/modules/main.scss */
body {
  margin: 0 auto;

/* line 1, ../sass/modules/footer.scss */
#footer {
  height: 40px;
Run Code Online (Sandbox Code Playgroud)

无论如何我能做到这一点,以便合并相同的选择器?像这样:

/* line 19, ../../sass/modules/main.scss */
html {
  height: 100%;
  overflow-y: scroll; }

/* line 37, ../../sass/modules/main.scss */
body {
  min-height: 100%;
  position: relative;
  margin: 0 auto;

/* line 1, ../sass/modules/footer.scss */
#footer {
  position: absolute;
  bottom: 0;
  height: 40px;}
Run Code Online (Sandbox Code Playgroud)

cim*_*non 5

没有.Sass无法合并选择器(这可能被认为是不合需要的,因为它会改变选择器的顺序).

你唯一能做的就是这样(或写2个独立的mixins):

@mixin footer_flush_bottom {
    height: 100%;

    body {
        min-height: 100%;
        position: relative;
        @content;
    }
}

html {
    // additional html styles
    @include footer_flush_bottom {
        // additional body styles
    }
}
Run Code Online (Sandbox Code Playgroud)