Sass

Sass官網:  http://www.w3cplus.com/sassguide/

sass: 預處理語言;  Ruby語言,即便不懂Ruby,也是可使用sasscss

文件後綴名

sass有兩種後綴名文件:一種後綴名爲sass,不使用大括號和分號;另外一種就是咱們這裏使用的scss文件,這種和咱們平時寫的css文件格式差很少,使用大括號和分號。而本教程中所說的全部sass文件都指後綴名爲scss的文件。在此也建議使用後綴名爲scss的文件,以免sass後綴名的嚴格格式要求報錯。html

//文件後綴名爲sass的語法
body
background: #eee 
  font-size:12px
p
  background: #0982c1
 
//文件後綴名爲scss的語法  
body {
  background: #eee;
font-size:12px; 
}
p{
background: #0982c1; 
}

導入

sass的導入(@import)規則和CSS的有所不一樣,編譯時會將@import的scss文件合併進來只生成一個CSS文件。可是若是你在sass文件中導入css文件如@import 'reset.css',那效果跟普通CSS導入樣式文件同樣,導入的css文件不會合併到編譯後的文件中,而是以@import方式存在。css3

全部的sass導入文件均可以忽略後綴名.scss。通常來講基礎的文件命名方法以_開頭,如_mixin.scss。這種文件在導入的時候能夠不寫下劃線,可寫成@import "mixin"web

被導入sass文件a.scssapi

//a.scss
//-------------------------------
body{
background: #eee; 
}

須要導入樣式的sass文件b.scss數組

@import "reset.css";
@import "a";
p{
  background: #0982c1;
}

轉譯出來的b.css樣式:sass

@import "reset.css";
body{
  background: #eee;
}
p{
  background: #0982c1;
}

根據上面的代碼能夠看出,b.scss編譯後,reset.css繼續保持import的方式,而a.scss則被整合進來了。app

註釋

sass有兩種註釋方式,一種是標準的css註釋方式/* */,另外一種則是//雙斜杆形式的單行註釋,不過這種單行註釋不會被轉譯出來。ide

標準的css註釋

/*
*我是css的標準註釋
*設置body內距
*/
body{
  padding:5px;
}

雙斜杆單行註釋

單行註釋跟JavaScript語言中的註釋同樣,使用又斜槓(//),但單行註釋不會輸入到CSS中。函數

//我是雙斜槓表示的單行註釋
//設置body內距
body{
padding:5px;//5px 
}

變量

sass的變量必須是$開頭,後面緊跟變量名,而變量值和變量名之間就須要使用冒號(:)分隔開(就像CSS屬性設置同樣),若是值後面加上!default則表示默認值。

普通變量

定義以後能夠在全局範圍內使用。

//sass style
//-------------------------------
: 12px;$fontSize
body{
font-size:$fontSize;   
}
 
//css style
//-------------------------------
body{
font-size:12px;   
}

默認變量

sass的默認變量僅須要在值後面加上!default便可。

//sass style
//-------------------------------
:        1.5 !default;$baseLineHeight
body{
line-height: $baseLineHeight;   
}
 
//css style
//-------------------------------
body{
line-height:1.5;   
}

sass的默認變量通常是用來設置默認值,而後根據需求來覆蓋的,覆蓋的方式也很簡單,只須要在默認變量以前從新聲明下變量便可

//sass style
//-------------------------------
:        2;$baseLineHeight
:        1.5 !default;$baseLineHeight
body{
line-height: $baseLineHeight;   
}
 
//css style
//-------------------------------
body{
line-height:2;   
}

能夠看出如今編譯後的line-height爲2,而不是咱們默認的1.5。默認變量的價值在進行組件化開發的時候會很是有用。

特殊變量

通常咱們定義的變量都爲屬性值,可直接使用,可是若是變量做爲屬性或在某些特殊狀況下等則必需要以#{$variables}形式使用。

//sass style
//-------------------------------
:       top !default;$borderDirection
:          12px !default;$baseFontSize
:        1.5 !default;$baseLineHeight
 
//應用於class和屬性
.border-#{$borderDirection}{
border:1px solid #ccc; -#{$borderDirection}
}
//應用於複雜的屬性值
body{
font:#{$baseFontSize}/#{$baseLineHeight};   
}
 
//css style
//-------------------------------
.border-top{
border-top:1px solid #ccc; 
}
body{
font: 12px/1.5; 
}

