[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 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的更多相关文章
- [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 ...
- [SCSS] Write Custom Functions with the SCSS @function Directive
Writing SCSS @functions is similar to writing functions in other programming languages; they can acc ...
- vue,一路走来(17)--vue使用scss,并且全局引入公共scss样式
最近朋友问如何在vue项目中使用scss样式,想起之前项目是直接在main.js直接import css文件的,然而main.js不可以直接import scss文件. import './asset ...
- 在vue-cli中安装scss,且可以全局引入scss的步骤
简历魔板__个人简历模板在线生成 在写vue的css样式时,觉得需要css预处理器让自己的css更加简洁.适应性更强.可读性更佳,更易于代码的维护,于是在vue-cli脚手架采用scss.写过的人都知 ...
- Upgrading to Java 8——第三章 Optional and Similar Classes
Java程序员对付空指针异常已经好多年了.在Java8中将有新的方式去处理他们.通过包装一个潜在的可能为null的类称为Optianal. 在Java8中添加了the Optional, Option ...
- Ruby安装Scss
Ruby安装Scss 引言 已经许久不写HTML了,今天有点以前的东西要改.但是刚装的Windows10,已经没有以前的Web开发环境了.只好重新安装. 结果Webstorm装好后配置Scss出现错误 ...
- webstorm配置scss自动编译路径
webstorm支持sass的同步编译,也就是即写即编译,并且可以指定编译后的css的目录. 本文使用的webstorm为8.0版本 scss安装:http://www.w3cplus.com/sas ...
- scss语法介绍
这里既然是对语法的介绍,那么至于如何安装和编译scss我就不多少了,主要是因为本人在群里认识的小伙伴有的不会用scss,所以就借着放假的机会来对scss语法做个总结,博主在开发过程中用scss蛮多,所 ...
- SCSS 在项目中的运用
最后一段时间一直在做一些网站或是CMS的项目,想用bootstrap,但是,设计那哥们说了,用什么都行,就不能用bootstrap,我去了个..... 无语中,逼着自己写.说实话,就是用bootstr ...
随机推荐
- 例说linux内核与应用数据通信(四):映射设备内核空间到用户态
[版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet.文章仅供学习交流,请勿用于商业用途] 一个进程的内存映象由以下几部分组成:代码段.数据段.BSS段和 ...
- 读阮一峰《ECMAScript 6 入门》小结
读阮一峰<ECMAScript 6 入门>小结,http://es6.ruanyifeng.com/ 1. ES6简介 Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转 ...
- LiveReload配置,安装使用方法~~~前端页面神助手
一.Chrome端安装LiveReload插件 1.首先这里啰嗦一下,如果Chrome无法进入商店,可以先安装一下谷歌商店助手 谷歌商店插件地址 http://pan.baidu.com/s/1dF1 ...
- 【习题 7-6 UVA - 12113】Overlapping Squares
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出来一个正方形. 然后每次枚举新加的正方形左上角的坐标就可以. 注意覆盖的规则,控制一下就可以. 然后暴力判断是否相同. 暴 ...
- C# Bartender模板打印 条码,二维码, 文字, 及操作RFID标签等。
1.在之前写的一篇文章中, 有讲到如何利用ZPL命令去操作打印里, 后面发现通过模板的方式会更加方便快捷, 既不用去掌握ZPL的实现细节, 就可以轻松的调用实现打印的功能. 解决方案: 1.网络下载 ...
- geotools修改shapefile 属性名乱码问题
在GeoServer中文社区的讨论地址为:http://opengeo.cn/bbs/read.php?tid=1701&page=e&#a 使用geotools修改shapefile ...
- caffe 在 windows 下的配置(scripts\build_win.cmd)
官网配置文档见:GitHub - BVLC/caffe at windows 1. windows 设置 requirements: visual studio 2013/2015 CMake > ...
- 使用@Order调整配置类加载顺序
转自:https://blog.csdn.net/qq_15037231/article/details/78158553 4.1 @Order Spring 4.2 利用@Order控制配置类的加载 ...
- 自己动手开发jQuery插件全面解析 jquery插件开发方法
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- Altium Designer如何统一改变pcb状态下的原件标号位置
原创 我用的是Altium Designer16版本 变成 步骤如下: 选中标号 右击 下边一步很重要: 点击应用和确定 在之后弹出的对话框中选则你要改变的位置,我这里是把标号改变到原件的右侧: 等待 ...