使用SASS

1. Sass簡介

Sass 是一種css預(yù)處理器,CSS 預(yù)處理器定義了一種新的語言捻脖,其基本思想是上鞠,用一種專門的編程語言尺借,為 CSS 增加了一些編程的特性份汗,將 CSS 作為目標生成文件盈电,然后開發(fā)者就只要使用這種語言進行編碼工作。

除了sass還有一些其他常見的預(yù)處理語言

  • LESS
  • Stylus

Sass 有時候也被稱為 SCSS,兩者之間不同之處有以下兩點:

  • 文件擴展名不同杯活,Sass 是以“.sass”后綴為擴展名匆帚,而 SCSS 是以“.scss”后綴為擴展名
  • 語法書寫方式不同,Sass 是以嚴格的縮進式語法規(guī)則來書寫旁钧,不帶大括號({})和分號(;)吸重,而 SCSS 的語法書寫和我們的 CSS 語法書寫方式非常類似。

來看一個示例:

/* Sass 語法 */
$font-stack: Helvetica, sans-serif
$primary-color: #333
body
  font: 100% $font-stack
  color: $primary-color
/* Scss 語法 */
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
  font: 100% $font-stack;
  color: $primary-color;
}

編譯出來的 CSS

body {
  font: 100% Helvetica, sans-serif;
  color: #333;
}

scss文件中可以完全像寫正常的css那樣去寫.

2. sass安裝

npm install -g node-sass
node-sass -w xxx.scss xxx.css --output-style expanded

node-sass 參數(shù):

-w, --watch                Watch a directory or file
-r, --recursive            Recursively watch directories or files
-o, --output               Output directory
-x, --omit-source-map-url  Omit source map URL comment from output
-i, --indented-syntax      Treat data from stdin as sass code (versus scss)
-q, --quiet                Suppress log output except on error
-v, --version              Prints version info
--output-style             CSS output style (nested | expanded | compact | compressed)
--indent-type              Indent type for output CSS (space | tab)
--indent-width             Indent width; number of spaces or tabs (maximum value: 10)
--linefeed                 Linefeed style (cr | crlf | lf | lfcr)
--source-comments          Include debug info in output
--source-map               Emit source map
--source-map-contents      Embed include contents in map
--source-map-embed         Embed sourceMappingUrl as data URI
--source-map-root          Base path, will be emitted in source-map as is
--include-path             Path to look for imported files
--follow                   Follow symlinked directories
--precision                The amount of precision allowed in decimal numbers
--importer                 Path to .js file containing custom importer
--functions                Path to .js file containing custom functions
--help                     Print usage info

3. Sass的基本特性

變量

