sass的文件后缀
sass文件后缀有两种即sass和scss
sass:不使用大括号和分号
scss:使用大括号和分号
| 12
 3
 4
 5
 6
 
 | //sass语法body
 background: #eee
 font-size:12px
 p
 background: #0982c1
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 
 | body {
 background: #eee;
 font-size:12px;
 p{
 background: #0982c1;
 }
 }
 
 | 
文件导入
1.导入sass文件可以省略文件后缀
2.导入sass文件会在编译是合并为一个文件
3._base.scss导入时可以不写下划线即可以@import base
4.导入css文件以@import方式存在,跟普通CSS导入样式文件一样
| 12
 3
 4
 
 | body {
 background: #eee;
 }
 
 | 
| 12
 3
 4
 5
 6
 
 | @import "reset.css";
 @import "a";
 p{
 background: #0982c1;
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 
 | @import "reset.css";
 body {
 background: #eee;
 }
 p{
 background: #0982c1;
 }
 
 | 
注释
/**/和//
单行注释不会输入到编译产生的CSS中
| 12
 3
 4
 5
 6
 
 | 
 
 body {
 color: red;
 }
 
 | 
变量
普通变量
普通变量定义之后可以在全局范围内使用。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | $fontSize: 12px;
 body{
 font-size:$fontSize;
 }
 
 
 body{
 font-size:12px;
 }
 
 | 
默认变量
sass的默认变量仅需要在值后面加上!default即可。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | $baseLineHeight:        2;
 $baseLineHeight:        1.5 !default;
 body{
 line-height: $baseLineHeight;
 }
 
 
 body{
 line-height:2;
 }
 
 | 
特殊变量
如果变量作为属性或在某些特殊情况下等则必须要以#{$variables}形式使用。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | $borderDirection:       top !default;
 $baseFontSize:          12px !default;
 $baseLineHeight:        1.5 !default;
 
 
 .border-#{$borderDirection}{
 border-#{$borderDirection}:1px solid #ccc;
 }
 
 body{
 font:#{$baseFontSize}/#{$baseLineHeight};
 }
 
 
 .border-top{
 border-top:1px solid #ccc;
 }
 body {
 font: 12px/1.5;
 }
 
 | 
多值变量
多值变量分为list类型和map类型,list类型有点像js中的数组,而map类型有点像js中的对象
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | 
 
 
 
 
 $linkColor:  #08c #333 !default;
 a{
 color:nth($linkColor,1);
 &:hover{
 color:nth($linkColor,2);
 }
 }
 
 
 a{
 color:#08c;
 }
 a:hover{
 color:#333;
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | 
 $headings: (h1: 2em, h2: 1.5em, h3: 1.2em);
 @each $header, $size in $headings {
 #{$header} {
 font-size: $size;
 }
 }
 
 
 h1 {
 font-size: 2em;
 }
 h2 {
 font-size: 1.5em;
 }
 h3 {
 font-size: 1.2em;
 }
 
 | 
嵌套
选择器的嵌套和属性的嵌套
选择器嵌套
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 
 | 
 #top_nav{
 background-color:#333;
 li{
 float:left;
 }
 a{
 color: #fff;
 &:hover{
 color:#ddd;
 }
 }
 }
 
 
 #top_nav{
 background-color:#333;
 }
 #top_nav li{
 float:left;
 }
 #top_nav a{
 color: #fff;
 }
 #top_nav a:hover{
 color:#ddd;
 }
 
 | 
属性嵌套
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | .fakeshadow {
 border: {
 style: solid;
 left: {
 color: #888;
 }
 right: {
 color: #ccc;
 }
 }
 }
 
 
 .fakeshadow {
 border-style: solid;
 border-left-color: #888;
 border-right-color: #ccc;
 }
 
 | 
混合(mixin)
无参数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | @mixin center-block {
 margin:0 auto;
 }
 .demo{
 @include center-block;
 }
 
 
 .demo{
 margin:0 auto;
 }
 
 | 
有参数mixin
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | 
 @mixin opacity($opacity:50) {
 opacity: $opacity / 100;
 filter: alpha(opacity=$opacity);
 }
 .opacity{
 @include opacity;
 }
 .opacity-80{
 @include opacity(80);
 }
 
 .opacity {
 opacity: 0.5;
 filter: alpha(opacity=50);
 }
 .opacity-80 {
 opacity: 0.8;
 filter: alpha(opacity=80);
 }
 
 | 
