Less 全称:Leaner Style Sheets,使用Less可以大大减少开发人员的编码量,提高CSS的可维护性
学习文档
Less(en-us)
Less(zh-cn)
变量
1 2 3 4 5 6 7 8 9 10
| @nice-blue: #5B83AD;
#header { color: @nice-blue; }
#header { color: #5B83AD; }
|
混合(Mixin)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| .bordered { border-top: dotted 1px black; border-bottom: solid 2px black; } .post a { color: red; .bordered; }
.post a { color: red; border-top: dotted 1px black; border-bottom: solid 2px black; }
|
嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #header { color: black; .navigation { font-size: 12px; } .logo { width: 300px; } }
#header { color: black; } #header .navigation { font-size: 12px; } #header .logo { width: 300px; }
.clearfix { display: block; zoom: 1;
&:after { content: " "; display: block; font-size: 0; height: 0; clear: both; visibility: hidden; } }
.clearfix { display: block; zoom: 1; } .clearfix:after { content: " "; display: block; font-size: 0; height: 0; clear: both; visibility: hidden; }
|
延伸(extend)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| nav ul { &:extend(.inline); background: blue; } .inline { color: red; }
nav ul { background: blue; } .inline, nav ul { color: red; }
nav ul { &:extend(.inline); background: blue; }
nav ul:extend(.inline) { background: blue; }
nav ul:extend(.inline all) { background: blue; }
.my-inline-block() { display: inline-block; font-size: 0; } .thing1 { .my-inline-block; } .thing2 { .my-inline-block; }
.thing1 { display: inline-block; font-size: 0; } .thing2 { display: inline-block; font-size: 0; }
.my-inline-block { display: inline-block; font-size: 0; } .thing1 { &:extend(.my-inline-block); } .thing2 { &:extend(.my-inline-block); }
.my-inline-block, .thing1, .thing2 { display: inline-block; font-size: 0; }
.mixin() { transform+_: scale(2); } .myclass { .mixin(); transform+_: rotate(15deg); }
.myclass { transform: scale(2) rotate(15deg); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| .component { width: 300px; @media (min-width: 768px) { width: 600px; @media (min-resolution: 192dpi) { background-image: url(/source/images/retina2x.png); } } @media (min-width: 1280px) { width: 800px; } }
.component { width: 300px; } @media (min-width: 768px) { .component { width: 600px; } } @media (min-width: 768px) and (min-resolution: 192dpi) { .component { background-image: url(/source/images/retina2x.png); } } @media (min-width: 1280px) { .component { width: 800px; } }
|
运算
插入文本
1
| ~"anything" 或 ~'anything'
|
封装和调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #bundle() { .button { display: block; border: 1px solid black; background-color: grey; &:hover { background-color: white } } .tab { ... } .citation { ... } }
#header a { color: orange; #bundle > .button; }
.foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; } .unimportant { .foo(); } .important { .foo() !important; }
.unimportant { background: #f5f5f5; color: #900; } .important { background: #f5f5f5 !important; color: #900 !important; }
.mixin(@color) { color-1: @color; } .mixin(@color; @padding: 2) { color-2: @color; padding-2: @padding; } .mixin(@color; @padding; @margin: 2) { color-3: @color; padding-3: @padding; margin: @margin @margin @margin @margin; } .some .selector div { .mixin(#008000); }
.some .selector div { color-1: #008000; color-2: #008000; padding-2: 2; }
#namespace when (@media = mobile) { .mixin() { } }
#namespace { .mixin() when (@media = mobile) { } }
.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }
.mixin (@a) when (@a > 10), (@a < -10) { ... }
.mixin (@b) when not (@b > 0) { ... }
|
注释
引入
1 2 3 4
| @import "library.less"; @import "typo.css";
|
编译器
Visual Studio 2013之后移除了Less的实时编译功能,可以在这里搜索 Web Compiler 下载最新版安装作为替代品