afeichuanqi 2019-07-01
<br/>
Sass is the most mature, stable, and powerful professional grade CSS extension language in the world.<!-- markdown-to-slides index.md -o index.html -s slide.css -->
是 CSS 的扩展,是 CSS 的预处理。提供了许多便利的写法,大大节省了设计者的时间,使得 CSS 的开发,变得简单和可维护。
sass 文件有两种文件名后缀,分别是 .sass 和 .scss,.sass 是严格的嵌套缩进规则,而 .scss 的则是跟写 css 代码类似的大括号,分号这样的语法规则。
CSS 预处理器技术已经非常的成熟,而且也涌现出了很多种不同的 CSS 预处理器语言,比如说:
Sass(SCSS)LESSStylusTurbineSwithch CSSCSS CacheerDT CSS到目前为止,在众多优秀的 CSS 预处理器语言中就属 Sass、LESS 和 Stylus 最优秀,讨论的也多,对比的也多。
Sass 和 SCSS 其实是同一种东西,我们平时都称之为 Sass,两者之间不同之处有以下两点:
文件扩展名不同,Sass 是以“.sass”后缀为扩展名,而 SCSS 是以“.scss”后缀为扩展名
语法书写方式不同,Sass 是以严格的缩进式语法规则来书写,不带大括号({})和分号(;),而 SCSS 的语法书写和我们的 CSS 语法书写方式非常类似。
$font-stack: Helvetica, sans-serif //定义变量 $primary-color: #333 //定义变量 body font: 100% $font-stack color: $primary-color
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}npm install sass -g
sass test.scss [test.css]
sass --style compressed test.sass test.css
你也可以让 SASS 监听某个文件或目录,一旦源文件有变动,就自动生成编译后的版本。
sass --watch input.scss:output.css // watch a file sass --watch app/sass:public/stylesheets // watch a directory
Sass 和 JavaScript 语言类似,也具有自己的数据类型,在 Sass 中包含以下几种数据类型:
数字: 如,1、 2、 13、 10px;字符串:有引号字符串或无引号字符串,如,"foo"、 'bar'、 baz;颜色:如,blue、 #04a3f9、 rgba(255,0,0,0.5);布尔型:如,true、 false;空值:如,null;值列表:用空格或者逗号分开,如,1.5em 1em 0 2em 、 Helvetica, Arial, sans-serif。有引号字符串 (quoted strings),如 "Lucida Grande" 、'';无引号字符串 (unquoted strings),如 sans-serifbold。使用 #{ }插值语句 (interpolation) 时,有引号字符串将被编译为无引号字符串,这样方便了在混合指令 (mixin) 中引用选择器名。
@mixin firefox-message($selector) {
body.firefox #{$selector}:before {
content: 'Hi, Firefox users!';
}
}body.firefox .header:before {
content: 'Hi, Firefox users!';
}$blue: #1875e7;
div {
color: $blue;
}$side: left;
.rounded {
border-#{$side}-radius: 5px;
}sass 的默认变量一般是用来设置默认值,然后根据需求来覆盖的,覆盖的方式也很简单,只需要在默认变量之前重新声明下变量即可。
$fontSize: 14px;
$fontSize: 12px !default;
body {
font-size: $fontSize;
}
/*普通变量与默认变量*/
body {
font-size: 14px;
}从 3.4 版本开始,Sass 已经可以正确处理作用域的概念
$color: orange !default; //定义全局变量(在选择器、函数、混合宏...的外面定义的变量为全局变量)
.block {
color: $color; //调用全局变量
}
em {
$color: red; //定义局部变量
a {
color: $color; //调用局部变量
}
}
span {
color: $color; //调用全局变量
}.block {
color: orange;
}
em a {
color: red;
}
span {
color: orange;
}当在局部范围(选择器内、函数内、混合宏内...)声明一个已经存在于全局范围内的变量时,局部变量就成为了全局变量的影子。基本上,局部变量只会在局部范围内覆盖全局变量。
该值至少重复出现了两次;该值至少可能会被更新一次;该值所有的表现都与变量有关(非巧合)。基本上,没有理由声明一个永远不需要更新或者只在单一地方使用变量。
body {
margin: (14px/2);
top: 50px + 100px;
}.box {
width: 20px + 1em;
}
$full-width: 960px;
.content {
width: $full-width - 1em;
}
// Incompatible units: 'em' and ‘px'.”.box {
width: 10px * 2;
// width: 10px * 2px 错误;
}在 Sass 中做除法运算时,直接使用“/”符号做为除号时,将不会生效,编译时既得不到我们需要的效果,也不会报错。
这样的结果对于大家来说没有任何意义。要修正这个问题,只需要给运算的外面添加一个小括号()
如果数值或它的任意部分是存储在一个变量中或是函数的返回值。如果数值被圆括号包围。如果数值是另一个数学表达式的一部分。p {
font: 10px/8px; // 纯 CSS,不是除法运算
height: (16px/8px);
$width: 1000px;
width: $width/2; // 使用了变量,是除法运算
width: round(1.5) / 2; // 使用了函数,是除法运算
height: (500px/2); // 使用了圆括号,是除法运算
margin-left: 5px + 8px/2px; // 使用了加(+)号,是除法运算
}p {
font: 10px/8px;
height: 2;
width: 500px;
width: 1;
height: 250px;
margin-left: 9px;
}<h5>除法运算时,如果两个值带有相同的单位值时,除法运算之后会得到一个不带单位的数值。</h5>
所有算数运算都支持颜色值,并且是分段运算的。也就是说,红、绿和蓝各颜色分段单独进行运算。
p {
color: #010203 + #040506; // 01+04 02+05 03+06
// 考虑颜色函数 未来版本不支持
}在 Sass 中可以通过加法符号“+”来对字符串进行连接。可以连接变量和和字符
$content: 'Hello' + '' + 'Sass!';
.box:before {
content: ' #{$content} ';
}
div {
cursor: 'e' + -resize;
position: re + 'lative';
}.box:before {
content: ' HelloSass! ';
}
div {
cursor: 'e-resize';
position: relative;
}div {
hi {
color: red;
}
}sass 的嵌套分为 3 种:选择器嵌套、属性嵌套、伪类嵌套
nav {
a {
color: red;
.b {
& .c {
font-size: 12px;
}
&:hover {
color: green;
}
}
head & {
color: green;
}
}
}nav a {
color: red;
}
nav a .b .c {
font-size: 12px;
}
nav a .b:hover {
color: green;
}
head nav a {
color: green;
}CSS 有一些属性前缀相同,只是后缀不一样,比如:border-top/border-right,与这个类似的还有 margin、padding、font 等属性。
.box {
font: {
size: 12px;
weight: bold;
}
}.box {
font-size: 12px;
font-weight: bold;
}.box {
&:before {
content: '伪元素嵌套';
}
}选择器嵌套最大的问题是将使最终的代码难以阅读,我们应该尽可能避免选择器嵌套。
标准的 CSS 注释 /_ comment _/ ,会保留到编译后的文件。
单行注释 // comment,只保留在 SASS 源文件中,编译后被省略。
在/*后面加一个感叹号,表示这是"重要注释"。即使是压缩模式编译,也会保留这行注释,通常可以用于声明版权信息。
SASS 允许一个选择器,继承另一个选择器。比如,现有 class1:
.btn {
border: 1px solid #ccc;
padding: 6px 10px;
font-size: 14px;
}.btn-primary {
background-color: #f36;
color: #fff;
@extend .btn;
}
.btn-default {
background-color: orange;
color: #fff;
@extend .btn;
}.btn,
.btn-primary,
.btn-default {
border: 1px solid #ccc;
padding: 6px 10px;
font-size: 14px;
}
.btn-primary {
background-color: #f36;
color: #fff;
}
.btn-default {
background-color: orange;
color: #fff;
}需要重复使用大段的样式时,使用变量就无法达到我们目目的,这时候可以使用@mixin,定义一个代码块。
@mixin left {
float: left;
margin-left: 10px;
}div {
@include left;
}div {
@include left(20px);
}// 在属性中取值需要使用 #{}
@mixin rounded($vert, $horz, $radius: 10px) {
border-#{$vert}-#{$horz}-radius: $radius;
-moz-border-radius-#{$vert}#{$horz}: $radius;
-webkit-border-#{$vert}-#{$horz}-radius: $radius;
}
#navbar li {
@include rounded(top, left);
}
#footer {
@include rounded(top, left, 5px);
}@mixin left($value: 10px) {
float: left;
margin-right: $value;
}@mixin center($width, $height) {
width: $width;
height: $height;
position: absolute;
top: 50%;
left: 50%;
margin-top: -($height) / 2;
margin-left: -($width) / 2;
}
.box-center {
@include center(500px, 300px);
}.box-center {
width: 500px;
height: 300px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -250px;
}有一个特别的参数“…”。当混合宏传的参数过多之时,可以使用参数来替代,如:
@mixin box-shadow($shadows...) {
@if length($shadows) >= 1 {
-webkit-box-shadow: $shadows;
box-shadow: $shadows;
} @else {
$shadows: 0 0 2px rgba(#000, 0.25);
-webkit-box-shadow: $shadow;
box-shadow: $shadow;
}
}
.box {
@include box-shadow(0 0 1px rgba(#000, 0.5), 0 0 2px rgba(#000, 0.2));
}
.box1 {
@include box-shadow();
}.box {
-webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
}
.box1 {
-webkit-box-shadow: 0 0 2px rgba(0, 0, 0, 0.25);
box-shadow: 0 0 2px rgba(0, 0, 0, 0.25);
}混合宏在实际编码中给我们带来很多方便之处,特别是对于复用重复代码块。但其最大的不足之处是会生成冗余的代码块。比如在不同的地方调用一个相同的混合宏时。
@mixin border-radius {
-webkit-border-radius: 3px;
border-radius: 3px;
}
.box {
@include border-radius;
margin-bottom: 5px;
}
.btn {
@include border-radius;
}.box {
-webkit-border-radius: 3px;
border-radius: 3px;
margin-bottom: 5px;
}
.btn {
-webkit-border-radius: 3px;
border-radius: 3px;
}并不能智能的将相同的样式代码块合并在一起。这也是 Sass 的混合宏最不足之处。
他可以取代以前 CSS 中的基类造成的代码冗余的情形。因为 %placeholder 声明的代码,如果不被 @extend 调用的话,不会产生任何代码。
%mt5 {
// 没有被@extent调用就不会被编译到css中
margin-top: 5px;
}
%pt5 {
padding-top: 5px;
}
.btn {
@extend %mt5;
@extend %pt5;
}通过 @extend 调用的占位符,编译出来的代码会将相同的代码合并在一起。
<img src="./sass_reuse.jpg" />