[关闭]
@Dreamingboy 2017-05-29T02:03:59.000000Z 字数 6397 阅读 1037

sass入门篇

sass


sass 初步认识

sass是一个利用变成思想来编写CSS代码和实现代码规范化的工具

sass和scss

sass和scss是同样的一个东西,只不过两者的文件名的后缀和书写方式不一样。

sass的语法格式

1、老版本的sass使用的是缩进的方式来编写

  1. $background-color:red;
  2. $font:16px;
  3. body
  4. font:$font
  5. background-color:$background-color

老版本的sass格式的sass文件以.sass为后缀,遵循严格的缩进格式。

2、新版本的sass语法格式和CSS一样

  1. $background-color:red;
  2. $font:16px;
  3. body{
  4. background-color:$background-color;
  5. font:$font;
  6. }

新版本的sass语法格式的sass文件以.scss为后缀,推荐使用新版本的sass语法格式

sass编译

sass这是类似于规定编译CSS的命令一样,在问价里面为编译CSS文件写好各种规定,但是实际项目中用的还是CSS文件,因此,需要编译工具。
1、命令编译
命令编译就是使用电脑的终端,输入sass指令来进行sass的编译。

  1. sass<要编译的sass的文件的路径>/style.scss:<CSS文件要存储的路径>/style.css
  2. sass style.scss:D:hope/sass/css/style.css
  1. sass sass/:css/

表示将项目中的“sass”文件夹中的".scss"或者是".sass"文件编译成".css"文件,并且将这些文件存放在项目中的"css"文件夹中。

实际编译过程中可以通过开启watch来检测代码的变化

  1. sass --watch<要编译的sass的文件的路径>/style.scss:<CSS文件要存储的路径>/style.css

2、GUI工具编译
为了方便,可以使用一些GUI工具辅助编译,也就是自动化编译。

3、自动化编译
常见的工具有:

编译过程中常见的错误是字符编译的错误,因此建议在最前面加上“utf-8”

不同的样式输出风格

在输出指令的最后加上 "--style 要输出的风格"

  1. sass --watch test.scss:test.css --style nested
  2. body {
  3. color: red;
  4. width: 1200px;
  5. height: 400px; }
  1. sass --watch test.scss:test.css --style expanded
  2. body {
  3. color: red;
  4. width: 1200px;
  5. height: 400px;
  6. }
  1. sass --watch test.scss:test.css --style nested
  2. body {color: red;width: 1200px;height: 400px; }
  1. sass --watch test.scss:test.css --style nested
  2. body {color: red;width: 1200px;height: 400px; }div{color: red;width: 1200px;height: 400px;}

sass变量

1、定义变量:$+name+value
最简单的变量四直接存贮有样式的变量,如:

  1. $width:200px
  2. $height:300px
  3. body{
  4. width:$width;
  5. height:$height;
  6. }
  7. //编译后的CSS样式
  8. body{
  9. width:200px;
  10. height:300px;
  11. }

2、变量类型

  1. $width:1200px;
  2. body{
  3. width:$width;
  4. }
  1. $height:200px !default;
  2. body{
  3. height:$height;
  4. }
  5. //css
  6. body{
  7. height:200px;
  8. }
  9. //默认变量覆盖
  10. $height:300px;
  11. $height:200px !default;
  12. body{
  13. height:$height;
  14. }
  15. //css
  16. body{
  17. height:300px;
  18. }

3、局部变量和全局变量

  1. $color:red;//全局变量
  2. .block{
  3. color:$color;
  4. }
  1. body{
  2. $color:red;
  3. a{
  4. color:$color;//调用局部变量
  5. }
  6. }

sass 嵌套

1、选择器嵌套
直接使用选择器进行嵌套。

  1. <div>
  2. <ul>
  3. <li></li>
  4. <li></li>
  5. <li></li>
  6. </ul>
  7. </div>
  8. //sass
  9. div{
  10. li{
  11. color:red;
  12. ul & {
  13. color:green;
  14. }
  15. }
  16. }
  17. //css
  18. div li{
  19. color:red;
  20. }
  21. div ul li{
  22. color:green;
  23. }

