武林網(www.49028c.com)文章簡介:less用變量 (variables),引用(mixins),表達式(operations),嵌套規則(nested rules)來擴展css開發.
less官方網址:
下面就來介紹下吧
less用變量 (variables),引用(mixins),表達式(operations),嵌套規則(nested rules)來擴展css開發
變量 (variables)
重復使用的值可以定義成變量的形式,方便更改哈
例子如下:
#header { color: #4D926F;}h2 { color: #4D926F;}@brand_color: #4D926F;#header { color: @brand_color;}h2 { color: @brand_color;}
引用(mixins)
在一個類中可以引用另一個類的名稱做為該類的屬性。
#header { -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;}#footer { -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;}.rounded_corners { -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;}#header { .rounded_corners;}#footer { .rounded_corners;}嵌套規則(nested rules)
以前我們開發的css的時候selector的繼承都要寫的很長 ,用less后。我們可以用更簡潔,直觀的方式來寫css
實例如下:
#header { color: red;}#header a { font-weight: bold; text-decoration: none;}#header { color: red; a {font-weight: bold;text-decoration: none; }}
表達式(operations)
一些單元之間可能有些值要成比例,比如寬高,顏色值
我們都可以用表達式來計算實現
#header { color: #333; border-left: 1px; border-right: 2px;}#footer { color: #222;}@the-border: 1px;@base-color: #111;#header { color: @base-color * 3; border-left: @the-border; border-right: @the-border * 2;}#footer { color: @base-color + #111;}