【问题标题】:How can SASS help me develop responsive web designs?SASS 如何帮助我开发响应式网页设计?
【发布时间】:2014-06-15 02:50:02
【问题描述】:

我多年来一直使用 CSS 进行设计,但我现在才刚刚学习如何使用 SASS。这是一个非常初学者的问题,所以请多多包涵。

我开始研究 SASS 的原因是因为我想开发一个响应式网页设计,但希望有一种比手动为每种屏幕尺寸制作不同样式表更好的方法。

我相信,原则上,像 SASS 这样的 CSS 预处理器应该可以帮助解决这个问题......但目前我不明白如何。

如果我有一个 id 为 #squishable 的 div,并且在大屏幕上我希望它的宽度为 1000 像素,在小屏幕上的宽度为 100 像素,但在这两种情况下都是红色背景,我想我会这样做:

$color = red;

#squishable {
    backgorundcolor: $color;
}

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

    #squishable {
        width:100px;
    }
}

@media only screen
and (min-width : 1824px) {
    #squishable {
        width:1000px;
    }
}

但是,我不确定这种方法是否比仅使用 CSS 更有优势。也许作为一个例子,它太简单了。但是,无论如何,我并没有确切地看到我会用 SASS 做些什么来让响应式网页设计的代码变得更容易。这就是它的全部吗?

谁能帮我克服这个概念上的障碍,让我知道我如何利用 SASS 来创建响应式元素?希望有一个类似于我上面的简单示例?

【问题讨论】:

    标签: css sass


    【解决方案1】:

    您绝对可以进一步改进您的工作流程!

    使用 SASS @mixin、@include、@content 和常规 variables,您可以设置一个环境,在其中创建“内联”媒体查询,或者如果您愿意,可以在常规选择器内部创建。


    这是我的媒体查询 mixin:

    (别在意愚蠢的变量名和占位符断点)

    // Breakpoints
    $mq-tiny:   5em;
    $mq-small:  10em;
    $mq-medium: 15em;
    $mq-large:  20em;
    $mq-huge:   25em;
    $mq-crazy:  30em;
    
    @mixin mq($size, $direction: min-width, $media-type: only all) {
      @if      $size == tiny   { @media #{$media-type} and (#{$direction}: $mq-tiny)   { @content; } }
      @else if $size == small  { @media #{$media-type} and (#{$direction}: $mq-small)  { @content; } }
      @else if $size == medium { @media #{$media-type} and (#{$direction}: $mq-medium) { @content; } }
      @else if $size == large  { @media #{$media-type} and (#{$direction}: $mq-large)  { @content; } }
      @else if $size == huge   { @media #{$media-type} and (#{$direction}: $mq-huge)   { @content; } }
      @else if $size == crazy  { @media #{$media-type} and (#{$direction}: $mq-crazy)  { @content; } }
      @else                    { @media #{$media-type} and (#{$direction}: $size)      { @content; } }
    }
    

    下面是一个如何使用它的示例:

    SCSS:

    .selector {
      width: 100px;
    
      @include mq(large) {
        width: 1000px;
      }
    }
    

    CSS 输出:

    .selector {
      width: 100px
    }
    
    @media only all and (min-width: 20em) {
      .selector {
        width: 1000px
      }
    }
    

    请注意,您不需要为宽度使用变量名。如果你愿意,你可以传递 1000px 而不是 large。

    您可能还注意到@mixin 中的可选参数; $direction 和 $media-type。这些参数默认分别为 min-width 和 only all,但如果您通过 @include 传递它们,它们将仅针对该特定元素进行更改。

    其他参数示例:

    SCSS:

    .selector {
      @include mq(1000px, min-height) {
        width: 100px;
      }
    }
    

    CSS 输出:

    @media only all and (min-height: 1000px) {
      .selector {
        width: 100px
      }
    }
    

    希望这会有所帮助!

    编辑: Here's a pen 如果你想玩它。

    【讨论】:

    • 不错的答案,看起来我可能会尝试一下,即使我是 CSS 的“硬编码者”。
    【解决方案2】:

    为了提高代码的可维护性,您可以使用变量来定义媒体查询断点:

    // define breakpoints
    $small-screen: 321px;
    $large-screen: 1824px;
    
    $small: "only screen and (min-width:"#{$small-screen}")";
    $large: "only screen and (min-width:"#{$large-screen}")";
    // and so on...
    
    
    // now you can esely manipulate with your media breakpoints
    @media #{$small} {
        ...
    }
    
    @media #{$large} {
        ...
    }
    

    【讨论】:

      【解决方案3】:

      强烈建议您开发您的 SAAS css 样式导入引导框架,它将为您节省大量响应式网页设计的时间

      更多详细信息请查看此链接: http://pivotallabs.com/sass-with-bootstrap/

      【讨论】:

      • 感谢您的建议。我将发布一个包含更详细信息的链接
      猜你喜欢
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多