Writing similar classes with minor variations, like utility classes, can be a pain to write and update. Sometimes just a single character is the only difference between classes and updating the defining parameter means we need to change it for every class name and value. We can write a class one time and the @for directive can write all the minor variations for us. If the similar classes need to be updated, with the help of the @for directive, they only need to be updated once. In this lesson we learn how to leverage the power of the SCSS @for control directive to relieve the pain.

Basic @for-to loop in SCSS:

// doesn't include 10
@for $i from 1 to 10 {
.order {
order: $i;
}
}

output:

.order {
order:; } .order {
order:; }
...
.order {
order:; }

@for-through:

// includes 5
@for $x from 1 through 5 {
.level {
z-index: $x;
}
}

output:

.level {
z-index:; } ... .level {
z-index:; }

@for with 'if' condition:

@for $i from 0 through 10 {
$value: .5 * $i;
$has-decimal: floor($value) != $value;
$class-name: if(
$has-decimal,
#{$value - 0.5}pt5, // if true
$value // if false
); .mt-#{$class-name} {
margin-top: #{$value}rem;
}
}

output:

.mt-0 {
margin-top: 0rem; } .mt-0pt5 {
margin-top: 0.5rem; } .mt-1 {
margin-top: 1rem; } .mt-1pt5 {
margin-top: 1.5rem; } .. .mt-5 {
margin-top: 5rem; }

Using attr selector:

@for $i from 0 through 10 {
$value: .5 * $i; [class~="mt-#{$value}"] {
margin-top: #{$value}rem;
}
}

output:

[class~="mt-0"] {
margin-top: 0rem; } [class~="mt-0.5"] {
margin-top: 0.5rem; } [class~="mt-1"] {
margin-top: 1rem; } .. [class~="mt-5"] {
margin-top: 5rem; }

@for with @mixin

@mixin light-color-class($color, $color-name,$i) {
$color-value: if($i == 0, $color, lighten($color, 5% * $i)); .#{$color-name}#{$i} {
color: $color-value;
}
} @for $i from 0 through 5 {
@include light-color-class(red, 'passion', $i);
@include light-color-class(green, 'natural', $i);
@include light-color-class(blue, 'cool', $i);
}

output:

.passion0 {
color: red; } .natural0 {
color: green; } .cool0 {
color: blue; } .passion1 {
color: #ff1a1a; } .natural1 {
color: #009a00; } .cool1 {
color: #1a1aff; } ...

[SCSS] Write similar classes with the SCSS @for Control Directive的更多相关文章

  1. [SCSS] Loop Over Data with the SCSS @each Control Directive

    The SCSS @for directive is great when we know how many iterations are required and we only need 1 va ...

  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. Upgrading to Java 8——第三章 Optional and Similar Classes

    Java程序员对付空指针异常已经好多年了.在Java8中将有新的方式去处理他们.通过包装一个潜在的可能为null的类称为Optianal. 在Java8中添加了the Optional, Option ...

  6. Ruby安装Scss

    Ruby安装Scss 引言 已经许久不写HTML了,今天有点以前的东西要改.但是刚装的Windows10,已经没有以前的Web开发环境了.只好重新安装. 结果Webstorm装好后配置Scss出现错误 ...

  7. webstorm配置scss自动编译路径

    webstorm支持sass的同步编译,也就是即写即编译,并且可以指定编译后的css的目录. 本文使用的webstorm为8.0版本 scss安装:http://www.w3cplus.com/sas ...

  8. scss语法介绍

    这里既然是对语法的介绍,那么至于如何安装和编译scss我就不多少了,主要是因为本人在群里认识的小伙伴有的不会用scss,所以就借着放假的机会来对scss语法做个总结,博主在开发过程中用scss蛮多,所 ...

  9. SCSS 在项目中的运用

    最后一段时间一直在做一些网站或是CMS的项目,想用bootstrap,但是,设计那哥们说了,用什么都行,就不能用bootstrap,我去了个..... 无语中,逼着自己写.说实话,就是用bootstr ...

随机推荐

  1. GridView-属性大全

    这是个网格控件 他的实现也是通过adapter来实现的,感觉跟listview在使用上并没有多大的区别 常见属性如下 1.android:numColumns=”auto_fit” //GridVie ...

  2. Pairs Forming LCM

    题目: B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB Description Find the result of ...

  3. 【例题 8-7 UVA - 11572】Unique Snowflakes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 类似尺取法. 用set判断这段区间有没有重复的数字. 有的话,就把头节点的那个数字删掉,直到没有为止. [代码] /* 1.Shou ...

  4. hysbz 2243 染色(树链剖分)

    题目链接:hysbz 2243 染色 题目大意:略. 解题思路:树链剖分+线段树的区间合并,可是区间合并比較简单,节点仅仅要记录左右端点的颜色就可以. #include <cstdio> ...

  5. 《从零開始学Swift》学习笔记(Day 59)——代码排版

    原创文章,欢迎转载.转载请注明:关东升的博客 代码排版包括: 空行.空格.断行和缩进等内容.代码排版内容比較多工作量非常多.可是非常重要. 空行 空行将逻辑相关的代码段分隔开.以提高可读性. 下列情况 ...

  6. Java – Reading a Large File Efficiently--转

    原文地址:http://www.baeldung.com/java-read-lines-large-file 1. Overview This tutorial will show how to r ...

  7. 1.16 Python基础知识 - 装饰器初识

    Python中的装饰器就是函数,作用就是包装其他函数,为他们起到修饰作用.在不修改源代码的情况下,为这些函数额外添加一些功能,像日志记录,性能测试等.一个函数可以使用多个装饰器,产生的结果与装饰器的位 ...

  8. 关于Promise的详细总结

    1. 异步回调 1.1 回调地狱 在需要多个操作的时候,会导致多个回调函数嵌套,导致代码不够直观,就是常说的回调地狱 1.2 并行结果 如果几个异步操作之间并没有前后顺序之分,但需要等多个异步操作都完 ...

  9. Flask项目之手机端租房网站的实战开发(七)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  10. Python 极简教程(六)运算符

    运算符,我们日常生活中使用的加减乘除,都是运算符的一种.当然这种一般我们称为算术运算符,用于处理数字运算的. 但是在计算机语言中,还有很多的运算符.用于处理不用的情况. 主要有以下几类: 算术运算符 ...