文件後綴名css
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.scss:api
//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註釋方式/* */
,非壓縮模式會被編譯出來,另外一種則是//
雙斜杆形式的單行註釋,不過這種單行註釋不會被轉譯出來,還有一種是重要註釋/!**/,任何模式均可以被編譯。函數
/* *我是css的標準註釋 *設置body內距 */ body{ padding:5px; }
單行註釋跟JavaScript語言中的註釋同樣,使用又斜槓(//
),但單行註釋不會輸入到CSS中。組件化
//我是雙斜槓表示的單行註釋 //設置body內距 body{ padding:5px; //5px }
sass的變量必須是$開頭,後面緊跟變量名,而變量值和變量名之間就須要使用冒號(:)分隔開(就像CSS屬性設置同樣),若是值後面加上!default則表示默認值。
定義以後能夠在全局範圍內使用。
//sass style //------------------------------- $fontSize: 12px; body{ font-size:$fontSize; } //css style //------------------------------- body{ font-size:12px; }
sass的默認變量僅須要在值後面加上!default
便可。
//sass style //------------------------------- $baseLineHeight: 1.5 !default; body{ line-height: $baseLineHeight; } //css style //------------------------------- body{ line-height:1.5; }
sass的默認變量通常是用來設置默認值,而後根據需求來覆蓋的,覆蓋的方式也很簡單,只須要在默認變量以前從新聲明下變量便可
//sass style //------------------------------- $baseLineHeight: 2; $baseLineHeight: 1.5 !default; body{ line-height: $baseLineHeight; } //css style //------------------------------- body{ line-height:2; }
能夠看出如今編譯後的line-height
爲2,而不是咱們默認的1.5。默認變量的價值在進行組件化開發的時候會很是有用。
通常咱們定義的變量都爲屬性值,可直接使用,可是若是變量做爲屬性或在某些特殊狀況下等則必需要以#{$variables}
形式使用。
//sass style //------------------------------- $borderDirection: top !default; $baseFontSize: 12px !default; $baseLineHeight: 1.5 !default; //應用於class和屬性 .border-#{$borderDirection}{ border-#{$borderDirection}:1px solid #ccc; } //應用於複雜的屬性值 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數據可經過空格,逗號或小括號分隔多個值,可用nth($var,$index)
取值。關於list數據操做還有不少其餘函數如length($list)
,join($list1,$list2,[$separator])
,append($list,$value,[$separator])
等,具體可參考sass Functions(搜索List Functions
便可)
定義
//一維數據
$px: 5px 10px 20px 30px;
//二維數據,至關於js中的二維數組
$px: 5px 10px, 20px 30px;
$px: (5px 10px) (20px 30px);
使用
//sass style //------------------------------- $linkColor: #08c #333 !default;//第一個值爲默認值,第二個鼠標滑過值 a{ color:nth($linkColor,1); &:hover{ color:nth($linkColor,2); } } //css style //------------------------------- a{ color:#08c; } a:hover{ color:#333; }
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
便可)
定義
$heading: (h1: 2em, h2: 1.5em, h3: 1.2em);
使用
//sass style //------------------------------- $headings: (h1: 2em, h2: 1.5em, h3: 1.2em); @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 //------------------------------- $fontSize: 12px; body{ $fontSize: 14px; font-size:$fontSize; } p{ font-size:$fontSize; } //css style //------------------------------- body{ font-size:14px; } p{ font-size:14px; }
啓用global以後的機制
請注意,這個目前還沒法使用,因此樣式不是真實解析出來的。
//sass style //------------------------------- $fontSize: 12px; $color: #333; body{ $fontSize: 14px; $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揭祕之變量
sass的嵌套包括兩種:一種是選擇器的嵌套;另外一種是屬性的嵌套。咱們通常提及或用到的都是選擇器的嵌套。
所謂選擇器嵌套指的是在一個選擇器中嵌套另外一個選擇器來實現繼承,從而加強了sass文件的結構性和可讀性。
在選擇器嵌套中,可使用&
表示父元素選擇器
//sass style //------------------------------- #top_nav{ line-height: 40px; text-transform: capitalize; background-color:#333; li{ float:left; } a{ display: block; padding: 0 10px; color: #fff; &:hover{ color:#ddd; } } } //css style //------------------------------- #top_nav{ line-height: 40px; text-transform: capitalize; background-color:#333; } #top_nav li{ float:left; } #top_nav a{ display: block; padding: 0 10px; color: #fff; } #top_nav a: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; }
固然這只是個屬性嵌套的例子,若是實際這樣使用,那估計得瘋掉。
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 { ... }
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-h li{ @include horizontal-line(1px solid #ccc); } .imgtext-h--product li{ @include horizontal-line($padding:15px); } //css style //------------------------------- .imgtext-h li { border-bottom: 1px solid #cccccc; padding-top: 10px; padding-bottom: 10px; } .imgtext-h--product li { border-bottom: 1px dashed #cccccc; padding-top: 15px; padding-bottom: 15px; }
多組值參數mixin
若是一個參數能夠有多組值,如box-shadow、transition等,那麼參數則須要在變量後加三個點表示,如$variables...
。
//sass style //------------------------------- //box-shadow能夠有多組值,因此在變量參數後面添加... @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)); } //css style //------------------------------- .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); }
@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) { body { color: 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; background-color: transparent; border: 0; } %clearfix{ @if $lte7 { *zoom: 1; } &:before, &:after { content: ""; display: table; font: 0/0 a; } &:after { clear: both; } } #header{ h1{ @extend %ir; width:300px; } } .ir{ @extend %ir; } //css style //------------------------------- #header h1, .ir{ color: transparent; text-shadow: none; background-color: transparent; border: 0; } #header h1{ 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 //------------------------------- $baseFontSize: 10px !default; $gray: #ccc !defualt; // 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; }
sass具備運算的特性,能夠對數值型的Value(如:數字、顏色、變量等)進行加減乘除四則運算。請注意運算符先後請留一個空格,否則會出錯。
$baseFontSize: 14px !default;
$baseLineHeight: 1.5 !default;
$baseGap: $baseFontSize * $baseLineHeight !default;
$halfBaseGap: $baseGap / 2 !default;
$samllFontSize: $baseFontSize - 2px !default;
//grid
$_columns: 12 !default; // Total number of columns
$_column-width: 60px !default; // Width of a single column
$_gutter: 20px !default; // Width of the gutter
$_gridsystem-width: $_columns * ($_column-width + $_gutter); //grid system width
@if
可一個條件單獨使用,也能夠和@else
結合多條件使用
//sass style //------------------------------- $lte7: true; $type: monster; .ib{ display:inline-block; @if $lte7 { *display:inline; *zoom:1; } } 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; *zoom:1; } p { color: green; }
語法爲:if($condition, $if_true, $if_false)
。三個參數分別表示:條件,條件爲真的值,條件爲假的值。
if(true, 1px, 2px) => 1px
if(false, 1px, 2px) => 2px
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-#{$i} { width: 2em * $i; } } //css style //------------------------------- .item-1 { width: 2em; } .item-2 { width: 4em; } .item-3 { width: 6em; }
語法爲:@each $var in <list or map>
。其中$var
表示變量,而list和map表示list類型數據和map類型數據。sass 3.3.0新加入了多字段循環和map數據循環。
單個字段list數據循環
//sass style //------------------------------- $animal-list: puma, sea-slug, egret, salamander; @each $animal in $animal-list { .#{$animal}-icon { 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 //------------------------------- $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; } } //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 //------------------------------- $headings: (h1: 2em, h2: 1.5em, h3: 1.2em); @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; }