We can write reusable styles with the SCSS @extend or @mixin directives. Which one is better? It depends. A better question is, how do they differ?
Extends:
change the source order, mixins don’t.
maintain relationships, mixins don’t.
share inherited traits, mixins don’t.
can extend multiple classes, mixins don’t.
can create multiple class declarations in the compiled CSS, mixins don’t.
can use the placeholder selector, mixins don’t.

Mixins:
can accept arguments, extends don’t.
can pass additional content, extends don’t.
repeat code when compiled, extends group class names together.
work well wIth media queries, extends have a limited interaction wIth media queries.

In this lesson we learn about writing reusable styles with the @extend directive and how it compares to the @mixin directive.

.base {
color: red; &:hover {color: green}
&::after {content: "?"} &-stuff {height: 5rem} // this will not be extended
} .cool {height: 20rem} .new {
width: 20px;
// extend multi classes
@extend .base, .cool;
} /*
It is possible to use placeholder
*/ %base {
color: red;
} .new2 {
@extend %base;
} /*
Placeholder for extend with mixin
*/
%hero {background: linear-gradient(red, white, black)}
%villain {background: darkred} @mixin character($type: hero) {
height: 20px;
width: 20px; @extend %#{$type} // #{} --> output a string
} .doc-ock {@include character(villain)} /*
Works with media query
*/
@media screen and (min-width: 800px) {
%buddy { color: purple; }
} @media screen and (min-width: 800px) {
.buddy {
@extend %buddy;
}
}

Reference to http://sass-lang.com/documentation/file.SASS_REFERENCE.html#interpolation_

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

  1. [SCSS] Reuse Styles with the SCSS @mixin Directive

    Copy/pasting the same code is redundant and updating copy/pasted code slows development velocity. Mi ...

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

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

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

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

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

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

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

  6. dvaJs使用注意事项

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

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

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

  8. Scss开发临时学习过程

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

  9. 前端之Sass/Scss实战笔记

    简介 Sass 有两种语法规则(syntaxes),目前新的语法规则(从 Sass 3开始)被称为 “SCSS”( 时髦的css(Sassy CSS)),它是css3语法的的拓展级,就是说每一个语法正 ...

随机推荐

  1. python3操作Excel

    1.安装openpyxl模块: 在cmd命令窗执行命令 pip install openpyxl    安装openpyxl模块 from openpyxl import load_workbook ...

  2. linux下安装sar

    本文转自(https://blog.csdn.net/qq_31391261/article/details/79419740) OS:centos6.5 操作步骤: 1)输入sar命令:sar -- ...

  3. Spring MVC modelandview

    一开始${Name} 不能显示 原来是: import org.springframework.web.portlet.ModelAndView; --错误的引入 import org.springf ...

  4. Excel查询序列所相应的值-vLoopup函数,求比例分子改变但分母不变

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWV3ZWlvdXlhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  5. 初识ThreadLocal

    近期公司在进行Java开发者的招聘活动,当中有一道面试题是这种:"请简单描写叙述一下ThreadLocal类的作用." 结果发现有非常多的面试者没有听说过ThreadLocal或者 ...

  6. HTML基础第四讲---图像

    转自:https://blog.csdn.net/likaier/article/details/326735 图像,也就是images,在html语法中用img来表示,其基本的语法是:   < ...

  7. 微信消息体加解密及EncodingAESKey

    公众平台消息体签名及加解密方案概述 1.新增消息体签名验证,用于公众平台和公众账号验证消息体的正确性 2.针对推送给微信公众账号的普通消息和事件消息,以及推送给设备公众账号的设备消息进行加密 3.公众 ...

  8. ORACLE11g R2【单实例 FS→单实例FS】

    ORACLE11g R2[单实例 FS→单实例FS] 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6 ...

  9. python相关系数

    皮尔逊相关系数: 用于度量两个变量X和Y之间的相关(线性相关),其值介于-1与1之间. 几组的点集,以及各个点集中和之间的相关系数.我们可以发现相关系数反映的是变量之间的线性关系和相关性的方向(第一排 ...

  10. 原生js大总结十

    91.ajax的优点     a.提高运行效率   b.提高用户体验,让多件事情同时发生   c.在不刷新页面的情况下可以对局部数据进行加载和刷新       92.ajax请求的流程   1.创建通 ...