【问题标题】:create custom breakpoint for media query为媒体查询创建自定义断点
【发布时间】:2019-08-17 22:10:15
【问题描述】:

我正在尝试在我的 Angular 应用程序中为媒体查询创建一些常见的断点。我正在使用scss。我到处重复这 3 行,我想改进它:

@media screen and (orientation: landscape) and (max-width: 959px), 
  screen and (orientation: portrait) and (max-width: 599px) {

@media screen and (orientation: landscape) and (max-width: 959px) {

@media screen and (orientation: portrait) and (max-width: 599px) {

我想创建一些变量来包含它,但我没有得到它,它应该是这样的:

$mobile = "screen and (orientation: landscape) and (max-width: 959px), 
  screen and (orientation: portrait) and (max-width: 599px)"
$landscape_mobile = "screen and (orientation: landscape) and (max-width: 959px)"
$portrait_mobile = "screen and (orientation: portrait) and (max-width: 599px)"

有可能吗?

【问题讨论】:

    标签: css sass media-queries


    【解决方案1】:

    变量是使用: 定义的,而不是=。

    你可以这样使用它们:

    $mobile: "screen and (orientation: landscape) and (max-width: 959px), 
      screen and (orientation: portrait) and (max-width: 599px)";
    $landscape_mobile: "screen and (orientation: landscape) and (max-width: 959px)";
    $portrait_mobile: "screen and (orientation: portrait) and (max-width: 599px)";
    
    @media #{$mobile} { ... }
    @media #{$landscape_mobile} { ... }
    @media #{$portrait_mobile} { ... }
    

    注意插值语法#{}。
    More about Sass interpolation

    【讨论】:

      【解决方案2】:

      您可以创建一个 mixin 并随时使用它

      @mixin breakpoint($point) {
        @if $point == desktop {
          @media (min-width: 1400px) { @content; }
        }
      }
      
      // Use like this
      div {
        @include breakpoint(desktop) {
          display: none;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-23
        • 2014-09-27
        • 2017-04-12
        • 1970-01-01
        • 2013-11-04
        • 2015-11-06
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        相关资源
        最近更新 更多