多值變量

多值變量分爲list類型和map類型,簡單來講list類型有點像js中的數組,而map類型有點像js中的對象。

list

list數據可經過空格,逗號或小括號分隔多個值,可用nth($var,$index)取值。關於list數據操做還有不少其餘函數如length($list)join($list1,$list2,[$separator])append($list,$value,[$separator])等,具體可參考sass Functions(搜索List Functions便可)

定義

//一維數據
: 5px 10px 20px 30px;$px
 
//二維數據,至關於js中的二維數組
: 5px 10px, 20px 30px;$px
: (5px 10px) (20px 30px);$px

使用

//sass style
//-------------------------------
:         #08c #333 !default;//第一個值爲默認值,第二個鼠標滑過值$linkColor
a{
color:nth($linkColor,1); 
 
:hover  &{
color:nth($linkColor,2);   
  }
}
 
//css style
//-------------------------------
a{
color:#08c; 
}
a:hover{
color:#333; 
}

map

map數據以key和value成對出現,其中value又能夠是list。格式爲:$map: (key1: value1, key2: value2, key3: value3);。可經過map-get($map,$key)取值。關於map數據還有不少其餘函數如map-merge($map1,$map2)map-keys($map)map-values($map)等,具體可參考sass Functions(搜索Map Functions便可)

定義

: (h1: 2em, h2: 1.5em, h3: 1.2em);$heading

使用

//sass style
//-------------------------------
: (h1: 2em, h2: 1.5em, h3: 1.2em);$headings
@each $header, $size in $headings {
header  #{$} {
font-size: $size;   
  }
}
 
//css style
//-------------------------------
h1{
font-size: 2em; 
}
h2{
font-size: 1.5em; 
}
h3{
font-size: 1.2em; 
}

全局變量

在變量值後面加上!global即爲全局變量。這個目前還用不上,不過將會在sass 3.4後的版本中正式應用。目前的sass變量範圍飽受詬病,因此纔有了這個全局變量。

目前變量機制

在選擇器中聲明的變量會覆蓋外面全局聲明的變量。(這也就人們常說的sass沒有局部變量)

//sass style
//-------------------------------
:      12px;$fontSize
body{
: 14px;    $fontSize       
font-size:$fontSize;    
}
p{
font-size:$fontSize;   
}
 
//css style
//-------------------------------
body{
font-size:14px;   
}
p{
font-size:14px;   
}

啓用global以後的機制

請注意,這個目前還沒法使用,因此樣式不是真實解析出來的。

//sass style
//-------------------------------
:      12px;$fontSize
color:         #333;$
body{
: 14px;    $fontSize       
color#fff    $:  !global;
font-size:$fontSize;   
color:$color;   
}
p{
font-size:$fontSize;   
color:$color;   
}
 
//css style
//-------------------------------
body{
font-size:14px;   
color:#fff;   
}
p{
font-size:12px;   
color:#fff;   
}

這裏設置了兩個變量,而後在body裏面從新設置了下,有點不一樣的是對於$color變量,咱們設置了!global。經過編譯後的css能夠看到font-size取值不一樣,而color取值相同。與上面的機制對比就會發現默認在選擇器裏面的變量爲局部變量,而只有設置了!global以後纔會成爲全局變量。

關於變量的詳細分析請查閱sass揭祕之變量

嵌套(Nesting)

sass的嵌套包括兩種:一種是選擇器的嵌套;另外一種是屬性的嵌套。咱們通常提及或用到的都是選擇器的嵌套。

選擇器嵌套

所謂選擇器嵌套指的是在一個選擇器中嵌套另外一個選擇器來實現繼承,從而加強了sass文件的結構性和可讀性。

在選擇器嵌套中,可使用&表示父元素選擇器

//sass style
//-------------------------------
#top_nav{
line-height: 40px; 
text-transform: capitalize; 
 
li {
float:left   ;
  }
a {
display: block;   
padding: 0 10px;   
color: #fff;   
 
:hover    &{
color:#ddd;     
    }
  }
}
 
//css style
//-------------------------------
#top_nav{
line-height: 40px; 
text-transform: capitalize; 
 
#top_navli{
float:left ;
}
#top_nava{
display: block; 
padding: 0 10px; 
color: #fff; 
}
#top_nava:hover{
color:#ddd; 
}

