[SCSS] Reuse Styles with the SCSS @mixin Directive
Copy/pasting the same code is redundant and updating copy/pasted code slows development velocity. Mixins are reusable chunks of code that are included, similar to calling a function, instead of copy/pasted.
Mixins have some nice features:
- Arguments just like functions.
- Arguments can have default values and optional values.
- Named arguments allow us to use optional and default arguments when the mixin is included.
- Variable arguments allow us to have a dynamic number of arguments when the mixin is included.
- The @content directive allow us to add additional styles when the mixin is included.
In this lesson we learn how to DRY up the code with the SCSS @mixin directive and make copy/paste a thing of the past.
Define a mixin:
@mixin make-character($base-color: #a83b24, $mix-color: #fffaa6, $border: null) {
Here as you can see, we use named parameters. The benifits when we use named parameters is:
- we can pass default value: '$base-color: #a83b24'
- we can use optional parameter: '$border: null'
- when we use mixin, the parameters order doesn't matter
@include make-character($border: 5px solid brown, $mix-color: pink)
If you don't know how many paramter the mixin will take, you can do:
@mixin make-transitions($transitions...) { transition: $transitions; }
It can take as many as paramters you pass in:
@include make-transitions(margin 1s, border-radius 1s, border 1s, transform 1s);
@content directive
@content directive refers to whatever you pass in when you using mixin:
.wolverine {
@include make-character($border: 5px solid brown, $mix-color: pink) {
@include make-transitions(margin 1s, border-radius 1s, border 1s, transform 1s);
&:hover {
margin-top: 5rem;
border-radius: %;
border: 10px solid green;
transform: rotate3d(, , , 360deg);
}
};
}
So now, @content referts to all the highlighted part.
@mixin make-character($base-color: #a83b24, $mix-color: #fffaa6, $border: null) {
$light-color: lighten($base-color, %);
$dark-color: darken($base-color, %);
$cbc: complement($base-color);
$clc: complement($light-color);
$cdc: complement($dark-color);
background-image: linear-gradient($light-color, $base-color, $dark-color);
border: $border;
&:hover { background-image: linear-gradient($clc, $cbc, $cdc); }
&:hover &-text { color: transparentize(mix($base-color, $mix-color, %), .); }
&-text {
color: mix($base-color, $mix-color, %);
}
img { @content; }
}
In this context, 'img' will get all the highlighted styles.
.character {
text-align: center;
width: 15rem;
display: inline-block;
margin: 0.5rem;
p {
font-size: 1.5rem;
padding-bottom: 0.5rem;
}
img {
margin-top: 1rem;
border-radius: 25%;
}
}
@mixin make-transitions($transitions...) { transition: $transitions; }
@mixin make-character($base-color: #a83b24, $mix-color: #fffaa6, $border: null) {
$light-color: lighten($base-color, 20%);
$dark-color: darken($base-color, 35%);
$cbc: complement($base-color);
$clc: complement($light-color);
$cdc: complement($dark-color);
background-image: linear-gradient($light-color, $base-color, $dark-color);
border: $border;
&:hover { background-image: linear-gradient($clc, $cbc, $cdc); }
&:hover &-text { color: transparentize(mix($base-color, $mix-color, 25%), .2); }
&-text {
color: mix($base-color, $mix-color, 75%);
}
img { @content; }
}
@mixin media($min-width) {
@media screen and (min-width: $min-width) { @content; }
}
.wolverine {
@include make-character($border: 5px solid brown, $mix-color: pink) {
@include make-transitions(margin 1s, border-radius 1s, border 1s, transform 1s);
&:hover {
margin-top: 5rem;
border-radius: 50%;
border: 10px solid green;
transform: rotate3d(10, 0, 0, 360deg);
}
};
}
.rogue { @include make-character(#0ab36d, #FFFE8A, 5px solid green); }
.firestar { @include make-character(#DB233B, #e3fd00); }
.nightcrawler {
@include make-character(#1d6098, #ffcef9) {
@include media(800px) { content: url("../images/bamf.jpg"); }
};
}
[SCSS] Reuse Styles with the SCSS @mixin Directive的更多相关文章
- [SCSS] Reuse Styles with the SCSS @extend Directive
We can write reusable styles with the SCSS @extend or @mixin directives. Which one is better? It dep ...
- 在vue-cli中如何安装scss,并全局引入scss
在vue-cli脚手架采用scss正确的使用姿势 步骤一: 安装node-sass.sass-loader.style-loader npm install node-sass --save-dev ...
- [Scss Flex] Reuse Flexbox Styles With A Sass Mixin
This lesson covers flexbox in a reusable mixin that should cover most layout situations on your site ...
- [SCSS] Organize Styles with SCSS Nesting and the Parent Selector
SCSS nesting can produce DRYer code by targeting child elements without having to write the parent c ...
- error app/styles/components/iconfont.scss (Line 12: Invalid GBK character "\xE5")
因为要用到iconfont,引入iconfont到sass文件后,出现编译sass文件错误,如下截图: 解决方法:在顶部设置编码格式 @charset "utf-8"; 编译成功!
- 在vue项目中使用scss,以及vscode适配scss语法(解决使用scss语法编辑器报错)
项目搭建好之后 安装sass 依赖包 npm install --save-dev sass-loader //sass-loader依赖于node-sass npm install --save-d ...
- dvaJs使用注意事项
项目参考地址 dva-yicha 1. 使用路由跳转的方式 (1)所有的路由跳转功能都放到 dva/router 里面的 import { routerRedux } from 'dva/router ...
- css预编译语言 sass scss(变量$var, css嵌套规则,@import规则,@extend,@mixin)
什么是sass Sass 是对 CSS 的扩展,让 CSS 语言更强大.优雅. 它允许你使用变量.嵌套规则. mixins.导入等众多功能, 并且完全兼容 CSS 语法. Sass 有助于保持大型样式 ...
- Scss开发临时学习过程
SCSS语法: 假设变量申明带有!default,那么如果在此申明之前没有这个变量的申明,则用这个值,反之如果之前有申明,则用申明的值. ‘...’传递多个参数: @mixin box-shadow( ...
随机推荐
- Android开发经验之点击图片判断是否在图片范围之内
package xiaosi.grivaty; import android.content.Context; import android.graphics.Bitmap; import andro ...
- 《WPF》Expander控件简单美化
示例图: Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板. 1.主要的组成 一个头部(header) 和 一个 内容(content) 组成. <Expander Expand ...
- 有趣的Ruby-学习笔记4
Ruby块 块.在我看来就是插入一段可变的函数 block_name{ statement1 statement2 .......... } 看起来不知道是什么,只是别急,继续往下看. 块函数通过yi ...
- Core Animation 文档翻译—附录B(可动画的属性)
前言 许多CALayer和CIFliter的属性都是可动画的.本节附录列出了这些属性默认使用的动画. CALayer可动画属性 表B-1展示了CALayer类的可动画属性.针对每个属性此表 ...
- 40.lombok在IntelliJ IDEA下的使用
转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...
- java 文件读写--转载
读文件 http://www.baeldung.com/java-read-file Java – Read from File 1. Overview In this tutorial we’ll ...
- html只能有一个id,并且id的值只能是一个
1.如果有相同的ID,javascript只会取第一个具有该ID的标签. 2.如果id值有两个,JS只会取到第一个,并不会像class类一样,类名并列就可以同时取到.
- 笔记二:JS的输出、语法、语句、字符串、条件语句、switch语句、for循环、while循环
1.JS的输出: 注意:JS没有任何打印或者输出的函数 JS输出数据的集中方法: 1.使用window.alert()弹出警告框: 2.使用document.write()方法将内容写到HTML文档 ...
- JS学习笔记 - fgm练习 - 限制输入框的字符类型 正则 和 || 或运算符的运用 i++和++i
<script> window.onload = function(){ var aInp = document.getElementsByTagName('input'); var oS ...
- 【习题 6-4 UVA-439】Knight Moves
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs模板题 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sev ...