在变量中保存媒体查询

时间:2012-03-22 20:01:13

标签: variables media-queries sass

是否可以将媒体查询保存为变量?

这不起作用:

$max: @media (max-width: 980px) and (min-width: 768px);

我不是在寻找如何保存最大宽度和最小宽度,而是整个字符串。

2 个答案:

答案 0 :(得分:7)

使用类似的东西

  @mixin respondTo($media) {
    @if $media == smallScreen {
      @media only screen and (max-width: $screenSmall - 1) { @content; }
    } @else if $media == mediumScreen {
      @media only screen and (max-width: $screenMedium) and (min-width: $screenSmall) { @content; }
    } @else if $media == largeScreen {
      @media only screen and (min-width: $screenXlarge) { @content; }
    }
  }

然后您可以执行以下操作:

.products {
  @include respondTo(smallScreen) {
    width: 300px;
  }

  @include respondTo(mediumScreen) {
    width: 700px;
  }
}

答案 1 :(得分:3)

从Sass 3.2开始,您可以将媒体查询存储在如下变量中:

$bp-small: "(min-width: 30em)";

@media #{$bp-small} {
    .foo {
        color: red;
    }
}