The SCSS @for directive is great when we know how many iterations are required and we only need 1 variable. What if we need multiple variables to iterate over data sets? Hello SCSS maps and the @each control directive! In this lesson, we’ll check out iterating with lists and looping over data sets with maps and the @each directive.

// work with simple array
$superheroes: wonder-woman, spiderman, batman, superman;
@each $hero in $superheroes {
.#{$hero}-logo {
content: "#{$hero}";
}
} // we get
.wonder-woman-logo {
content: "wonder-woman"; } .spiderman-logo {
content: "spiderman"; } .batman-logo {
content: "batman"; } .superman-logo {
content: "superman"; }

key: value pairs using map-get():

// work with key: value pair
$breakpoints: (sm: 375px, md: 768px, lg: 1200px);
$container-widths: (sm: 250px, md: 500px, lg: 750px);
@each $size, $bp in $breakpoints {
@media only screen and (min-width: $bp) {
.container-width {
// because $breakpoints and $container-widths have the same key
// we can use map_get(target_ary, key) to get value
width: map_get($container-widths, $size);
}
}
} // we get
@media only screen and (min-width: 375px) {
.container-width {
width: 250px; } } @media only screen and (min-width: 768px) {
.container-width {
width: 500px; } } @media only screen and (min-width: 1200px) {
.container-width {
width: 750px; } }

index: values pair using nth()

$hero-media:  (1 375px 768px crimson),
(2 768px 1000px darkred),
(3 1200px 1400px grey),
(4 768px 1200px blue); // we get @media only screen and (min-width: 375px) and (max-width: 768px) {
.wonder-woman {
background-color: crimson; } } @media only screen and (min-width: 768px) and (max-width: 1000px) {
.spiderman {
background-color: darkred; } } @media only screen and (min-width: 1200px) and (max-width: 1400px) {
.batman {
background-color: grey; } } @media only screen and (min-width: 768px) and (max-width: 1200px) {
.superman {
background-color: blue; } }

[SCSS] Loop Over Data with the SCSS @each Control Directive的更多相关文章

  1. [SCSS] Write similar classes with the SCSS @for Control Directive

    Writing similar classes with minor variations, like utility classes, can be a pain to write and upda ...

  2. [SCSS] Write Custom Functions with the SCSS @function Directive

    Writing SCSS @functions is similar to writing functions in other programming languages; they can acc ...

  3. vue,一路走来(17)--vue使用scss,并且全局引入公共scss样式

    最近朋友问如何在vue项目中使用scss样式,想起之前项目是直接在main.js直接import css文件的,然而main.js不可以直接import scss文件. import './asset ...

  4. 在vue-cli中安装scss,且可以全局引入scss的步骤

    简历魔板__个人简历模板在线生成 在写vue的css样式时,觉得需要css预处理器让自己的css更加简洁.适应性更强.可读性更佳,更易于代码的维护,于是在vue-cli脚手架采用scss.写过的人都知 ...

  5. csharp: Data binding in WPF DataGrid control

    <Window x:Class="WpfProjectDemo.MainWindow" xmlns="http://schemas.microsoft.com/wi ...

  6. VueCli3如何传递scss全局变量

    当我们尝试在一个scss文件中定义全局变量然后在.vue文件中使用的时候 哦豁,找不到变量,意料之外 我发现犯了一个错误,没导入,@import 'path/to/file.scss',不过这样的话, ...

  7. Nuxt.js中scss公用文件(不使用官方插件style-resources)

    项目多多少少应该都遇到有公用文件这种情况,比如说偶尔某一天产品来找你,能不能明天把网站的这个颜色给我改下?第二天再来给我换回来? 如果再css2.x时代,不使用css预处理技术,这一改只能“查找替换” ...

  8. vue cli 3

    介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-service-global 快 ...

  9. d2-admin中那些不错的技巧

    d2-admin基于vue-cli3 路由相关 刷新路由,参照官方  组件内的守卫 但是搞不明白为何加了句 render:h => h() { path: 'refresh', name: 'r ...

随机推荐

  1. golang 简单web服务

    1.golang print输入 package main import "fmt" func main() { fmt.Printf("Hello World!\n&q ...

  2. 热点共享SS网络

    # 测试系统: Ubuntu 16.04 LTS-lxde-ARM # ***-libev 安装脚本源于 秋水逸冰: https://teddysun.com/358.html # ss-tproxy ...

  3. [51Nod]NOIP2018提高组省一冲奖班模测训练(一)题解

    http://www.51nod.com/contest/problemList.html#!contestId=72&randomCode=147206 原题水题大赛.. A.珂朵莉的旅行 ...

  4. read()方法读取的是一个字节,为什么返回是int,而不是byte

    因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到111111111                 那 ...

  5. 用硬件卡克隆Linux集群

    650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpic.php?refimg= ...

  6. go timer

    go timer package main import ( "fmt" "time" ) func debounce(interval time.Durati ...

  7. 设计模式六大原则(四):接口隔离原则(Interface Segregation Principle)

    接口隔离原则(ISP)定义: 客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来: 类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不 ...

  8. 2.1 Vue组件

    Vue组件 全局组件和局部组件 父子组件通讯-数据传递 父->子:通过Props传递 子->父:不允许,但vue通过子组件触发Emit来提交给子组件进行触发 Slot import Cou ...

  9. Servlet的异常处理机制

    一 声明式异常处理   在web.xml中对声明对各种异常的处理方法. 通过 <error-page>元素来声明. 此元素的结构如下:                    +------ ...

  10. 【“玲珑杯”ACM比赛 Round #20 H】康娜的数学课

    [链接]http://www.ifrog.cc/acm/problem/1161 [题意] 在这里写题意 [题解] 首先x<l肯定无解; 然后,肯定是要选其中的一些数字的. 而且这些数字肯定是大 ...