多个参数mixin
调用时可直接传入值,如@include传入参数的个数小于@mixin定义参数的个数,则按照顺序表示,后面不足的使用默认值,如不足的没有默认值则报错。除此之外还可以选择性的传入参数,使用参数名与值同时传入。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | @mixin horizontal-line($border:1px dashed #ccc, $padding:10px){
 border-bottom:$border;
 padding-top:$padding;
 }
 .imgtext-h li{
 @include horizontal-line(1px solid #ccc);
 }
 .imgtext-h--product li{
 @include horizontal-line($padding:15px);
 }
 
 
 .imgtext-h li {
 border-bottom: 1px solid #cccccc;
 padding-top: 10px;
 }
 .imgtext-h--product li {
 border-bottom: 1px dashed #cccccc;
 padding-top: 15px;
 }
 
 | 
多组值参数mixin
在变量后加三个点表示,如$variables…。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | 
 @mixin box-shadow($shadow...) {
 -webkit-box-shadow:$shadow;
 box-shadow:$shadow;
 }
 .box{
 border:1px solid #ccc;
 @include box-shadow(0 2px 2px rgba(0,0,0,.3),0 3px 3px rgba(0,0,0,.3),0 4px 4px rgba(0,0,0,.3));
 }
 
 
 .box{
 border:1px solid #ccc;
 -webkit-box-shadow:0 2px 2px rgba(0,0,0,.3),0 3px 3px rgba(0,0,0,.3),0 4px 4px rgba(0,0,0,.3);
 box-shadow:0 2px 2px rgba(0,0,0,.3),0 3px 3px rgba(0,0,0,.3),0 4px 4px rgba(0,0,0,.3);
 }
 
 | 
继承
继承@extend
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | 
 
 
 
 h1{
 border: 4px solid #ff9aa9;
 }
 .speaker{
 @extend h1;
 border-width: 2px;
 }
 
 
 h1,.speaker{
 border: 4px solid #ff9aa9;
 }
 .speaker{
 border-width: 2px;
 }
 
 | 
占位选择器%
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | 
 %ir{
 color: transparent;
 text-shadow: none;
 background-color: transparent;
 border: 0;
 }
 %border5{
 border: 5px;
 }
 #header{
 h1{
 @extend %ir;
 width:300px;
 }
 }
 .ir{
 @extend %ir;
 }
 
 
 #header h1,.ir{
 color: transparent;
 text-shadow: none;
 background-color: transparent;
 border: 0;
 }
 #header h1{
 width:300px;
 }
 
 | 
函数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | $baseFontSize:      10px !default;
 
 
 @function pxToRem($px) {
 @return $px / $baseFontSize * 1rem;
 }
 
 body{
 font-size:$baseFontSize;
 }
 .test{
 font-size:pxToRem(16px);
 }
 
 
 body{
 font-size:10px;
 }
 .test{
 font-size:1.6rem;
 }
 
 | 
运算
| 12
 3
 4
 5
 6
 
 | $baseFontSize:          14px !default;
 $baseLineHeight:        1.5 !default;
 $baseGap:               $baseFontSize * $baseLineHeight !default;
 $halfBaseGap:           $baseGap / 2  !default;
 $samllFontSize:         $baseFontSize - 2px  !default;
 
 | 
条件判断及循环
if判断@if判断
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | $lte7: true;
 $type: monster;
 .ib{
 display:inline-block;
 @if $lte7 {
 *display:inline;
 *zoom:1;
 }
 }
 p {
 @if $type == ocean {
 color: blue;
 } @else if $type == monster {
 color: green;
 }
 }
 
 
 .ib{
 display:inline-block;
 *display:inline;
 *zoom:1;
 }
 p {
 color: green;
 }
 
 | 
三目判断
| 12
 3
 
 | if(true, 1px, 2px) => 1px
 if(false, 1px, 2px) => 2px
 
 | 
for循环
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | 
 
 
 @for $i from 1 through 3 {
 .item-#{$i} { width: 2em * $i; }
 }
 
 
 .item-1 {
 width: 2em;
 }
 .item-2 {
 width: 4em;
 }
 .item-3 {
 width: 6em;
 }
 
 | 
循环,@each循环
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | 
 
 $animal-list: puma, sea-slug, egret, salamander;
 @each $animal in $animal-list {
 .#{$animal}-icon {
 background-image: url('/images/#{$animal}.png');
 }
 }
 
 
 .puma-icon {
 background-image: url('/images/puma.png');
 }
 .sea-slug-icon {
 background-image: url('/images/sea-slug.png');
 }
 .egret-icon {
 background-image: url('/images/egret.png');
 }
 .salamander-icon {
 background-image: url('/images/salamander.png');
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | 
 $animal-data: (puma, black, default),(sea-slug, blue, pointer),(egret, white, move);
 @each $animal, $color, $cursor in $animal-data {
 .#{$animal}-icon {
 background-image: url('/images/#{$animal}.png');
 border: 2px solid $color;
 cursor: $cursor;
 }
 }
 
 
 .puma-icon {
 background-image: url('/images/puma.png');
 border: 2px solid black;
 cursor: default;
 }
 .sea-slug-icon {
 background-image: url('/images/sea-slug.png');
 border: 2px solid blue;
 cursor: pointer;
 }
 .egret-icon {
 background-image: url('/images/egret.png');
 border: 2px solid white;
 cursor: move;
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | 
 $headings: (h1: 2em, h2: 1.5em, h3: 1.2em);
 @each $header, $size in $headings {
 #{$header} {
 font-size: $size;
 }
 }
 
 
 h1 {
 font-size: 2em;
 }
 h2 {
 font-size: 1.5em;
 }
 h3 {
 font-size: 1.2em;
 }
 
 | 
参考链接