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.

  1. .base {
  2. color: red;
  3.  
  4. &:hover {color: green}
  5. &::after {content: "?"}
  6.  
  7. &-stuff {height: 5rem} // this will not be extended
  8. }
  9.  
  10. .cool {height: 20rem}
  11.  
  12. .new {
  13. width: 20px;
  14. // extend multi classes
  15. @extend .base, .cool;
  16. }
  17.  
  18. /*
  19. It is possible to use placeholder
  20. */
  21.  
  22. %base {
  23. color: red;
  24. }
  25.  
  26. .new2 {
  27. @extend %base;
  28. }
  29.  
  30. /*
  31. Placeholder for extend with mixin
  32. */
  33. %hero {background: linear-gradient(red, white, black)}
  34. %villain {background: darkred}
  35.  
  36. @mixin character($type: hero) {
  37. height: 20px;
  38. width: 20px;
  39.  
  40. @extend %#{$type} // #{} --> output a string
  41. }
  42.  
  43. .doc-ock {@include character(villain)}
  44.  
  45. /*
  46. Works with media query
  47. */
  48. @media screen and (min-width: 800px) {
  49. %buddy { color: purple; }
  50. }
  51.  
  52. @media screen and (min-width: 800px) {
  53. .buddy {
  54. @extend %buddy;
  55. }
  56. }

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. 【Todo】Zookeeper系列文章

    http://nileader.blog.51cto.com/1381108/1068033

  2. 移动mm 话费支付接入过程(ane)

    下面记录移动mm 话费支付接入的过程 1.强联网.弱联网差别.sdk是否有区分?用户体验部分由什么不同和差异? 差别在于强联网是网络通道(wifi/gprs/3g),弱联网是走短信通道,用户层面差异在 ...

  3. amazeui学习笔记三(你来我往1)--常见问题FAQs

    amazeui学习笔记三(你来我往1)--常见问题FAQs 一.总结 1.DOM事件失败:记得加上初始化代码,例如 图片轮播 $('#my-slider').flexslider(); 2.jquer ...

  4. socket UDP简单通讯

    // // SocketUDPServerClient.m // socket_server_client // // Created by lujunjie on 2016/11/26. // Co ...

  5. 谈谈Command对象与数据检索

    摘要 到目前为止,我相信大家对于ADO.NET如何与外部数据源建立连接以及如何提高连接性能等相关知识已经牢固于心了.连接对象作为ADO.NET的主力先锋,为用户与数据库交互搭建了扎实的桥梁.它的一生是 ...

  6. Python 极简教程(五)输入输出

    输入函数,用于接收键盘输入.主要用于在学习和练习过程中,增加练习的乐趣.让我们的程序相对完整和具备简单的交互能力. 输出函数,将代码运行结果打印在控制台上,同样也能让我们观察程序运行的结果.也是为了增 ...

  7. 黑马day01 xml 的解析方式

    XML编程:利用java程序去增删改查(CRUD)xml中的数据 解析思想: dom解析 sax解析 基于这两种解析思想市面上就有了非常多的解析api sun jaxp既有dom方式也有sax方式,而 ...

  8. 洛谷—— P1091 合唱队形

    https://www.luogu.org/problem/show?pid=1091#sub  ||  http://codevs.cn/problem/1058/ 题目描述 N位同学站成一排,音乐 ...

  9. 深入理解线程本地变量ThreadLocal

    ThreadLocal理解: 假设在多线程并发环境中.一个可变对象涉及到共享与竞争,那么该可变对象就一定会涉及到线程间同步操作,这是多线程并发问题. 否则该可变对象将作为线程私有对象,可通过Threa ...

  10. [Nuxt] Build a Vue.js Form then use Vuex Actions to Post to an API in Nuxt

    The default behavior of submitting an HTML form is to reload the page. You can use the Vue.js @submi ...