屬性嵌套

所謂屬性嵌套指的是有些屬性擁有同一個開始單詞,如border-width,border-color都是以border開頭。拿個官網的實例看下:

//sass style
//-------------------------------
.fakeshadow{
border: { 
    style: solid;
left: {   
      width: 4px;
color: #888;     
    }
right: {   
      width: 2px;
color: #ccc;     
    }
  }
}
 
//css style
//-------------------------------
.fakeshadow{
border-style: solid; 
border-left-width: 4px; 
border-left-color: #888; 
border-right-width: 2px; 
border-right-color: #ccc; 
}

固然這只是個屬性嵌套的例子,若是實際這樣使用,那估計得瘋掉。

@at-root

sass3.3.0中新增的功能,用來跳出選擇器嵌套的。默認全部的嵌套,繼承全部上級選擇器,但有了這個就能夠跳出全部上級選擇器。

普通跳出嵌套

//sass style
//-------------------------------
//沒有跳出
.parent-1{
color:#f00; 
.child {
width:100px;   
  }
}
 
//單個選擇器跳出
.parent-2{
color:#f00; 
@at-root .child { 
width:200px;   
  }
}
 
//多個選擇器跳出
.parent-3{
background:#f00; 
@at-root { 
.child1   {
width:300px;     
    }
.child2   {
width:400px;     
    }
  }
}
 
//css style
//-------------------------------
.parent-1{
color: #f00; 
}
.parent-1.child{
width: 100px; 
}
 
.parent-2{
color: #f00; 
}
.child{
width: 200px; 
}
 
.parent-3{
background: #f00; 
}
.child1{
width: 300px; 
}
.child2{
width: 400px; 
}

@at-root (without: ...)@at-root (with: ...)

默認@at-root只會跳出選擇器嵌套,而不能跳出@media@support,若是要跳出這兩種,則需使用@at-root (without: media)@at-root (without: support)。這個語法的關鍵詞有四個:all(表示全部),rule(表示常規css),media(表示media),support(表示support,由於@support目前還沒法普遍使用,因此在此不表)。咱們默認的@at-root其實就是@at-root (without:rule)

//sass style
//-------------------------------
//跳出父級元素嵌套
@media print {
.parent1   {
color:#f00;     
@at-root .child1 {     
width:200px;       
      }
    }
}
 
//跳出media嵌套,父級有效
@media print {
.parent2 {
color:#f00;   
 
@at-root (without: media) {   
.child2     {
width:200px;       
      }
    }
  }
}
 
//跳出media和父級
@media print {
.parent3 {
color:#f00;   
 
@at-root (without: all) {   
.child3     {
width:200px;       
      }
    }
  }
}
 
//sass style
//-------------------------------
@media print {
.parent1 {
color: #f00;   
  }
.child1 {
width: 200px;   
  }
}
 
@media print {
.parent2 {
color: #f00;   
  }
}
.parent2.child2{
width: 200px; 
}
 
@media print {
.parent3 {
color: #f00;   
  }
}
.child3{
width: 200px; 
}

@at-root&配合使用

//sass style
//-------------------------------
.child{
@at-root .parent &{   
color:#f00;       
    }
}
 
//css style
//-------------------------------
.parent.child{
color: #f00; 
}

應用於@keyframe

//sass style
//-------------------------------
.demo{
    ...
animation: motion 3s infinite;   
 
@at-root {   
@keyframes motion {       
          ...
        }
    }
}
 
//css style
//-------------------------------   
.demo{
    ...  
animation: motion 3s infinite;    
}
@keyframes motion {
    ...
}

混合(mixin)

sass中使用@mixin聲明混合,能夠傳遞參數,參數名以$符號開始,多個參數以逗號分開,也能夠給參數設置默認值。聲明的@mixin經過@include來調用。

無參數mixin

//sass style
//-------------------------------
@mixin center-block {
margin-left:auto;   
margin-right:auto;   
}
.demo{
@include center-block;   
}
 
//css style
//-------------------------------
.demo{
margin-left:auto;   
margin-right:auto;   
}

有參數mixin

//sass style
//-------------------------------   
@mixin opacity($opacity:50) {
opacity: $opacity / 100; 
filter: alpha(opacity=$opacity); 
}
 
//css style
//-------------------------------
.opacity{
@include opacity;//參數使用默認值 
}
.opacity-80{
@include opacity(80);//傳遞參數 
}

