gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:05:32 27 4
gpt4 key购买 nike

我试图找出用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com