Sass 基礎(四),sass基礎

來源:互聯網
上載者:User

Sass 基礎(四),sass基礎

當你想設定屬性值的時候你可以使用字串插入進來,另一個使用的用法是構建一個選取器。
      @mixin generate-sizes($class,$small,$medium,$big){
                  .#{$class}-small {font-size:$small;}
                  .#{$class}-medium{ font-size:$medium}
                  .#{$class}-big{ font-size:$big;}
            }
      @include gencerate-sizes("header-text",12px,20px,40px);
      編譯出來的css
        .header-text-small { font-size:12px;}
        .header-text-medium{ font-size:20px;}
        .header-text-big{ font-size:40px;}
      第一個限制,這可能會很刪除用於 Sass 變數的插值。
        $margin-big:40px;
        $margin-medium:20px;
        $margin-small:12px;
        @mixin set-value($size){
            margin-top:$margin-#{$size};
        }
        .login-box{
            @include set-value(big);
        }
        上面的代碼編譯出來,
            error style.scss(Line 5:Undefined variable:"$margin-".)
            所以,#{}文法並不能是隨處可用的,你也不能在mixin中調用
            @mixin updated-status{
                  margin-top:20px;
                  background:#F00;
              }
            $flag:"status";
            .navigation{
              @include updated-#{$flag}
            }
      上面的代碼在編譯成css時同樣會報錯;
          error style.scss(Line7:Invalid css after"...nclude updated-*":expected"}",was"#{$flag};"
        幸運的是,可以使用 @extend 中使用插值。例如:
          %updated-status{
              margin-top:20px;
              background:#f00;
            }
          .selected-status{
              font-weight:bold;
           }
          $flag:"status";
          .nacigation{
                @extend $updated -#{$flag};
                @extend .selected-#{$flag};
            }
          編譯出來的css
              .navigation{
                margin-top:20px;
                background:#f00;
              }
              .selected-status, .navigation{
                  font-weight:bold;
              }
注釋
  在Sass 中注釋有兩種方式
    1.類似css 的注釋方式,使用“/*”開頭,結尾使用“*/”
    2.類似JavaScript的注釋方式 使用“//”
    //定義一個預留位置
      %mt5 {
          margin-top: 5px;
      }
    /*調用一個預留位置*/
    .box {
        @extend %mt5;
      }
    編譯出來的css
    .box{
        margin-top:5px;
    }
    /*調用一個預留位置*/

資料類型
    Sass 和JavaScript 語言類型,也具有自己的資料類型,在Sass中包含以下幾種資料類型。
    數字:如,1,2,13,10px,
    字串:有引號字串或無引號字串,如“foo”,'bar',baz;
    顏色:如,blue,#04a3f9,rgba(255,0,0,0.5);
    布爾值:如,true,false;
    空值:如,null;
    值列表:用空格或者逗號分開,如,1.em 1em 0.2em Helvetica, Arial, sans-serif。

    SassScrip也支援其他css 屬性值(property value),比如Unicode 範圍,或者!important 聲明。然而
    Sass 不會特殊對待這些屬性值,一律視為無引號字串。


字串
    SassS支援CSS的兩種字串類型:
    有引號字串(quoted strings),如“Lucida Grande”,'http://sass-lang.com';
    無引號字串(unquoted strings),如sans-serifbold。
    使用 #{ }插值語句 (interpolation) 時,有引號字串將被編譯為無引號字串,
    這樣方便了在混合指令 (mixin) 中引用選取器名。
    @mixin firefox-message($selector){
        body.firefox-message($selector){
            content:"Hi Firefox users!";
          }
    }
    @include firefox-message(".header");
    編譯為:
        body.firefox .header:before(content"hi,Firefox users!";)
    值列表
        所謂值列表(lists)是指Sass 如何出來css中;
      margin:10px 15px 0 0
    或者:
      font-face:Helvetica,Arial,sans-serif
    像上面這樣通過空格或者逗號分隔的一系列的值。
        Sass列表函數(Sass list functions)賦予了值列表功能(Sass 進級會有講解)
    1.nth函數(nth function) 可以直接存取值列表中的某一項;
    2.join函數(join function)可以將多個值列表串連在一起;
    3.append函數(append function)可以在值列表中添加值;
    4.@each規則(@each rule) 則能夠給值列表中的每個項目添加樣式。
      值列表中可以再包含值列表,比如 1px 2px, 5px 6px 是包含 1px 2px 與 5px 6px 兩個值列表的值列表。
      如果內外兩層值列表使用相同的分隔方式,要用圓括弧包裹內層,所以也可以寫成 (1px 2px) (5px 6px)。
      當值列表被編譯為 CSS 時,Sass 不會添加任何圓括弧,因為 CSS 不允許這樣做。
      可以用 () 表示空的列表,這樣不可以直接編譯成 CSS,比如編譯 font-family: ()時,Sass 將會報錯。
      如果值列表中包含空的值列表或空值,編譯時間將清除空值,比如 1px 2px () 3px 或 1px 2px null 3px。
加法
    在css中能做運算的,到目前為止僅有calc()函數可行,但在Sass 中,運算只是在基本特性之一,在Sass
    中可以做各種數學計算。
    (一),加法
    加法運算是Sass中運算的一種,在變數或屬性中都可以做加法運算。如:
      .box{
          width:px +8in;
        }
      編譯出來css
      .box{
        width;788px;
      }
    對於攜帶不同類型的單位時,在 Sass 中計算會報錯
     如:
      .box{
          width:20px +1em;
       } 或報錯 “Incompatible units: 'em' and ‘px'.”
減法
    $full-width:960px;
    $sidebar-width:200px;
    .content{
      width:$full-width - $sidebar-width;
    }
    編譯出來的css 如下
      .content{ width:760px}
    同樣的,運算時碰到不同類型的單位時,編譯也會報錯
      $full-width:960px;
      .content{
          width:$full-width - 1em;
      }
    編譯器報“Incompatible units: 'em' and ‘px’.”錯誤。
乘法
    Sass中的乘法運算和前面介紹的加法和減法運算略有不同,雖然他也能夠支援多種單位(比如em ,px,%)
      如:.box{
          width:10px *2px;
        }
    編譯的時候報錯“20px*px isn't a valid CSS value.”錯誤資訊
    上面的執行個體可以修改成:
      .box{
        width:10px *2;
      }
    編譯出來的css
      .box{
        width:20px;
      }
除法
    先來看一個簡單的樣本
      .box{
        width:100px / 2;
      }
    編譯出來的css 如下
      .box{
        width:100px / 2;
        }
    只需要加上()即可:
    .box{
      width:(100px / 2)
     }
    編譯出來
    .box{
        width:50px;
    }
    下面的樣本:
    .box{
      width:100px / 2 + 2in;
    }
    編譯出來css
    .box{
      width:242px;
    }
  在sass除法運算中,當變數進行除法運算時“/”符號也會被自動識別成除法。
    $width:1000px;
    $nums:10;
    .item{
      width:$width /10;
    }
  編譯出來的css
    .item{
      width;100px;
    }

  .list{
      width:$width / $nums;
  }
    編譯出來的css
  .item{
    width:100px;
  }
  .list{
    width:100px;
  }
變數計算
    在Sass中除了可以使用數值進行元素之外,還可以使用變數進行計算,
    例如:
      $content-width:720px;
      $content-width:220px;
      $gutter:20px;
    .container{
      width:$conter-width + $sidebar-width + $gutter;
      margin: 0 auto;
      }
   編譯出來的css
    .container{
        width:960px;
        margin:0 auto;
    }
數字運算
    在Sass運算中數字運算是較為常見的,數字包括前面介紹的,加減乘除。
    例如:
    .box{
      width:((220px + 720px)-11*20)/12;
    }
    編譯出來的css
    .box{
      width:60px;
    }
顏色運算
    所有算數運算都支援顏色值,並且是分段運算的。也就是說,紅,綠和藍各顏色分段單獨進行運算。
    如:
      p{
        color:#010203 + #040506;
      }
    計算公式為01+04 =05, 02 +05 =07 和 03 + 06 = 09,
    如此編譯出來的css為:
      p{
        color:#050709;
      }
    算數運算也能將數字和顏色值一起運算,同樣也是分段運算的。
      p{
        color:#010203 * 2;
      }
    計算公式為01 * 2 =02, 02 *2 = 04 和03*2 = 06
      p{
        color:#020306;
      }
字元運算
  在Sass中可以通過加法符號 “+”來對字串進行串連。 例如
  $content:"Hello" + "" +"Sass!";
   .box:before{
      content:"#{$content}";
    }
  編譯出來的css
    .box:before{
      content:"Hello Sass!";
    }
  除了在變數中做字串串連運算之外,還可以直接通過 +,把字串串連在一起。
    div{
      cursor:e + -resize;
    }
  編譯出來的css
    div {
      cursor:e-resize;
    }
   注意,如果有引號的字串被添加了一個沒有引號的字串 (也就是,帶引號的字串在 + 符號左側),
   結果會是一個有引號的字串。 同樣的,如果一個沒有引號的字串被添加了一個有引號的
   字串 (沒有引號的字串在 + 符號左側), 結果將是一個沒有引號的字串。
   例如:
    p:before{
      content:"Foo" +Bar;
      font-family:sans- +"serif";
    }
  編譯出來的的css;
    p:before{
      content:"foo Bar";
      font-family:sans-serif;
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.