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

标签 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.

有什么见解吗?

最佳答案

您的代码中有一些错误,缺少 $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

关于css - 如何在 sass 中编写媒体查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39845470/

相关文章:

html - 显示 : flex and unresponsive photos HTML + CSS

html - 如何设置输入文本的背景颜色?

javascript - gulp - 从多个目录编译 gulp-sass

mobile - ionic 2/ ionic 3/ ionic 4 : (Lazy) Loading spinner for pictures

css - 如何停止 compass 将 rgba 格式转换为十六进制格式

javascript - 为什么我在 Element 中单击脚本不会转到 chrome beta 40 开发工具中的源面板

javascript - Angular 无法读取未定义的属性 'Version'

被 WebResource.AXD 覆盖的 CSS 属性

javascript - 当光标位于它前面的位置绝对元素上时,能够在底层元素内滚动

javascript - 在表格单元格 HTML 中隐藏/显示长文本