@mySelector: banner; .@{mySelector} { font-weight: bold; }
变量是延迟加载的,在使用前不必定要先申明css
.brass { @var: 2; three: @var; //这是的值是3,同级别中最后一个,顺序无所谓 @var: 3; }
使用“+”,“+_”,合并同一个css样式的属性,前者用逗号隔开,后者用空格隔开。less
.mixin() { transform+_: scale(2); } .myclass { .mixin(); transform+_: rotate(15deg); } => .myclass { transform: scale(2) rotate(15deg); }
nav ul { &:extend(.inline); background: blue; } .inline { color: red; } => nav ul { background: blue; } .inline, nav ul { color: red; }
//混合“类”选择器或者“id”选择器 .a { color: red; } .mixin-class { .a(); } //不输出混合集 .my-mixin { color: black; } .my-other-mixin() { background: white; &:hover { border: 1px solid red; } } .class { .my-mixin; .my-other-mixin; } => .my-mixin { color: black; } .class { color: black; background: white; } .class:hover { border: 1px solid red; } //!important .foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; } .important { .foo() !important; } => .important { background: #f5f5f5 !important; color: #900 !important; }
//带参数的混合 .border_width1(@b_width){ border: solid blue @b_width; } .test_mix1{ .border_width1(5px); } => .test_mix1 { border: solid #0000ff 5px; } //参数有默认值的混合 .border_radius(@radius:5px){ border-radius: @radius; } .test_radius{ .border_radius(); height: 20px; } => .test_radius { border-radius: 5px; height: 20px; } //参数经过它的名称来引用 .mixin(@color: black; @margin: 10px; @padding: 20px) { color: @color; margin: @margin; padding: @padding; } .class1 { .mixin(@margin: 20px; @color: #33acfe); } => .class1 { color: #33acfe; margin: 20px; padding: 20px; }
.mixin(dark; @color) { color: darken(@color, 10%); } .mixin(light; @color) { color: lighten(@color, 10%); } .mixin(@_; @color) { display: block; } @switch: light; .class { .mixin(@switch; #888); }
有的时候,须要一次性将全部的参数所有传进去,此时使用@arguments能够更加方便。
//@arguments表明传进全部的参数 .border_arg(@c:red,@w:3px,@x:solid){ border:@arguments; } .test_arg{ .border_arg(40px); }
有些状况下,咱们不须要less中的某些内容被自动编译,而是保留下来原样放到CSS中,此时能够使用~''oop
.test_avoid{ width: ~'(300px-100px)'; } => .test_avoid { width: (300px-100px); }
.test{ height: calc(~'100% - 100px'); } => .test { height: calc(100% - 100px); }
.loop(@counter) when (@counter > 0) { .loop((@counter - 1)); width: (10px * @counter); } div { .loop(3); // 调用循环 } => div { width: 10px; width: 20px; width: 30px; }