多個參數mixin

調用時可直接傳入值,如@include傳入參數的個數小於@mixin定義參數的個數,則按照順序表示,後面不足的使用默認值,如不足的沒有默認值則報錯。除此以外還能夠選擇性的傳入參數,使用參數名與值同時傳入。

//sass style
//-------------------------------   
@mixin horizontal-line($border:1px dashed #ccc, $padding:10px){
border-bottom:$border;   
padding-top:$padding;   
padding-bottom:$padding;    
}
.imgtext-hli{
@include horizontal-line(1px solid #ccc);   
}
.imgtext-h--productli{
@include horizontal-line($padding:15px);   
}
 
//css style
//-------------------------------
.imgtext-hli{
border-bottom: 1px solid #cccccc;   
padding-top: 10px;   
padding-bottom: 10px;   
}
.imgtext-h--productli{
border-bottom: 1px dashed #cccccc;   
padding-top: 15px;   
padding-bottom: 15px;   
}

多組值參數mixin

若是一個參數能夠有多組值,如box-shadow、transition等,那麼參數則須要在變量後加三個點表示,如$variables...

//sass style
//-------------------------------   
//box-shadow能夠有多組值,因此在變量參數後面添加...
@mixin box-shadow($shadow...) {
box-shadow:$shadow;  -webkit-
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)); 
}
 
//css style
//-------------------------------
.box{
border:1px solid #ccc; 
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);  -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); 
}

@content

@content在sass3.2.0中引入,能夠用來解決css3的@media等帶來的問題。它可使@mixin接受一整塊樣式,接受的樣式從@content開始。

//sass style 
//-------------------------------                     
@mixin max-screen($res){
@media only screen and ( max-width: $res ) 
  {
@content;   
  }
}
 
@include max-screen(480px) {
bodycolor: red } {
}
 
//css style
//-------------------------------
@media only screen and (max-width: 480px) {
  body { color: red }
}                     

PS:@mixin經過@include調用後解析出來的樣式是以拷貝形式存在的,而下面的繼承則是以聯合聲明的方式存在的,因此從3.2.0版本之後,建議傳遞參數的用@mixin,而非傳遞參數類的使用下面的繼承%

繼承

sass中,選擇器繼承可讓選擇器繼承另外一個選擇器的全部樣式,並聯合聲明。使用選擇器的繼承,要使用關鍵詞@extend,後面緊跟須要繼承的選擇器。

//sass style
//-------------------------------
h1{
border: 4px solid #ff9aa9; 
}
.speaker{
@extend h1; 
border-width: 2px; 
}
 
//css style
//-------------------------------
h1.speaker,{
border: 4px solid #ff9aa9; 
}
.speaker{
border-width: 2px; 
}

佔位選擇器%

從sass 3.2.0之後就能夠定義佔位選擇器%。這種選擇器的優點在於:若是不調用則不會有任何多餘的css文件,避免了之前在一些基礎的文件中預約義了不少基礎的樣式,而後實際應用中不論是否使用了@extend去繼承相應的樣式,都會解析出來全部的樣式。佔位選擇器以%標識定義,經過@extend調用。

//sass style
//-------------------------------
%ir{
color: transparent; 
text-shadow: none; 
 
border: 0; 
}
%clearfix{
@if $lte7 { 
: 1;    *zoom
  }
:before  &,
:after  &{
content: "";   
display: table;   
font: 0/0 a;   
  }
:after  &{
clear: both;   
  }
}
#header{
h1 {
@extend %ir;   
width:300px;   
  }
}
.ir{
@extend %ir; 
}
 
//css style
//-------------------------------
#headerh1,
.ir{
color: transparent; 
text-shadow: none; 
 
border: 0; 
}
#headerh1{
width:300px; 
}

如上代碼,定義了兩個佔位選擇器%ir%clearfix,其中%clearfix這個沒有調用,因此解析出來的css樣式也就沒有clearfix部分。佔位選擇器的出現,使css文件更加簡練可控,沒有多餘。因此能夠用其定義一些基礎的樣式文件,而後根據須要調用產生相應的css。

ps:在@media中暫時不能@extend @media外的代碼片斷,之後將會能夠。

函數

