SlideShare a Scribd company logo
1 of 49
Sass带来的变革 
By @大漠
Sass是什么? 
工具语言处理器 
CSS预处理器语言
为什么选择Sass? 
功能强 
资料多 
案例多
Sass功能与特性 
定义变量 
混合宏与占位符 
逻辑判断 
函数 
Maps 
…
Sass学习路线 
环境 
安装 
语法 
编译 
特性 
组织 
初级中级高级 
实战 
调试 
定义功能 
封装插件 
编写框架
Sass环境 
自带Ruby 
需要安装Ruby 
参考资料: 
https://www.ruby-lang.org/en/installation/ 
http://www.w3cplus.com/sassguide/install.html 
http://www.w3cplus.com/css/css-preprocessor-sass-vs-less-stylus-2.html
Sass安装 
$ gem install sass 
$ sudo gem install sass 
$ sass -v 
Sass 3.4.7 (Selective Steve) 
http://rubygems.org/gems/sass
Sass语法 
$width: 300px; 
.container { 
width: $width; 
} 
Sass 
$width: 300px 
.container 
width: $width 
SCS 
S
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
Sass调试 
参考资料: 
http://www.w3cplus.com/preprocessor/sass-debug-with-developer-tool.html 
http://www.w3cplus.com/preprocessor/sass-3.3-source-maps.html 
http://www.w3cplus.com/tools/source-maps-101.html
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 
继承
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
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); 
} 
} 
混合宏代码块 
使用@include 
.box { 
引用混合宏@include box-shadow; 
调用默认混合宏 
} 
.box { 
@include box-shadow(inset 1px 1px 2px rgba(black,.3)); 
} 
调用带参数的混合宏
Sass特性之继承 
.mt5{ 
margin-top:5px; 
} 
.block{ 
@extend .mt5; 
} 
.mt5, .block { 
margin-top: 5px; 
} 
Sass 
Sass 
CSS 
@extend来继承类名
Sass特性之占位符 
%mt5{ 
margin-top:5px; 
} 
.block{ 
@extend %mt5; 
} 
.block { 
margin-top: 5px; 
} 
Sass 
Sass 
CSS 
使用%声明占位符 
@extend来扩展
混合宏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
混合宏vs 继承vs占位 
符 
那么问题来了? 
哪家技术强?
混合宏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
单类vs 多类
单类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;} 
.header { 
<div class="header"></div> 
@extend %paxs; 
@extend %maxs; 
} 
.header { 
padding:5px; 
margin:5px; 
} 
CSS 
HTML 
Sass 
Sass 
CSS 
HTML
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
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
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; 
} 
Sass 
CSS 
参考资料: 
http://www.w3cplus.com/preprocessor/sass-other-function.html 
http://www.w3cplus.com/preprocessor/sass-color-function.html 
Sass 
CSS
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
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
Sass特性之控制命 
令 
@if @for @while @each 
参考资料: 
http://www.w3cplus.com/preprocessor/Sass-control-directives-if-for-each-while.html 
http://www.w3cplus.com/preprocessor/sass-advanced-application.html 
http://www.w3cplus.com/preprocessor/getting-into-sass-control-directives.html
控制命令之@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
控制命令之@for 
@for $var from <start> through <end> 
$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> to <end> 
.green-1 { 
color: #006700; 
} 
.green-2 { 
color: #004d00; 
} 
.green-3 { 
color: #003400; 
} 
CSS 
$color: green !default; 
@for $i from 1 to 4 { 
.#{$color}-#{$i}{ 
color: darken($color,5% * $i); 
} 
} 
Sass
控制命令之@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>
控制命令之@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
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
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
如何在项目中使用 
Sass
组织文件
组织文件 
参考资料: 
http://www.w3cplus.com/preprocessor/organize-that-sass.html 
http://www.w3cplus.com/preprocessor/architecture-sass-project.html 
http://www.w3cplus.com/preprocessor/beginner/how-to-structure-a-sass-project.html 
http://www.w3cplus.com/preprocessor/converting-your-css-project-to-sass.html
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; 
}
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; 
}
Sass+OOCSS 
Sass将带来怎样的变革
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
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
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
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
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
编写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
学习Sass案例
学习Sass资料
Q & A
Thank You!!!

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Sass带来的变革

  • 2.
  • 6. Sass学习路线 环境 安装 语法 编译 特性 组织 初级中级高级 实战 调试 定义功能 封装插件 编写框架
  • 7. Sass环境 自带Ruby 需要安装Ruby 参考资料: https://www.ruby-lang.org/en/installation/ http://www.w3cplus.com/sassguide/install.html http://www.w3cplus.com/css/css-preprocessor-sass-vs-less-stylus-2.html
  • 8. Sass安装 $ gem install sass $ sudo gem install sass $ sass -v Sass 3.4.7 (Selective Steve) http://rubygems.org/gems/sass
  • 9. Sass语法 $width: 300px; .container { width: $width; } Sass $width: 300px .container width: $width SCS S
  • 10. 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
  • 11. Sass调试 参考资料: http://www.w3cplus.com/preprocessor/sass-debug-with-developer-tool.html http://www.w3cplus.com/preprocessor/sass-3.3-source-maps.html http://www.w3cplus.com/tools/source-maps-101.html
  • 12. 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 继承
  • 13. 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
  • 14. 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); } } 混合宏代码块 使用@include .box { 引用混合宏@include box-shadow; 调用默认混合宏 } .box { @include box-shadow(inset 1px 1px 2px rgba(black,.3)); } 调用带参数的混合宏
  • 15. Sass特性之继承 .mt5{ margin-top:5px; } .block{ @extend .mt5; } .mt5, .block { margin-top: 5px; } Sass Sass CSS @extend来继承类名
  • 16. Sass特性之占位符 %mt5{ margin-top:5px; } .block{ @extend %mt5; } .block { margin-top: 5px; } Sass Sass CSS 使用%声明占位符 @extend来扩展
  • 17. 混合宏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
  • 18. 混合宏vs 继承vs占位 符 那么问题来了? 哪家技术强?
  • 19. 混合宏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
  • 21. 单类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;} .header { <div class="header"></div> @extend %paxs; @extend %maxs; } .header { padding:5px; margin:5px; } CSS HTML Sass Sass CSS HTML
  • 22. 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
  • 23. 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
  • 24. 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; } Sass CSS 参考资料: http://www.w3cplus.com/preprocessor/sass-other-function.html http://www.w3cplus.com/preprocessor/sass-color-function.html Sass CSS
  • 25. 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
  • 26. 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
  • 27. Sass特性之控制命 令 @if @for @while @each 参考资料: http://www.w3cplus.com/preprocessor/Sass-control-directives-if-for-each-while.html http://www.w3cplus.com/preprocessor/sass-advanced-application.html http://www.w3cplus.com/preprocessor/getting-into-sass-control-directives.html
  • 28. 控制命令之@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
  • 29. 控制命令之@for @for $var from <start> through <end> $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> to <end> .green-1 { color: #006700; } .green-2 { color: #004d00; } .green-3 { color: #003400; } CSS $color: green !default; @for $i from 1 to 4 { .#{$color}-#{$i}{ color: darken($color,5% * $i); } } Sass
  • 30. 控制命令之@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>
  • 31. 控制命令之@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
  • 32. 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
  • 33. 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
  • 36. 组织文件 参考资料: http://www.w3cplus.com/preprocessor/organize-that-sass.html http://www.w3cplus.com/preprocessor/architecture-sass-project.html http://www.w3cplus.com/preprocessor/beginner/how-to-structure-a-sass-project.html http://www.w3cplus.com/preprocessor/converting-your-css-project-to-sass.html
  • 37. 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; }
  • 38. 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; }
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 编写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
  • 48. Q & A