使用LESS構建你的前端CSS(by vczero)

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   io   strong   

一、為什麼要用LESS

很多時候我們覺得CSS只是樣式框定,前端的重頭戲是JavaScript;其實,很多能用HTML + CSS實現的效果的效率遠比使用JS來的划算。LESS是什嗎?我們為什麼要使用LESS呢?Less 是一個Css 先行編譯器,意思指的是它可以擴充Css語言,添加功能如允許變數(variables),混合(mixins),函數(functions) 和許多其他的技術,讓你的Css更具維護性,主題性,擴充性。你是否曾經遇到過這種情境:

定義了一個DIV的寬度是980px,到了另一個頁面也有一個DIV是980px;其實這兩個DIV的寬度需要保持一致。

       #div1{width:980px; height:300px; font-size:16px;}       #div2{width:980px; color:red; height:40px;}

也許你說,可以使用一個類來代表寬度嘛,但是時間一長,你不小心改動了width這個類,造成了整個頁面其他的地方都變了,你是否還要去檢查你的HTML呢?這種DIV的語義化也沒有得到充分的體現,沒有將width直接寫進#div1和#div來的直觀。我們心微微動一下,要是有變數就好了。對!這就是LESS最基本的初衷。

二、使用Node.js構建你的LESS編譯環境

可以查看官網LESS好多使用方式,可以在瀏覽器直接使用LESS,但是,不建議這樣使用,因為瀏覽器需要載入LESS的解析JS檔案去解析LESS檔案,這樣在一定程度上消耗了瀏覽器的資源,降低了web程式的效能。所以推薦使用Node.js編譯LESS指令碼。Node.js的安裝和說明這裡不說了。

安裝LESS架構,這裡選擇npm工具全域安裝。

$ npm install -g less

安裝完畢後,你就會發現如此迅速,OK了。cmd中運行lessc命令檢測是否成功,如果成功就可以看到以下options。

三、編譯第一個LESS指令碼

LESS檔案的尾碼名可以是.css,也可以是.less。建議使用.css,因為使用一些編輯器可以高亮一些CSS屬性。比如你寫了一個less指令碼“main_0.css”:

 1 @header_height:3em;  2 @float_left:left; 3 @float_right:right; 4  5 body{ 6     margin: 0; 7 } 8  9 .header{10     height: @header_height;11     width: 100%;12     background-color: #F0F0F0;13 }14 15 #header_back{16     height:@header_height;17     width:30%;18     float:@float_left;19     border: 1px solid red;20 }21 22 #header_title{23     height:@header_height;24     float:@float_right;25     width:69%; 26     border: 1px solid red;27 }28 29 #header_title{30     text-align: center;31 }

其實,你看到檔案已經很明白了,@header_height就是一個變數...代碼很簡單,OK。這段代碼載入到HTML中是跑不起來的,因為我們沒有將它編譯為可用的CSS,即瀏覽器可以解析的CSS檔案。同樣,使用LESS工具。開啟cmd:

$ lessc main_0.css > main.css

很明顯,這就是將main_0.css這個less指令碼編譯成瀏覽器可用的main.css檔案,編譯後的結果如下:

 1 body { 2   margin: 0; 3 } 4 .header { 5   height: 3em; 6   width: 100%; 7   background-color: #F0F0F0; 8 } 9 #header_back {10   height: 3em;11   width: 30%;12   float: left;13   border: 1px solid red;14 }15 #header_title {16   height: 3em;17   float: right;18   width: 69%;19   border: 1px solid red;20 }21 #header_title {22   text-align: center;23 }

四、熟悉LESS文法規則,開始你的CSS“編程”之旅

特意加了“編程”二字,讓CSS運行在你的邏輯中吧。具體的LESS基本文法如下:

(1)@color: #4D926F; 定義一個變數@color,變數名就是@color,像PHP的定義吧。

(2)變數範圍的就近原則。例如:

 1  @width : 20px;  2  #homeDiv {  3    @width : 30px;  4    #centerDiv{  5        width : @width;// 此處應該取最近定義的變數 width 的值 30px  6               }  7  }  8  #leftDiv {  9      width : @width; // 此處應該取最上面定義的變數 width 的值 20px 10 11  }

編譯後的CSS是:

1 #homeDiv #centerDiv { 2   width: 30px; 3  } 4  #leftDiv { 5   width: 20px; 6  }

(3)可嵌入class塊,例如下面的.borderRadius就是一個樣式塊。其中.borderRadius(@radius)允許傳入預設值,改為->.borderRadius(@radius:25px)

 1 // 定義一個樣式選取器 2  .borderRadius(@radius){  3  -moz-border-radius: @radius;  4  -webkit-border-radius: @radius;  5  border-radius: @radius;  6  }  7  // 使用已定義的樣式選取器 8  #header {  9  .borderRadius(10px); // 把 10px 作為參數傳遞給樣式選取器10  } 11  .btn { 12  .borderRadius(3px);// // 把 3px 作為參數傳遞給樣式選取器13 14  }

編譯轉化後的CSS代碼如下:

 #header {  -moz-border-radius: 10px;  -webkit-border-radius: 10px;  border-radius: 10px;  }  .btn {  -moz-border-radius: 3px;  -webkit-border-radius: 3px;  border-radius: 3px;  }

(4)具有命名空間的特性。

(5)理解CSS的嵌套規則。

(6)支援基本的運算。

(7)LESS的內建函數。

 

 ——前端工程如此浩大,孜孜不倦,孜孜不倦

參考:

http://lesscss.org/

https://github.com/less/less.js

http://www.lesscss.net/

http://www.ibm.com/developerworks/cn/web/1207_zhaoch_lesscss/

 

聯繫我們

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