如何在sass中编写媒体查询?

时间:2016-10-04 06:25:28

标签: css sass frontend

我试图找出用sass编写媒体查询的方法。这是我的代码

devices: ("sphone","phone","tablet")
$breakpoints: (	width2: ('sphone' : 320, 'phone' : 360, 'tablet' : 600), height2: ('sphone' : 509, 'phone' : 583, 'tablet' : 952))

=screen-width($device)
  @each $name in $devices
    @if $device != $name
      @error "No device of such name"
  $media: (max-width: map-get(map-get($breakpoints,"width2"),$device) + px )
  @media screen only and ($media)
    @content
		
		
.hello
  background: #333
  @include screen-width("mobile")
    background: #444
    

我正在尝试在sass中编译相同但是它一直在向我抛出以下错误。我不知道为什么。

Properties are only allowed within rules, directives, mixin includes, or other properties.

任何见解?

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些错误,错过了$变量中的$devices。您编写@include screen-width("mobile")但移动设备不在设备列表中。在$media变量max-width中必须是字符串,然后您必须使用插值来添加@media规则。

@each循环中的比较是错误的,但我暂时没有解决这个问题。此代码有效:

$devices: ("sphone","phone","tablet")
$breakpoints: ( width2: ('sphone' : 320, 'phone' : 360, 'tablet' : 600), height2: ('sphone' : 509, 'phone' : 583, 'tablet' : 952))

=screen-width($device)
  $media: "max-width:" map-get(map-get($breakpoints,"width2"),$device) + px
  @media screen only and (#{$media})
    @content

.hello
  background: #333
  @include screen-width("sphone")
    background: #444