編寫SASS的一些技巧,編寫SASS技巧

來源:互聯網
上載者:User

編寫SASS的一些技巧,編寫SASS技巧

  • 更好的為變數命名

變數是Sass中最簡單的特性之一,但有時候也會使用不當。建立網站範圍內有語義化的變數,是不可或缺的工作。如果命名不好,他會變得難以理解和重複使用。

這裡有一些命名變數的小技巧,提供參考:

  • 命名變數時不要含糊不清
  • 堅持一種命名規則(Modular, BEM等等)
  • 確定變數的使用是有道理的

這有一個好的樣本:

$orange: #ffa600; $grey: #f3f3f3; $blue: #82d2e5;$link-primary: $orange;$link-secondary: $blue;$link-tertiary: $grey;$radius-button: 5px;$radius-tab: 5px;

這個是不好的樣本:

$link: #ffa600;$listStyle: none;$radius: 5px;

  

  • 減少Mixins的使用

 

Mixins是實現代碼塊的一種偉大方式,可以在一個網站內多次使用。然而,@include定義好的Mixins和在CSS代碼中複製、粘貼沒什麼不一樣。它將會讓你的CSS代碼產生很多重複的代碼,讓你的檔案變得越來越臃腫。

 

到目前為止,Mixins只適合那種需要通過傳遞參數來快速建立樣式的情形。

 

例如:

@mixin rounded-corner($arc) {    -moz-border-radius: $arc;    -webkit-border-radius: $arc;    border-radius: $arc;  }

rounded-corner這個Mixins可以在任何情況下使用,僅僅通過改變其參數$arc的值,將得到不同的代碼:

.tab-button {     @include rounded-corner(5px); }.cta-button {     @include rounded-corner(8px); }

像這樣使用Mixins是不明智的:

@mixin cta-button {    padding: 10px;    color: #fff;    background-color: red;    font-size: 14px;    width: 150px;    margin: 5px 0;    text-align: center;    display: block;}

這個Mixins沒有傳遞任何參數,更建議使用%placeholder來建立,這也是接下來要說的下一點。

  • 擁抱Placeholder

與Mixins不同,%placeholder也可以多次使用,而且不會產生重複的代碼。這使得輸入的CSS更友好,更乾淨。

%bg-image {    width: 100%;    background-position: center center;    background-size: cover;    background-repeat: no-repeat;}.image-one {    @extend %bg-image;    background-image:url(/img/image-one.jpg");}.image-two {    @extend %bg-image;    background-image:url(/img/image-two.jpg");}

編譯出來的CSS:

.image-one, .image-two {    width: 100%;    background-position: center center;    background-size: cover;    background-repeat: no-repeat;}.image-one {    background-image:url(/img/image-one.jpg") ;}.image-two {    background-image:url(/img/image-two.jpg") ;}

多個選取器運用了相同的%placeholder也只會輸出一次代碼。沒有引用的%placeholder是不會輸出任何CSS代碼。

和之前的Mixins配合在一起使用,既可保持Mixins靈活性,而且還可以保持代碼的簡潔與乾淨。

/* PLACEHOLDER ============================================= */%btn {    padding: 10px;    color:#fff;    curser: pointer;    border: none;    shadow: none;    font-size: 14px;    width: 150px;    margin: 5px 0;    text-align: center;    display: block;}/* BUTTON MIXIN ============================================= */@mixin  btn-background($btn-background) {    @extend %btn;    background-color: $btn-background;    &:hover {        background-color: lighten($btn-background,10%);    }}/* BUTTONS============================================= */.cta-btn {    @include btn-background(green);}.main-btn {    @include btn-background(orange);}.info-btn {    @include btn-background(blue);}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.