2、属性嵌套
对于一些开头一样但是后缀不一样的属性可以使用属性嵌套。比如border-top、border-bottom等

  1. div{
  2. border:{
  3. top:1px solid green;
  4. bottom:1px solid red;
  5. }
  6. }
  7. //css
  8. div {
  9. border-top: 1px solid green;
  10. border-bottom: 1px solid red;
  11. }

3、伪类选择器
伪类选择器借助&实现嵌套

  1. div{
  2. &:after,
  3. &:before{
  4. content:"";
  5. width:12px;
  6. height:2px;
  7. color:red;
  8. }
  9. &:after{
  10. content:"";
  11. border-top:1px solid green;
  12. }
  13. }
  14. //css
  15. div:after, div:before {
  16. content: "";
  17. width: 12px;
  18. height: 2px;
  19. color: red;
  20. }
  21. div:after {
  22. content: "";
  23. border-top: 1px solid green;
  24. }

混合宏

混合宏适用于在需要重复使用大段的样式的时候使用

1、声明混合宏
使用@mixin来声明混合宏

  1. @mixin border-radius{
  2. -webkit-border-radius:5px;
  3. border-radius:5px;
  4. }

@mixin声明混合宏的关键字,类似CSS中的@media,上面的代码中,border-radius是混合宏的名字,大括号里面是重复的代码片段。

2、带参数的混合宏

  1. @mixin border-radius($radius:5px){
  2. webkit-border-radius:$radius;
  3. border-radius:$radius;
  4. }

3、调用混合宏
@include用于调用声明好的混合宏

  1. @mixin border-radius($a:5px){
  2. webkit-border-radius:$a;
  3. border-radius:$a;
  4. }
  5. div{
  6. @include border-radius;
  7. }
  8. //css
  9. div {
  10. webkit-border-radius: 5px;
  11. border-radius: 5px;
  12. }

4、混合宏的参数

  1. @mixin border-radius($a){
  2. webkit-border-radius:$a;
  3. border-radius:$a;
  4. }
  5. div{
  6. @include border-radius(5px);
  7. }
  8. //css
  9. div {
  10. webkit-border-radius: 5px;
  11. border-radius: 5px;
  12. }
  1. @mixin border-radius($a:5px){
  2. webkit-border-radius:$a;
  3. border-radius:$a;
  4. }
  5. div{
  6. @include border-radius;
  7. }
  8. //css
  9. div {
  10. webkit-border-radius: 5px;
  11. border-radius: 5px;
  12. }
  1. @mixin example($width,$height){
  2. width:$width;
  3. height:$height;
  4. }
  5. div{
  6. @include example(1200px,300px);
  7. }
  8. //css
  9. div {
  10. width: 1200px;
  11. height: 300px;
  12. }

