css - 使用 SASS 进行嵌套数据切换

标签 css button sass

我想知道是否可以在 SASS 中嵌套数据切换。 假设我要制作一些宽度和高度相同的按钮,但每个按钮的颜色不同。

是否可以让他们喜欢:

[data-toggle]{
   width:150px;
   height:50px;
   &=result{
     background:green;
   }
   &=create{
     background:red;
   }
}

最佳答案

不可能按照您建议的方式嵌套选择器。

一个解决方案是创建一个 data-toggle 值到背景颜色的 Sass 映射,然后使用 @each 循环迭代键值对:

// Base styling for elements with the [data-toggle] attribute
[data-toggle] {
  width: 150px;
  height: 50px;  
}

// Generate declarations for colored toggles
$data-toggle-colors: (
  'result': green,
  'create': red
);

@each $color in $data-toggle-colors {
  $key:   nth($color, 1);
  $value: nth($color, 2);

  [data-toggle=#{$key}] {
    background-color: $value;
  }
}

代码笔:https://codepen.io/anon/pen/Lkgxvv

关于css - 使用 SASS 进行嵌套数据切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38767339/

相关文章:

html - 内联 block 元素的问题

ember.js - 将 Material Design 组件与 ember-cli-sass 结合使用

javascript - grunt sass 任务不起作用?

node.js - 边看咖啡边看 Jade 文件,同时完成繁琐的任务

css - Bootstrap 网格 - center-block 不将元素定位在中心 - 为什么?

jquery - DIV 的 jQuery-UI 的 CSS

css - 获取 Google 的 Smarty Pins 背景动画

c# - 如何制作简单的 XNA 按钮?

html - 文本不会出现在页眉上

python - QPushButton 获取颜色