sass語法
關於sass 3.3.0更新說明——3.3.0
sublime相關插件爲:scss語法高亮,sass語法高亮,編譯,保存即編譯,格式化
文件後綴名
sass有兩種後綴名文件:一種後綴名爲sass,不使用大括號和分號;另一種就是我們這裏使用的scss文件,這種和我們平時寫的css文件格式差不多,使用大括號和分號。而本教程中所說的所有sass文件都指後綴名爲scss的文件。在此也建議使用後綴名爲scss的文件,以避免sass後綴名的嚴格格式要求報錯。
//文件後綴名爲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
方式存在。
所有的sass導入文件都可以忽略後綴名.scss
。一般來說基礎的文件命名方法以_開頭,如_mixin.scss
。這種文件在導入的時候可以不寫下劃線,可寫成@import "mixin"
。
被導入sass文件a.scss:
//a.scss
//-------------------------------
body {
background: #eee;
}
需要導入樣式的sass文件b.scss:
@import "reset.css";
@import "a";
p{
background: #0982c1;
}
轉譯出來的b.css樣式:
@import "reset.css";
body {
background: #eee;
}
p{
background: #0982c1;
}
根據上面的代碼可以看出,b.scss編譯後,reset.css繼續保持import的方式,而a.scss則被整合進來了。
註釋
sass有兩種註釋方式,一種是標準的css註釋方式/* */
,另一種則是//
雙斜杆形式的單行註釋,不過這種單行註釋不會被轉譯出來。
標準的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
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
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揭祕之變量
嵌套(Nesting)
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;
}
當然這只是個屬性嵌套的例子,如果實際這樣使用,那估計得瘋掉。
@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-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
@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;
}
關於@mixin
,%
,@function
更多說明可參閱:
運算
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判斷
@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循環有兩種形式,分別爲:@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循環
語法爲:@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;
}