"..."参数:当混合宏的参数很多的时候,可以使用这个参数来代替

  1. @mixin box-shadow($shadows...){
  2. @if length($shadows)>= 1{
  3. -webkit-box-shadow:$shadow;
  4. box-shadow:$shadow;
  5. } @else {
  6. $shadow:0 0 2px rgba(#000,.25);
  7. -webkit-box-shadow:$shadow;
  8. box-shadow:$shadow;
  9. }
  10. }
  11. .div{
  12. @include box-shadow(0 0 1px rgba(#000,.5),0 0 2px rgba(#000,.2));
  13. }
  14. //css
  15. .div {
  16. -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
  17. box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
  18. }

混合宏并不会将重复的代码只能合并,而是留下冗余的代码

继承

@extend:使用@extend对某个元素的样式进行继承

  1. .div1{
  2. width:1200px;
  3. height:300px;
  4. border:1px solid green;
  5. }
  6. .box{
  7. @extend .div1;
  8. color:red;
  9. font-size:14px;
  10. }
  11. //css
  12. .div1, .box {
  13. width: 1200px;
  14. height: 300px;
  15. border: 1px solid green;
  16. }
  17. .box {
  18. color: red;
  19. font-size: 14px;
  20. }

占位符

占位符可以取代以前的CSS的基类造成的代码冗余的情形,但是%声明的代码如果不被@extend调用的话不会产生任何代码。

  1. %a{
  2. padding-top:10px;
  3. }
  4. %b{
  5. margin-top:20px;
  6. }
  7. .div3{
  8. @extend %a;
  9. }
  10. .div4{
  11. @extend %a;
  12. @extend %b;
  13. }
  14. div2{
  15. @extend %b
  16. }
  17. //css
  18. .div3, .div4 {
  19. padding-top: 10px;
  20. }
  21. .div4, div2 {
  22. margin-top: 20px;
  23. }

需要注意的是占位符根据的是属性对选择器进行归类

  1. %a{
  2. padding-top:10px;
  3. }
  4. %b{
  5. margin-top:20px;
  6. }
  7. .div3{
  8. @extend %a;
  9. @extend %b;
  10. }
  11. //css
  12. .div3 {
  13. padding-top: 10px;
  14. }
  15. .div3 {
  16. margin-top: 20px;
  17. }

插值

#{}:提供选择器和属性名称中的sassscript变量。在花括号中指定变量和属性值。

  1. $prop:margin;
  2. @mixin set-value($side,$value){
  3. #{$prop}-#{$side}:$value;
  4. }
  5. .div4{
  6. @include set-value(top,12px);
  7. }
  8. //css
  9. .div4 {
  10. margin-top: 12px;
  11. }

可以使用#{}构建一个选择器

  1. @mixin set-value($class,$small, $medium,$big){
  2. .#{$class}-top{font-size:$small;}
  3. .#{$class}-left{font-size:$medium;}
  4. .#{$class}-bottom{font-size:$big;}
  5. }
  6. @include set-value(slider-text,12px,13px,14px);
  7. //css
  8. .slider-text-top {
  9. font-size: 12px;
  10. }
  11. .slider-text-left {
  12. font-size: 13px;
  13. }
  14. .slider-text-bottom {
  15. font-size: 14px;
  16. }

sass 注释

1、"/* */":注释不会再CSS文件中显示
2、"//":注释会在CSS文件显示

数据类型

1、数字:包括纯数字和带有单位的数字,如:1,2,3,14px
2、字符串:包括有引号字符串和没有引号字符串,"head"、'bar'、doit
3、颜色值
4、布尔值
5、空值
6、列表值:用空额或者是逗号分开。如:1.5em 3em 4em ;Helvetica,Arial,sans

字符串

1、有引号的字符串:如:“Lucida Grande”
2、无引号字符串,如:sans-seribold

只有在使用插值的时候会将引号字符编译为无引号的字符,其余情况下字符串的类型不会被改变。

值列表

值列表:margin:10px 15px 0px 0px;或者font-family:Helvetica,Arial,sans-serif

sass列表函数

值列表可以再包含值列表:1px 2px,3px 4px
内外两层值列表使用相同的分割方式,要用圆括号包裹内层:(1px 2px)(3px 4px)
值列表包含空的值列表或者是空值的时候编译将消除空值

加法和减法

可以再变量和属性中做加法运算,但是单位要一致

  1. .box{width:20px+8in;}
  2. //css
  3. .box{width:788px;}
  4. $a:300px;
  5. $b:400px;
  6. .box{width:$a+$b;}
  7. //css
  8. .box{width:700px;}

乘法

乘法用*表示,乘法中只能有一个值带有单位,不支持同时出现种单位

  1. $a:2px;
  2. $b:3;
  3. ul{width:$a*$b;}
  4. //css
  5. ul {
  6. width: 6px;
  7. }

除法

1、纯数字除法运算时,需要加括号

  1. .box{ width:(100px/2);}

2、"/"被当成是除号的情况

3、当两个值具有相同的单位时,得到一个不带单位的值。

数字运算

sass中的数字运算和数学运算一样

  1. .box1{
  2. width:((220px + 720px) - 11*20)/12;
  3. }
  4. //css
  5. .box2{width:60px;}

颜色运算

1、颜色的运算是分段式运算,红绿蓝各段分别进行各自的运算。

  1. p{
  2. color:#010203 + #040506;
  3. }
  4. p{
  5. color:#010203*2;
  6. }
  7. //csss
  8. p{ color:#050709;}
  9. p{ color:#020406;}

字符串运算

直接使用“+”将字符串进行连接,可以直接连接也可以使用变量进行连接。

两种比较特殊的结果:

  1. p:before {
  2. content: "Foo " + Bar;
  3. font-family: sans- + "serif";
  4. }
  5. //css
  6. p:before {
  7. content: "Foo Bar";
  8. font-family: sans-serif; }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注