1. 程式人生 > 程式設計 >sass 常用備忘案例詳解

sass 常用備忘案例詳解

一、變數

所有變數以$開頭

$font_size: 12px;
.container{
    font-size: $font_size;
}

如果變數巢狀在字串中,需要寫在#{}中

$side : left;
.rounded {
    border-#http://www.cppcns.com{$side}: 1px solid #000;
}

二、巢狀

層級巢狀

.container{
    display: none;
    .header{
        width: 100%;
    }
}

屬性巢狀,注意,border後需要加上冒號:

.container {
    border: {
        width: 1px;
    }
}

 可以通過&引用父元素,常用在各種偽類

.link{
    &:hover{ 
        color: green;
    }  
}

三、mixin

簡單理解,是可以重用的程式碼塊,通過@include 命令

// mixin
@mixin focus_style {
    outline: none;
}
div {
    @include focus_style; 
}

編譯後生成

div {
  outline: none; }
reTaHQHP

還可指定引數、預設值

// 引數、預設值
@mixin the_height($h: 200px) {
        height: $h;
}
.box_default {
        @include the_height;
}
.box_not_default{
        @include the_height(100px);
}

編譯後生成

.box_default {
  height: 200px; }

.box_not_default {
  height: 100px; }

四、繼承

通過@extend,一個選擇器可以繼承另一個選擇器的樣式。例子如下

// 繼承
.class1{
        float: left;
}
.class2{
        @extend .class1;
        width: 200px;
}

編譯後生成

.class1,.class2 {
  float: left; }

.class2 {
  width: 200px; }

五、運算

直接上例子

.container{
        position:http://www.cppcns.com
relative; height: (200px/2); width: 100px + 200px; left: 50px * 2; top: 50px - 10px; }

編譯後生成

.container {
  position: relative;
  height: 100px;
  width: 300px;
  left: 100px;
  top: 40px; }

插入檔案

用@import 來插入外部檔案

@import "outer.s";

也可插入普通css檔案

@import "outer.css";

自定義函式

通過@function 來自定義函式

@function higher($h){
        @return $h * 2;
}
.container{
        height: higher(100px);
}

編譯後輸出

.container {
  height: 200px; 
}

註釋

兩種風格的註釋

// 單行註釋,編譯後消失
/* 標準的CSS註釋,會保留到編譯後的程式碼中 */

如果重要的註釋,壓縮編譯後還想保留,可在 /* 後面加上 !

/*!
重要註釋,壓縮編譯也不會消失
*/

參考:

http://www.ruanyifeng.com/blog/2012/06/sass.html

到此reTaHQHP這篇關於sass 常用備忘案例詳解的文章就介紹到這了,更多相關sass 常用備忘內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!