sass定義了不少函數可供使用,固然你也能夠本身定義函數,以@fuction開始。sass的官方函數連接爲:sass fuction,實際項目中咱們使用最多的應該是顏色函數,而顏色函數中又以lighten減淡和darken加深爲最,其調用方法爲lighten($color,$amount)darken($color,$amount),它們的第一個參數都是顏色值,第二個參數都是百分比。

//sass style
//-------------------------------                     
:      10px !default;$baseFontSize
:              #ccc !defualt;$gray       
 
// pixels to rems 
@function pxToRem($px) {
@return $px / $baseFontSize * 1rem; 
}
 
body{
font-size:$baseFontSize; 
color:lighten($gray,10%); 
}
.test{
font-size:pxToRem(16px); 
color:darken($gray,10%); 
}
 
//css style
//-------------------------------
body{
font-size:10px; 
color:#E6E6E6; 
}
.test{
font-size:1.6rem; 
color:#B3B3B3; 
}

關於@mixin%@function更多說明可參閱:

運算

sass具備運算的特性,能夠對數值型的Value(如:數字、顏色、變量等)進行加減乘除四則運算。請注意運算符先後請留一個空格,否則會出錯。

:          14px !default;$baseFontSize
:        1.5 !default;$baseLineHeight
:               $baseFontSize * $baseLineHeight !default;$baseGap
:           $baseGap / 2  !default;$halfBaseGap
:         $baseFontSize - 2px  !default;$samllFontSize
 
//grid 
:                     12 !default;// Total number of columns$_columns     
width:                60px !default;// Width of a single column$_column-  
:                      20px !default;// Width of the gutter$_gutter    
width:            $_columns * ($_column-width + $_gutter);//grid system width$_gridsystem-

條件判斷及循環

@if判斷

@if可一個條件單獨使用,也能夠和@else結合多條件使用

//sass style
//-------------------------------
: true;$lte7
: monster;$type
.ib{
display:inline-block;   
@if $lte7 {   
display:inline;        *
:1;        *zoom
    }
}
p{
@if $type == ocean { 
color: blue;   
@else if $type == matador {  }
color: red;   
@else if $type == monster {  }
color: green;   
@else {  }
color: black;   
  }
}
 
//css style
//-------------------------------
.ib{
display:inline-block;   
display:inline;    *
:1;    *zoom
}
p{
color: green; 
}

三目判斷

語法爲:if($condition, $if_true, $if_false) 。三個參數分別表示:條件,條件爲真的值,條件爲假的值。

iftrue121(,px,px) =>px
iffalse122(,px,px) =>px

for循環

for循環有兩種形式,分別爲:@for $var from <start> through <end>@for $var from <start> to <end>。$i表示變量,start表示起始值,end表示結束值,這兩個的區別是關鍵字through表示包括end這個數,而to則不包括end這個數。

//sass style
//-------------------------------
@for $i from 1 through 3 {
.item-iwidth: 2em * $i; #{$} {}
}
 
//css style
//-------------------------------
.item-1{
width: 2em; 
}
.item-2{
width: 4em; 
}
.item-3{
width: 6em; 
}

@each循環

語法爲:@each $var in <list or map>。其中$var表示變量,而list和map表示list類型數據和map類型數據。sass 3.3.0新加入了多字段循環和map數據循環。

單個字段list數據循環

//sass style
//-------------------------------
: puma, sea-slug, egret, salamander;$animal-list
@each $animal in $animal-list {
icon  .#{$animal}-{
background-image: url('/images/#{$animal}.png');   
  }
}
 
//css style
//-------------------------------
.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'); 
}

多個字段list數據循環

//sass style
//-------------------------------
: (puma, black, default),(sea-slug, blue, pointer),(egret, white, move);$animal-data
@each $animal, $color, $cursor in $animal-data {
icon  .#{$animal}-{
background-image: url('/images/#{$animal}.png');   
border: 2px solid $color;   
cursor: $cursor;   
  }
}
 
//css style
//-------------------------------
.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; 
}

多個字段map數據循環

//sass style
//-------------------------------
: (h1: 2em, h2: 1.5em, h3: 1.2em);$headings
@each $header, $size in $headings {
header  #{$} {
font-size: $size;   
  }
}
 
//css style
//-------------------------------
h1{
font-size: 2em; 
}
h2{
font-size: 1.5em; 
}
h3{
font-size: 1.2em; 
}
相關文章
相關標籤/搜索