Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sass带来的变革

w3cplus
November 08, 2014

 Sass带来的变革

介绍Sass的特性以及学习路线,并用案例介绍Sass & OOCSS结合在一些编写代码的强大魅力。

w3cplus

November 08, 2014
Tweet

More Decks by w3cplus

Other Decks in Technology

Transcript

  1. Sass学习路线 初级 ⾼高级 中级 环境 安装 语法 编译 特性 组织

    实战 调试 定义功能 封装插件 编写框架
  2. Sass安装 $ gem install sass $ sudo gem install sass

    $ sass -v Sass 3.4.7 (Selective Steve) http:/ /rubygems.org/gems/sass
  3. Sass编译 $ sass style.scss:style.css $ sass sass/:css/ 命令 编译 $

    sass —watch style.scss/:css/style.css GUI 编译 自动 化编 参考资料: http:/ /www.w3cplus.com/preprocessor/sass-compile.html http:/ /www.w3cplus.com/preprocessor/sass-gui-tool-koala.html http:/ /www.w3cplus.com/preprocessor/sass-gui-tool-codekit.html http:/ /www.w3cplus.com/preprocessor/nodejs-and-grunt-compile-sass-to-css.html
  4. Sass基本特性 变量$ 混合宏@mixin 占位符%placeholder 运算符 函数 嵌套 列表list 选择符& map

    控制命令 参考资料: http:/ /www.w3cplus.com/preprocessor/sass-basic-syntax-and-features.html http:/ /www.w3cplus.com/preprocessor/two-handy-and-advanced-sass-features-and-their-limitations.html http:/ /www.w3cplus.com/preprocessor/future-sass.html @at-root 继承
  5. Sass特性之变量 CSS中能有变量?据说在未来.... Sass中是有变量的,Sass中如何定义变量? Sass中的变量离不开美⼑刀 $ $width: 300px; 变量声明符 变量名称 变量值

    参考资料: http:/ /www.w3cplus.com/preprocessor/sass-basic-variable.html http:/ /www.w3cplus.com/preprocessor/stop-using-so-many-sass-variables.html http:/ /www.w3cplus.com/preprocessor/beginner/variable-naming.html http:/ /www.w3cplus.com/preprocessor/sass-selector-variables.html
  6. Sass特性之混合宏 @mixin box-shadow($shadow...) { @if length($shadow) >= 1 { @include

    prefixer(box-shadow, $shadow); } @else{ $shadow:0 0 4px rgba(0,0,0,.3); @include prefixer(box-shadow, $shadow); } } 声明混合宏符号 混合宏名称 混合宏参数 混合宏代码块 .box { @include box-shadow; } .box { @include box-shadow(inset 1px 1px 2px rgba(black,.3)); } 使用@include 引用混合宏 调用默认混合宏 调用带参数的混合宏
  7. Sass特性之继承 .mt5{ margin-top:5px; } .block{ @extend .mt5; } .mt5, .block

    { margin-top: 5px; } Sass Sass CSS @extend来继 承类名
  8. Sass特性之占位符 %mt5{ margin-top:5px; } .block{ @extend %mt5; } .block {

    margin-top: 5px; } Sass Sass CSS @extend来扩展 使用%声明占位符
  9. 混合宏 vs 继承 vs占位符 @mixin mt($var){ margin-top: $var; } .block{

    @include mt(5px); } .block { margin-top: 5px; } Sass Sass CSS 混合宏@mixin .mt5{ margin-top:5px; } .block{ @extend .mt5; } .mt5, .block { margin-top: 5px; } Sass Sass CSS 继承class %mt5{ margin-top:5px; } .block{ @extend %mt5; } .block { margin-top: 5px; } Sass Sass CSS 占位符%placeholder
  10. 混合宏 vs 继承 vs占位符 参考资料: http:/ /www.w3cplus.com/preprocessor/sass-basic-mixins-nesting-placeholders-extend.html http:/ /www.w3cplus.com/preprocessor/sass-mixins-function-placeholder.html http:/

    /www.w3cplus.com/preprocessor/ten-best-common-mixins.html http:/ /www.w3cplus.com/preprocessor/understanding-placeholder-selectors.html http:/ /www.w3cplus.com/preprocessor/sass-mixin-placeholder.html http:/ /www.w3cplus.com/preprocessor/creating-repeatable-style-pattern-sass-placeholders-vs-mixins.html http:/ /www.w3cplus.com/preprocessor/css-shapes-reference-boxes.html
  11. 单类 vs 多类 /*padding and margin all*/ .paxs{padding:5px;} .pas {padding:

    10px;} .pam{padding:15px;} .pal{padding:20px;} .paxl{padding: 25px;} .paxxl{padding: 30px;} .maxs{margin:5px;} .mas {margin: 10px;} .mam{margin:15px;} .mal{margin:20px;} .maxl{margin: 25px;} .maxxl{margin: 30px;} <div class="header maxs paxs"></div> /*padding and margin all*/ %paxs{padding:5px;} %pas {padding: 10px;} %pam{padding:15px;} %pal{padding:20px;} %paxl{padding: 25px;} %paxxl{padding: 30px;} %maxs{margin:5px;} %mas {margin: 10px;} %mam{margin:15px;} %mal{margin:20px;} %maxl{margin: 25px;} %maxxl{margin: 30px;} <div class="header"></div> .header { @extend %paxs; @extend %maxs; } .header { padding:5px; margin:5px; } 多类 单类 CSS HTML Sass Sass CSS HTML
  12. Sass特性之嵌套 .header { padding:5px; margin:5px; nav { background-color: red; }

    &:after{ content:""; display: table; clear: both; } .home & { background-color: green; } } .header { padding: 5px; margin: 5px; } .header nav { background-color: red; } .header:after { content: ""; display: table; clear: both; } .home .header { background-color: green; } Sass CSS 参考资料: http:/ /www.w3cplus.com/preprocessor/beginner/the-inception-rule.html http:/ /www.w3cplus.com/preprocessor/CSS-preprocessors-and-parent-selectors.html
  13. Sass特性之运算 $wide: 200px; $space: $wide / 10; .content { width:

    $wide * 5; padding: $space; } .block { width: $wide - $space; } .content { width: 1000px; padding: 20px; } .block { width: 180px; } Sass CSS
  14. Sass特性之函数 字符串函数 数字函数 列表函数 三元函数 颜⾊色函数 自定义函数 $link-color: green; .a

    { color: $link-color; background-color: lighten($link-color,5%); &:hover { color: darken($link-color,5%); } } .a { color: green; background-color: #009a00; } .a:hover { color: #006700; } @function pxTorem($px,$browser-default-font-size){ @return $px / $browser-default-font-size * 1rem; } h2 { font-size: pxTorem(32px,16px); } h2 { font-size: 2rem; } 参考资料: http:/ /www.w3cplus.com/preprocessor/sass-other-function.html http:/ /www.w3cplus.com/preprocessor/sass-color-function.html Sass CSS Sass CSS
  15. Sass特性之选择符 &的魅⼒力 a { color: orange; &:hover { color: darken(orange,5%);

    } .header & { color: lighten(orange,5%); } } .block { color: blue; &__element{ color: yellow; } &--modify { color: green; } } Sass a { color: orange; } a:hover { color: #e69500; } .header a { color: #ffae1a; } .block { color: blue; } .block__element { color: yellow; } .block--modify { color: green; } CSS 参考资料: http:/ /www.w3cplus.com/preprocessor/use-ampersand-in-selector-name-with-Sass.html http:/ /www.w3cplus.com/preprocessor/sass-future-use-ampersand.html
  16. Sass特性之@at-root .block { color: green; ul { list-style:none outside none;

    li { margin:0; @at-root a { color:green; } } } } .block { color: green; } .block ul { list-style: none outside none; } .block ul li { margin: 0; } a { color: green; } Sass CSS
  17. 控制命令之@if @mixin blockOrHidden($boolean:true) { @if $boolean { @debug "$boolean is

    #{$boolean}"; display: block; } @else { @debug "$boolean is #{$boolean}"; display: none; } } .block { @include blockOrHidden; } .hidden{ @include blockOrHidden(false); } .block { display: block; } .hidden { display: none; } Sass CSS
  18. 控制命令之@for $color: green !default; @for $i from 1 through 4

    { .#{$color}-#{$i}{ color: darken($color,5% * $i); } } .green-1 { color: #006700; } .green-2 { color: #004d00; } .green-3 { color: #003400; } .green-4 { color: #001a00; } Sass CSS @for $var from <start> through <end> .green-1 { color: #006700; } .green-2 { color: #004d00; } .green-3 { color: #003400; } @for $var from <start> to <end> CSS $color: green !default; @for $i from 1 to 4 { .#{$color}-#{$i}{ color: darken($color,5% * $i); } } Sass
  19. 控制命令之@each $list: adam john wynn mason kuroi; @mixin author-images {

    @each $author in $list { .photo-#{$author} { background: url("/images/avatars/#{$author}.png") no-repeat; } } } .author-bio { @include author-images; } .author-bio .photo-adam { background: url("/images/avatars/adam.png") no-repeat; } .author-bio .photo-john { background: url("/images/avatars/john.png") no-repeat; } .author-bio .photo-wynn { background: url("/images/avatars/wynn.png") no-repeat; } .author-bio .photo-mason { background: url("/images/avatars/mason.png") no-repeat; } .author-bio .photo-kuroir { background: url("/images/avatars/kuroir.png") no-repeat; } Sass CSS @each $var in <list>
  20. 控制命令之@while $types: 4; $type-width: 20px; @while $types > 0 {

    .while-#{$types} { width: $type-width + $types; } $types: $types - 1; } .while-4 { width: 24px; } .while-3 { width: 23px; } .while-2 { width: 22px; } .while-1 { width: 21px; } Sass CSS
  21. Sass特性之列表 $list:(); $list:(#b4d455,42,"awesome"); $list-space: "item-1" "item-2" "item-3"; $list: ( ("item-1.1",

    "item-1.2", "item-1.3"), ("item-2.1", "item-2.2", "item-2.3"), ("item-3.1", "item-3.2", "item-3.3") ); 定义列表⽅方式 .header { color: green; } .footer { color: yellow; } @function first($list){ @return nth($list,1); } @function last($list){ @return nth($list,length($list)); } $colors: green,blue,orange,yellow; .header { color: first($colors); } .footer{ color: last($colors); } CSS Sass 参考资料: http:/ /www.w3cplus.com/preprocessor/understanding-sass-list.html
  22. Sass特性之map $map: ( key: value, other-key: other-value ); Map语法 $configuration:

    ( padding: 1em, margin: 0 1em, color: grey ); .element { color: map-get($configuration, color); padding: map-get($configuration, padding); margin: map-get($configuration, margin); &::before { background-color: map-get($configuration, color); } } .element { color: grey; padding: 1em; margin: 0 1em; } .element::before { background-color: grey; } Sass CSS 参考资料: http:/ /www.w3cplus.com/preprocessor/using-sass-maps.html http:/ /www.w3cplus.com/preprocessor/so-you-want-to-play-with-list-maps.html http:/ /www.w3cplus.com/preprocessor/sass-maps.html http:/ /www.w3cplus.com/preprocessor/exploring-maps-in-sass-3-3-part-1.html
  23.  Sass+OOCSS <a href="#" class="twitter">Twitter</a> <a href="#" class="facebook">Facebook</a> .twitter { border:3px

    solid #000; padding:10px 20px; color:#fff; border-radius:10px; background:red; text-decoration: none; } .facebook { border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; background:blue; text-decoration: none; } .twitter,.facebook{ border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; text-decoration: none; } .twitter { background:red; } .facebook { background:blue; }
  24.  Sass+OOCSS <a href="#" class=“btn btn-twitter”>Twitter</a> <a href="#" class=“btn btn-facebook”>Facebook</a> .btn{

    border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; text-decoration: none; } .btn-twitter { background:red; } .btn-facebook { background:blue; }
  25.  Sass+OOCSS <a href="#" class=“btn-twitter”>Twitter</a> <a href="#" class=“btn-facebook”>Facebook</a> .btn-twitter { background:

    red; border: 3px solid #000; padding: 10px 20px; color: #fff; border-radius: 10px; text-decoration: none; } .btn-facebook { background: blue; border: 3px solid #000; padding: 10px 20px; color: #fff; border-radius: 10px; text-decoration: none; } @mixin btn{ border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; text-decoration: none; } .btn-twitter { background:red; @include btn; } .btn-facebook { background:blue; @include btn; } Sass CSS
  26.  Sass+OOCSS <a href="#" class=“btn-twitter”>Twitter</a> <a href="#" class=“btn-facebook”>Facebook</a> .btn, .btn-twitter, .btn-facebook

    { border: 3px solid #000; padding: 10px 20px; color: #fff; border-radius: 10px; text-decoration: none; } .btn-twitter { background: red; } .btn-facebook { background: blue; } .btn{ border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; text-decoration: none; } .btn-twitter { background:red; @extend .btn; } .btn-facebook { background:blue; @extend .btn; } Sass CSS
  27.  Sass+OOCSS <a href="#" class=“btn-twitter”>Twitter</a> <a href="#" class=“btn-facebook”>Facebook</a> .btn-twitter, .btn-facebook {

    border: 3px solid #000; padding: 10px 20px; color: #fff; border-radius: 10px; text-decoration: none; } .btn-twitter { background: red; } .btn-facebook { background: blue; } %btn{ border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; text-decoration: none; } .btn-twitter { background:red; @extend %btn; } .btn-facebook { background:blue; @extend %btn; } Sass CSS
  28.  Sass+OOCSS <a href="#" class=“btn-twitter”>Twitter</a> <a href="#" class=“btn-facebook”>Facebook</a> .btn-twitter, .btn-facebook {

    border: 3px solid #000; padding: 10px 20px; color: #fff; border-radius: 10px; text-decoration: none; } .btn-twitter { background: red; } .btn-facebook { background: blue; } $vars:( prefix-class: btn, twitter: red, facebook: blue ); %btn{ border:3px solid #000; padding:10px 20px; color:#fff; border-radius:10px; text-decoration: none; } .#{map-get($vars,prefix-class)}-twitter { background:map-get($vars,twitter); @extend %btn; } .#{map-get($vars,prefix-class)}-facebook { background:map-get($vars,facebook); @extend %btn; } Sass CSS
  29.  Sass+OOCSS 参考资料: http:/ /www.w3cplus.com/preprocessor/using-object-oriented-css-with-sass.html http:/ /www.w3cplus.com/preprocessor/how-to-scale-and-maintain-legacy-css-with-sass-and-smacss.html http:/ /www.w3cplus.com/preprocessor/oocss-plus-sass-is-the-best-way-to-css.html http:/ /www.w3cplus.com/preprocessor/oocss-sass.html

    http:/ /www.w3cplus.com/preprocessor/bem-modifiers-multiple-classes-vs-extend.html http:/ /www.w3cplus.com/preprocessor/pushing-bem-to-the-next-level-with-sass-3-4.html http:/ /www.w3cplus.com/css/introducing-am-css.html http:/ /www.w3cplus.com/preprocessor/sass-component-10-minutes.html
  30.  编写Sass技巧 管理好⽂文件件结构 有效使用Sass变量 减少对混合宏的依赖 拥抱%placeholder 用@function做更多事情 合理嵌套 保持Sass的简单 参考资料: http:/

    /www.w3cplus.com/preprocessor/8-tips-help-get-best-sass.html http:/ /www.w3cplus.com/preprocessor/how-to-create-project-with-sass.html http:/ /www.w3cplus.com/preprocessor/keep-sass-simple.html http:/ /www.w3cplus.com/preprocessor/tips-help-level-up-sass.html http:/ /www.w3cplus.com/preprocessor/whats-great-sass.html http:/ /www.w3cplus.com/preprocessor/why-sass.html