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:

  1. we can pass default value: '$base-color: #a83b24'
  2. we can use optional parameter: '$border: null'
  3. 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"); }
};
}

Github

[SCSS] Reuse Styles with the SCSS @mixin Directive的更多相关文章

  1. [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 ...

  2. 在vue-cli中如何安装scss,并全局引入scss

    在vue-cli脚手架采用scss正确的使用姿势 步骤一: 安装node-sass.sass-loader.style-loader npm install node-sass --save-dev ...

  3. [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 ...

  4. [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 ...

  5. error app/styles/components/iconfont.scss (Line 12: Invalid GBK character "\xE5")

    因为要用到iconfont,引入iconfont到sass文件后,出现编译sass文件错误,如下截图: 解决方法:在顶部设置编码格式 @charset "utf-8"; 编译成功!

  6. 在vue项目中使用scss,以及vscode适配scss语法(解决使用scss语法编辑器报错)

    项目搭建好之后 安装sass 依赖包 npm install --save-dev sass-loader //sass-loader依赖于node-sass npm install --save-d ...

  7. dvaJs使用注意事项

    项目参考地址 dva-yicha 1. 使用路由跳转的方式 (1)所有的路由跳转功能都放到 dva/router 里面的 import { routerRedux } from 'dva/router ...

  8. css预编译语言 sass scss(变量$var, css嵌套规则,@import规则,@extend,@mixin)

    什么是sass Sass 是对 CSS 的扩展,让 CSS 语言更强大.优雅. 它允许你使用变量.嵌套规则. mixins.导入等众多功能, 并且完全兼容 CSS 语法. Sass 有助于保持大型样式 ...

  9. Scss开发临时学习过程

    SCSS语法: 假设变量申明带有!default,那么如果在此申明之前没有这个变量的申明,则用这个值,反之如果之前有申明,则用申明的值. ‘...’传递多个参数: @mixin box-shadow( ...

随机推荐

  1. android图像处理系列之四--给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  2. ReactJs 入门DEMO(转自别人)

    附件是分享的一些他人的ReactJs入门DEMO,以前版本使用的是JSXTransformer.js,新版的用browser.min.js替代了. DEMO 下载地址:http://files.cnb ...

  3. Linux平台下使用AdventNet ManageEngine OpUtils监控网络

    AdventNet ManageEngine OpUtils 是一套系统和网络监视工具,它有Linux/Windows系统平台的免费版和企业版,该软件是一款用于监视诸如路由器,交换机,服务器或者桌面这 ...

  4. vue项目使用axios

    使用: npm install axios --save-dev 在main.js中import: 使用: (1):POST方式 let data= [{receiveAdd:receiveAddVa ...

  5. elasticsearch选举master

    数据节点确定了master以后.会开启MasterPinger线程来与其保持心跳通信的逻辑假设通信过程中链接断开,会通过MasterNodeFailureListener监听器进行回调处理处理过程中会 ...

  6. UML学习之初步总结

    UML(Unified Modeling Language)即统一建模语言,是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的.面向对象的.软件密集系统的制品的开放方法.UML展现了一系列最 ...

  7. 动态规划例子:Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  8. 福昕pdf阅读器如何删除所有注释

    然后选中第一个 移动到最后按住shift,选择最后一个, 总之就是选中所有的 然后右键,点击删除即可. 不要忘记保存呦

  9. 洛谷——P1548 棋盘问题

    https://www.luogu.org/problem/show?pid=1548#sub 题目描述 设有一个N*M方格的棋盘(l<=N<=100,1<=M<=100)(3 ...

  10. 洛谷 P1727 计算π

    P1727 计算π 题目背景 <爱与愁的故事第二弹·compute>第一章. 题目描述 中秋至,博饼声铿锵不断.爱与愁大神兴致勃勃地到学校博饼,结果抱回家的只有一秀二举.爱与愁大神十分生气 ...