$brand-primary : darken(#428bca, 6.5%) !default;
$btn-primary-color : #fff !default;
$btn-primary-bg : $brand-primary !default;
$btn-primary-border : darken($btn-primary-bg, 5%) !default;
.btn-primary {
   background-color: $btn-primary-bg;
   color: $btn-primary-color;
   border: 1px solid $btn-primary-border;
}
$baseLineHeight: 2;
$baseLineHeight: 1.5 !default;
body{
    line-height: $baseLineHeight;
}
/* 局部變量 */
$color: orange !default;
.block {
  color: $color;
}
em {
  $color: red;
  a {
    color: $color;
  }
}
span {
  color: $color;
}

嵌套

nav {
  a {
    color: red;
    header & {
      color:green;
    }
  }  
}

.box {
  border: {
   top: 1px solid red;
   bottom: 1px solid green;
  }
}

.clearfix{
  &:before,
  &:after {
    content:"";
    display: table;
  }
  &:after {
    clear:both;
    overflow: hidden;
  }
}

不要無節(jié)制嵌套,一切都根據(jù)實際情況判斷

混合宏


@mixin border-radius{
  -webkit-border-radius: 5px;
  border-radius: 5px;
}
button{
  @include border-radius;
}
@mixin border-radius($radius:5px){
  -webkit-border-radius: $radius;
  border-radius: $radius;
}
.nav{
  @include border-radius;
}
.box{
  @include border-radius(3px);
}
@mixin center($width,$height){
  width: $width;
  height: $height;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -($height) / 2;
  margin-left: -($width) / 2;
}
.box-center {
  @include center(500px,300px);
}
@mixin box-shadow($shadows...){
  @if length($shadows) >= 1 {
    -webkit-box-shadow: $shadows;
    box-shadow: $shadows;
  } @else {
    $shadows: 0 0 2px rgba(#000,.25);
    -webkit-box-shadow: $shadow;
    box-shadow: $shadow;
  }
}
.box {
  @include box-shadow(0 0 1px rgba(#000,.5),0 0 2px rgba(#000,.2));
}

混合宏會生成冗余代碼

擴展/繼承

.btn {
  border: 1px solid #ccc;
  padding: 6px 10px;
  font-size: 14px;
}

.btn-primary {
  background-color: #f36;
  color: #fff;
  @extend .btn;
}

.btn-second {
  background-color: orange;
  color: #fff;
  @extend .btn;
}

不會生成冗余代碼

占位符

%mt5 {
  margin-top: 5px;
}
%pt5{
  padding-top: 5px;
}
.btn {
  @extend %mt5;
  @extend %pt5;
}

mt5 和 pt5 并不會生成最終代碼

插值#{}

允許在#{}內(nèi)部解析變量

$properties: (margin, padding);
@mixin set-value($side, $value) {
  @each $prop in $properties {
    #{$prop}-#{$side}: $value;
  }
}
.login-box {
  @include set-value(top, 14px);
}

注釋

  1. 類似 CSS 的注釋方式歪今,使用 ”/* ”開頭嚎幸,結(jié)屬使用 ”*/ ”
  2. 類似 JavaScript 的注釋方式,使用“//”

兩者區(qū)別寄猩,前者會在編譯出來的 CSS 顯示嫉晶,后者在編譯出來的 CSS 中不會顯示

數(shù)據(jù)類型

Sass 和 JavaScript 語言類似,也具有自己的數(shù)據(jù)類型田篇,在 Sass 中包含以下幾種數(shù)據(jù)類型:

  1. 數(shù)字: 如替废,1、 2斯辰、 13舶担、 10px;
  2. 字符串:有引號字符串或無引號字符串彬呻,如衣陶,"foo" 'bar' baz;
  3. 顏色:如闸氮,blue, #04a3f9, rgba(255,0,0,0.5);
  4. 布爾型:如剪况,true, false
  5. 空值:如蒲跨,null译断;
  6. 值列表:用空格或者逗號分開,如或悲,1.5em 1em 0 2em , Helvetica, Arial, sans-serif

運算

.box {
  width: 20px + 8in;
}
$full-width: 960px;
$sidebar-width: 200px;
.content {
  width: $full-width -  $sidebar-width;
}
.box {
  width: 10px * 2;
}
.box {
  width: (100px / 2);  
}
p {
  font: 10px/8px;             // 純 CSS孙咪,不是除法運算
  $width: 1000px;
  width: $width/2;            // 使用了變量堪唐,是除法運算
  width: round(1.5)/2;        // 使用了函數(shù),是除法運算
  height: (500px/2);          // 使用了圓括號翎蹈,是除法運算
  margin-left: 5px + 8px/2px; // 使用了加(+)號淮菠,是除法運算
}
.box {
  width: ((220px + 720px) - 11 * 20 ) / 12 ;  
}
p {
  color: #010203 + #040506;
  background-color: #010203 * 2;
}

$content: "Hello" + "" + "Sass!";
.box:before {
  content: " #{$content} ";
}

4. Sass高級特性

@if

@mixin blockOrHidden($boolean:true) {
  @if $boolean {
    display: block;
  }
  @else {
    display: none;
  }
}
.block {
  @include blockOrHidden;
}
.hidden{
  @include blockOrHidden(false);
}

@for

@for $i from <start> through <end>
@for $i from <start> to <end>
@for $i from 1 through 3 {
  .item-#{$i} { width: 2em * $i; }
}
@for $i from 1 to 3 {
  .item-#{$i} { width: 2em * $i; }
}
$grid-prefix: span !default;
$grid-width: 60px !default;
$grid-gutter: 20px !default;

%grid {
  float: left;
  margin-left: $grid-gutter / 2;
  margin-right: $grid-gutter / 2;
}
@for $i from 1 through 12 {
  .#{$grid-prefix}#{$i}{
    width: $grid-width * $i + $grid-gutter * ($i - 1);
    @extend %grid;
  }  
}

@each

$list: adam john wynn mason kuroir; //$list 就是一個列表
@mixin author-images {
    @each $author in $list {
        .photo-#{$author} {
            background: url("/images/avatars/#{$author}.png") no-repeat;
        }
    }
}
.author-bio {
    @include author-images;
}

5. Sass中的函數(shù)

@function double($n) {
  @return $n * 2;
}
#sidebar {
  width: double(5px);
}

全部的內(nèi)置函數(shù)

  • 字符串函數(shù)

    • unquote
    • quote
    • To-upper-case
    • To-lower-case
    • percentage
    .footer{
      width : percentage(.2)
    }
    
  • round
  • ceil
  • floor
  • abs
  • min
  • max
  • random
  • 列表函數(shù)

    • length 取列表數(shù)據(jù)的長度
    • nth (10px 20px 30px, 1)
    • join join(10px 20px, 30px 40px)
    • append append(10px 20px ,30px)
    • zip zip(1px 2px 3px,solid dashed dotted,green blue red)
    • index index(1px solid red, solid)
    • type-of type-of(100)
    • unit 取單位
    • unitless 判斷一個值是否帶有單位
    • comparable 判斷兩個數(shù)是否可以進行加減合并
    • if(true,1px,2px)
  • Maps 函數(shù)

    • map-get
    • map-has-key
    • map-keys
    • map-values
    • map-merge
    • map-remove
    • keywords
    $map: (
      $key1: value1,
      $key2: value2,
      $key3: value3
    )   
    
    $map: (
      key1: value1,
      key2: (
        key-1: value-1,
        key-2: value-2,
      ),
    key3: value3
    );
    
    $theme-color: (
      default: (
          bgcolor: #fff,
          text-color: #444,
          link-color: #39f
      ),
      primary:(
          bgcolor: #000,
          text-color:#fff,
          link-color: #93f
      ),
      negative: (
          bgcolor: #f36,
          text-color: #fefefe,
          link-color: #d4e
      )
    );
    
    
    /* map-get */
    $social-colors: (
      dribble: #ea4c89,
      facebook: #3b5998,
      github: #171515,
      google: #db4437,
      twitter: #55acee
    );
    .btn-dribble{
      color: map-get($social-colors,facebook);
    }
    /* 沒有值不會報錯 */
    .btn-weibo{
      font-size: 12px;
      color: map-get($social-colors,weibo);
    }
    
    
    /* map中的容錯函數(shù) */
    @function colors($color){
      @if not map-has-key($social-colors,$color){
          @warn "No color found for `#{$color}` in $social-colors map. Property omitted.";
      }
      @return map-get($social-colors,$color);
    }
    .btn-dribble {
      color: colors(dribble);
    }
    
    /* each 遍歷 map */
    @each $name in map-keys($social-colors){
      .btn-#{$name}{
          color: colors($name);
      }
    }
    @for $i from 1 through length(map-keys($social-colors)){
      .btn-#{nth(map-keys($social-colors),$i)} {
        color: colors(nth(map-keys($social-colors),$i));
      }
    }
    
    
    map-values($social-colors)
    /*得到所有的值*/
    
    $color: (
      text: #f36,
      link: #f63,
      border: #ddd,
      backround: #fff
    );
    $typo:(
      font-size: 12px,
      line-height: 1.6
    );
    $newcolor = map-merge($color, $typo);
    
    /* 得到新值 */
    $map:map-remove($social-colors,dribble);
    
  • 顏色函數(shù)

    • rgb

    • rgba

    • red

    • green

    • blue

    • mix 混合兩種顏色 第三個參數(shù)為第一種顏色的比例 mix(blue,red,20%)

    • lighten lighten(red, 20%);

    • darken darken(red,30%);

    • saturate 改變顏色的飽和度 參數(shù)單位為百分比 saturate(blue,20%)

    • desaturate

    • adjust-hue 通過調(diào)整色相 adjust-hue(blue,30deg)

    • grayscale 直接讓飽和度為0 grayscale(blue);

    • alpha 獲取透明度

    • opacity 獲取透明度

    • rgba

    • opacify 增加透明度

    • fade-in 增加透明度

    • transparentize 減少透明度

    • fade-out 減少透明度

    <ul class="swatches red">
      <li></li>
      ...      
      <li></li>
    </ul>
    <ul class="swatches orange">
      <li></li>
      …
      <li></li>
    </ul>
    <ul class="swatches yellow">
      <li></li>
      …
      <li></li>
    </ul>
    <ul class="swatches green">
      <li></li>
      …
      <li></li>
    </ul>
    <ul class="swatches blue">
      <li></li>
      …
      <li></li>
    </ul>
    <ul class="swatches purple">
      <li></li>
      …
      <li></li>
    </ul>
    
    $redBase: #DC143C;
    $orangeBase: saturate(lighten(adjust_hue($redBase, 39), 5), 7);//#f37a16
    $yellowBase: saturate(lighten(adjust_hue($redBase, 64), 6), 13);//#fbdc14
    $greenBase: desaturate(darken(adjust_hue($redBase, 102), 2), 11);//#73c620
    $blueBase: saturate(darken(adjust_hue($redBase, 201), 2), 1);//#12b7d4
    $purpleBase: saturate(darken(adjust_hue($redBase, 296), 2), 1);//#a012d4
    $blackBase: #777;
    $bgc: #fff;
    
    //定義顏色變暗的 mixin
    @mixin swatchesDarken($color) {
      @for $i from 1 through 10 {
        $x:$i+11;
        li:nth-child(#{$x}) {
          $n:$i*5;
          $bgc:darken($color,$n); //顏色變暗
          background-color: $bgc;
          &:hover:before { //hover狀態(tài)顯示顏色編號
            content: "#{$bgc}";
            color: lighten($bgc,40);
            font-family: verdana;
            font-size: 8px;
            padding: 2px;
          }
        }
      }
    }
    //定義顏色變亮的 mixin
    @mixin swatchesLighten($color) {
      @for $i from 1 through 10 {
        $x:11-$i;
        li:nth-child(#{$x}) {
          $n:$i*5;
          $bgc:lighten($color,$n);
          background-color: $bgc;
          &:hover:before {
            content: "#{$bgc}";
            color: darken($bgc,40);
            font-family: verdana;
            font-size: 8px;
            padding: 2px;
          }
        }
      }
    }
    
    .swatches li {    
      width: 4.7619047619%;
      float: left;
      height: 60px;
      list-style: none outside none;
    }
    
    ul.red {
      @include swatchesLighten($redBase);
      @include swatchesDarken($redBase);
      li:nth-child(11) {
        background-color: $redBase;
      }
    }
    
    ul.orange {
      @include swatchesLighten($orangeBase);
      @include swatchesDarken($orangeBase);
      li:nth-child(11) {
        background-color: $orangeBase;
      }
    }
    
    ul.yellow {
      @include swatchesLighten($yellowBase);
      @include swatchesDarken($yellowBase);
      li:nth-child(11) {
        background-color: $yellowBase;
      }
    }
    
    ul.green {
      @include swatchesLighten($greenBase);
      @include swatchesDarken($greenBase);
      li:nth-child(11) {
        background-color: $greenBase;
      }
    }
    
    ul.blue {
      @include swatchesLighten($blueBase);
      @include swatchesDarken($blueBase);
      li:nth-child(11) {
        background-color: $blueBase;
      }
    }
    
    ul.purple {
      @include swatchesLighten($purpleBase);
      @include swatchesDarken($purpleBase);
      li:nth-child(11) {
        background-color: $purpleBase;
      }
    }
    
    ul.black {
      @include swatchesLighten($blackBase);
      @include swatchesDarken($blackBase);
      li:nth-child(11) {
        background-color: $blackBase;
      }
    }
    

