// LESS.rounded-corners (@radius: 5px) {border-radius: @radius;-webkit-border-radius: @radius;-moz-border-radius: @radius;}#header {.rounded-corners;//不带参数引用,则使用默认值}#footer {.rounded-corners(10px);//传递参数}/* 生成的 CSS */#header {border-radius: 5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;}#footer {border-radius: 10px;-webkit-border-radius: 10px;-moz-border-radius: 10px;}
.mixin (dark, @color) {color: darken(@color, 10%);}.mixin (light, @color) {color: lighten(@color, 10%);}.mixin (@_, @color) {//通配变量@_ ,写公共的部分代码display: block;}//现在,如果运行:@switch: light;.class {.mixin(@switch, #888);}//就会得到下列CSS:.class {color: #a2a2a2;--匹配了lightdisplay: block;}
// LESS@the-border: 1px;@base-color: #111;@red: #842210;#header {color: @base-color * 3;border-left: @the-border;border-right: @the-border * 2;}#footer {color: @base-color + #003300;border-color: desaturate(@red, 10%);}/* 生成的 CSS */#header {color: #333;border-left: 1px;border-right: 2px;}#footer {color: #114411;border-color: #7d2717;}
// LESS#header {h1 {font-size: 26px;font-weight: bold;}p { font-size: 12px;a { text-decoration: none;&:hover { border-width: 1px }}}}/* 生成的 CSS */#header h1 {font-size: 26px;font-weight: bold;}#header p {font-size: 12px;}#header p a {text-decoration: none;}#header p a:hover {border-width: 1px;}
a{//&代表它的上一层选择器&:hover{color:red}}
@arguments包含了所有传递进来的参数.
.box-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) {box-shadow: @arguments;-moz-box-shadow: @arguments;-webkit-box-shadow: @arguments;}.box-shadow(2px, 5px);将会输出:box-shadow: 2px 5px 1px #000;-moz-box-shadow: 2px 5px 1px #000;-webkit-box-shadow: 2px 5px 1px #000;
#bundle中定义一些属性集之后可以重复使用:
#bundle {.button () {display: block;border: 1px solid black;background-color: grey;&:hover { background-color: white }}.tab { ... }.citation { ... }}//你只需要在 #header a中像这样引入 .button:#header a {color: orange;#bundle > .button;}
附:less中文文档:http://www.bootcss.com/p/lesscss/
原文地址:http://blog.csdn.net/geyao2015/article/details/44308083