【问题标题】:sass interpolation in selector doesn't work选择器中的 sass 插值不起作用
【发布时间】:2016-07-07 07:33:54
【问题描述】:

我想只用 sass 为 safari 浏览器创建一个规则。根据this 回答我需要

@media screen and (min-color-index:0) and(-webkit-min-device-pixel-ratio:0)

注意第二个后面没有空格,否则会触发chrome。 所以在my-file.sass 的某个地方我做了以下事情:

$safari-only: '(min-color-index:0) and(-webkit-min-device-pixel-ratio:0)'

@media screen and #{$safari-only}
    body
        background-image: linear-gradient(green 0%, red 100%) !important

但是这东西编译成

@media screen and (min-color-index: 0) and (-webkit-min-device-pixel-ratio: 0) {
  body {
    background-image: linear-gradient(green 0%, red 100%) !important;
  }
}

注意,在第二个之后IS有一个空格。 这是demo

另一件事我想知道我是否可以创建一个@safari-only mixin。例如:

@mixing safari-only
  $safari-only: '(min-color-index:0) and(-webkit-min-device-pixel-ratio:0)'
  @media screen and #{$safari-only}

@include safari-only
  body
    background-image: linear-gradient(green 0%, red 100%) !important

这会产生一个 mixin 没有主体的错误

所以我想知道:

  1. 我如何告诉 sass 不格式化代码而不是我?
  2. 如何将第一级选择器创建为 mixin

附言我正在使用Sass 3.4.21 (Selective Steve)

最好的问候,

【问题讨论】:

    标签: css sass


    【解决方案1】:

    您可以在 SASS 中使用 #{} 强制格式化

    $safari-only: #{'(min-color-index:0) and(-webkit-min-device-pixel-ratio:0)'}
    

    【讨论】:

    • 您能根据我的需要编辑您的答案吗?我需要为选择器创建文字,而不是为价值。 background 只是一个突出示例,可以清楚地看到 chrome 和 safari 之间的区别。正如我写的那样,我使用了#{$safari-only} 它不起作用!如果我用它的值替换一个变量,它不会改变任何事情:(
    • 正如我在问题中所写,它不起作用!它符合...) and (-web...之后看到space吗?它破坏了此浏览器特定规则的所有功能。我想你只是没有阅读我的问题:)
    • 能否请您查看我添加的演示? sassmeister.com/gist/3ddf25510ea9b0e5d9e55f6ac9929ea7
    猜你喜欢
    • 2017-09-19
    • 2019-07-11
    • 2017-01-07
    • 2014-10-21
    • 2021-03-17
    • 2020-09-03
    • 2019-09-08
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多