6. Sass的@規(guī)則

  • @import

  • @media

    .sidebar {
      width: 300px;
      @media screen and (orientation: landscape) {
        width: 500px;
      }
    }
    @media screen {
      .sidebar {
        @media (orientation: landscape) {
          width: 500px;
        }
      }
    }
    
    $media: screen;
    $feature: -webkit-min-device-pixel-ratio;
    $value: 1.5;
    
    @media #{$media} and ($feature: $value) {
      .sidebar {
        width: 500px;
      }
    }
    
  • @extend

  • @at-root

  • @debug

  • @warn

  • @error

  • @content

    $small : 750px;
    @mixin  onsmall {
      @media  only screen and (max-width: $small){
        @content;
      }
    }
    
    .navbar-content{
      max-width:980px;
      @include onsmall {
        min-width:320px;
      }
    }
    
@mixin useRem($size){
  $device-list : 320px, 375px , 414px;
  html{
    @each $device in $device-list{
      @media screen and (min-width: $device){
        font-size: 100px * ($device/$size);
      }
    }
  }
}
@include useRem(750px);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市荤堪,隨后出現(xiàn)的幾起案子合陵,更是在濱河造成了極大的恐慌,老刑警劉巖澄阳,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拥知,死亡現(xiàn)場離奇詭異,居然都是意外死亡碎赢,警方通過查閱死者的電腦和手機低剔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來揩抡,“玉大人户侥,你說我怎么就攤上這事÷袜停” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵屋摔,是天一觀的道長烁设。 經(jīng)常有香客問我,道長钓试,這世上最難降的妖魔是什么装黑? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮弓熏,結(jié)果婚禮上恋谭,老公的妹妹穿的比我還像新娘。我一直安慰自己挽鞠,他們只是感情好疚颊,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著信认,像睡著了一般材义。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嫁赏,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天其掂,我揣著相機與錄音,去河邊找鬼潦蝇。 笑死款熬,一個胖子當著我的面吹牛深寥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贤牛,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼惋鹅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了盔夜?” 一聲冷哼從身側(cè)響起负饲,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎喂链,沒想到半個月后返十,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡椭微,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年洞坑,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝇率。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡迟杂,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出本慕,到底是詐尸還是另有隱情排拷,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布锅尘,位于F島的核電站监氢,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏藤违。R本人自食惡果不足惜浪腐,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望顿乒。 院中可真熱鬧议街,春花似錦、人聲如沸璧榄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽犹菱。三九已至拾稳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間腊脱,已是汗流浹背访得。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人悍抑。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓鳄炉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親搜骡。 傳聞我的和親對象是個殘疾皇子拂盯,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容

  • -------------------------一、控制指令--------------------------...
    夜幕小草閱讀 3,149評論 0 5
  • 基礎(chǔ) 聲明變量 普通變量 默認變量 變量覆蓋:只需要在默認變量之前重新聲明下變量即可 變量的調(diào)用 局部變量和全局變...
    Jill1231閱讀 1,282評論 0 1
  • 想想學習Sass時間也有差不多一年的光景了记靡。在這一年來的時間中谈竿,在GitHub不斷閱讀Sass相關(guān)的源碼。也在國外...
    小豌豆書吧閱讀 7,249評論 1 24
  • 1摸吠,使用save會在package.json中自動添加空凸。 2,npm install node-sass --sa...
    F2E_pyl閱讀 709評論 0 0
  • 在HTML中快標簽和行標簽分別為div和span寸痢;它們的顯示可以分為顯示塊級元素 block呀洲、行級元素 inl...
    放飛吧自我閱讀 566評論 1 4