Sass中的媒体查询

Mac*_* S. 5 media sass responsive

我想知道是否有一种方法可以用sass编写媒体查询,因此我可以在例如300px至900px之间给出某种样式

在CSS中看起来像这样

@media only screen and (min-width: 300px) and (max-width: 900px){

}
Run Code Online (Sandbox Code Playgroud)

我知道我会写

@media (max-width: 900px)
Run Code Online (Sandbox Code Playgroud)

在无礼,但如何使范围?

小智 15

这就是我用于带有 sass 的 Mixin 的内容,它允许我快速引用我想要的断点。显然,您可以调整媒体查询列表以适应您的项目移动拳头等。

但是我相信它会为您提供多个查询。

$size__site_content_width: 1024px;

/* Media Queries */ Not necessarily correct, edit these at will 
$media_queries : (
    'mobile'    : "only screen and (max-width: 667px)",
    'tablet'    : "only screen and (min-width: 668px) and (max-width: $size__site_content_width)",
    'desktop'   : "only screen and (min-width: ($size__site_content_width + 1))",
    'retina2'   : "only screen and (-webkit-min-device-pixel-ratio: 2) and (min-resolution: 192dpi)",
    'retina3'   : "only screen and (-webkit-min-device-pixel-ratio: 3) and (min-resolution: 288dpi)",
    'landscape' : "screen and (orientation:landscape) ",    
    'portrait'  : "screen and (orientation:portrait) "
);

@mixin for_breakpoint($breakpoints) {
    $conditions : ();
    @each $breakpoint in $breakpoints {
        // If the key exists in the map
        $conditions: append(
            $conditions,
            #{inspect(map-get($media_queries, $breakpoint))},
            comma
        );
    }

    @media #{$conditions} {
        @content;
    }

}
Run Code Online (Sandbox Code Playgroud)

在你的 scss 中像这样使用它:

#masthead {
    background: white;
    border-bottom:1px solid #eee;
    height: 90px;
    padding: 0 20px;
    @include for_breakpoint(mobile desktop) {
        height:70px;
        position:fixed;
        width:100%;
        top:0;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后这将编译为:

#masthead { 
  background: white;
  border-bottom: 1px solid #eee;
  height: 90px;
  padding: 0 20px;
}

@media only screen and (max-width: 667px), only screen and (min-width: 1025px) {
  #masthead {
    height: 70px;
    position: fixed;
    width: 100%;
    top: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 从我的测试中,`mobile` 和其他键应该将它们的值封装在 `unquote("$string")` 中,例如;`unquote("only screen and (max-width: 667px)")`,因为 `@media` 查询不善待引号......或者删除引号,因为这似乎没有弹出构建错误。此外,我无法在定义 key:value pares 时使用 sass 数学,因此分配 `$_content_width_plus_one: $size__site_content_width + 1;` 并将其用于 `desktop` `min-width` 值可能更有效。除了这两点之外,这个答案很漂亮。 (2认同)

Joh*_*uez 8

看看这个 scss。 https://github.com/Necromancerx/media-queries-scss-mixins

用法

    .container {
      @include xs {
        background: blue;
      }

      @include gt-md {
        color: green
      }
    }
Run Code Online (Sandbox Code Playgroud)

演示Stackblitz

基于Angular FlexLayout MediaQueries


pgu*_*how 7

$small: 300px;
$medium: 900px;

.smth {
  //some CSS
  @media screen and (max-width: $small) {
    //do Smth
  }
  @media screen and (min-width: $medium) {
      //do Smth
  }
}
Run Code Online (Sandbox Code Playgroud)

像这样吗


Mic*_*ant 5

$small: 300px;
$medium: 900px;

@media screen and (min-width: $small) and (max-width: $medium) {
    //css code
}
Run Code Online (Sandbox Code Playgroud)