【问题标题】:Sass : overwrite definitions with different placeholder in @media querySass:在@media 查询中使用不同的占位符覆盖定义
【发布时间】:2013-09-22 21:14:31
【问题描述】:

我有以下 scss:

@mixin logo($size:mobile) {

  @if $size == mobile {

    #logo {

      @extend %logoMobile;

      a {

        @extend %logoMobile;

      }

    }

  }
  @else {

    #logo {

      @extend %logoDesktop;

      a {

        @extend %logoDesktop;

      }

    }

  }

}

%fullWidth {
  width: 100%;
}

%logoMobile {
  @extend %fullWidth;
  height: 30px;
}

%logoDesktop {
  width: 211px;
  height: 35px;
}


#header {

  @include logo('mobile');

}

@media only screen and (min-width: 768px) {

  #header {

    @include logo('desktop');

  }

}

其中生成的css输出为:

#header #logo, #header #logo a {
  width: 100%;
}

#header #logo, #header #logo a {
  height: 30px;
}

知道为什么没有生成@media 查询吗?

【问题讨论】:

  • 为什么会生成媒体查询?
  • 什么意思?它在 scss 文件中 - 因此您希望它在输出中生成 - 对吧?

标签: css sass media-queries


【解决方案1】:

不允许从存在于媒体查询之外的媒体查询中扩展类。当你编译 Sass 文件时,控制台会告诉你:

DEPRECATION WARNING on line 12 of test.scss:
  @extending an outer selector from within @media is deprecated.
  You may only @extend selectors within the same directive.
  This will be an error in Sass 3.3.
  It can only work once @extend is supported natively in the browser.

DEPRECATION WARNING on line 14 of test.scss:
  @extending an outer selector from within @media is deprecated.
  You may only @extend selectors within the same directive.
  This will be an error in Sass 3.3.
  It can only work once @extend is supported natively in the browser.

除非您实际上是在重复使用您的徽标样式,否则这非常复杂。过度使用 extend 会导致 CSS 文件变大而不是变小,因此请谨慎使用。如果您必须使用媒体查询进行扩展,可以这样做:

%logo {
    &, & a {
        width: 100%;
        height: 30px;
    }

    @media only screen and (min-width: 768px) {
        &, & a {
            width: 211px;
            height: 35px;
        }
    }
}

#header {
    #logo {
        @extend %logo;
    }
}

输出:

#header #logo, #header #logo a {
  width: 100%;
  height: 30px; }
@media only screen and (min-width: 768px) {
  #header #logo, #header #logo a {
    width: 211px;
    height: 35px; } }

【讨论】:

  • 当媒体查询在 mixin 中定义时,此解决方案似乎不起作用。然后找不到扩展类。显然不能在扩展类中使用包含函数。
猜你喜欢
  • 2013-07-11
  • 2018-05-02
  • 2017-02-16
  • 2011-08-02
  • 2021-11-27
  • 1970-01-01
  • 2022-08-16
  • 1970-01-01
  • 2013-11-04
相关资源
最近更新 更多