css - 在 Sass @for 循环中使用 css3 媒体查询

标签 css media-queries sass

可能吗?

例如:

@for $i from 1 through 12 {
  @media screen and (min-width: #{$i * 240}) { 
    width: {$i * 240 + (20*($i-1)) } ;
  }
}

但我不断得到

Invalid CSS after "...nd (min-width: ": expected expression (e.g. 1px, bold), was "#{$i * 240}) { ..."

最佳答案

您只能在 Sass 3.2 及更高版本的媒体查询中使用变量。您可以通过在终端中运行以下命令来找到您正在使用的版本:

gem list | grep sass

您可以通过运行以下命令来安装 3.2 alpha:

(sudo) gem install sass --pre

我假设您也希望使用像素,因此一旦安装了 3.2+,您就可以将代码清理为:

@for $i from 1 through 12 {
  @media screen and (min-width: $i * 240px) { 
    width: $i * 240px + (20*($i - 1));
  }
}

注意乘以240px,而不是仅仅240,并且在$i - 1之间添加空格

关于css - 在 Sass @for 循环中使用 css3 媒体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353385/

相关文章:

html - 将文本路径定位到包含圆 SVG 的中心

html - 来自 float :left and right to under eachother

html - 图像高度与宽度相同

sass - 使 Material 表列动态宽度到内容

javascript - 为什么 Webgl 3D 无法在 Chrome 中运行?

CSS3 没有过渡出来

javascript - JQuery使用正则表达式删除contenteditable div中的多个<p>

javascript - 控制通过 javascript 应用哪些 css 媒体查询

css - 如何让媒体查询识别横向?

asp.net - 将 SASS 与 ASP.NET 结合使用