edit the markdown source for "features-overview"

as an extension to css, less is not only backwards compatible with css, but the extra features it adds use existing css syntax. this makes learning less a breeze, and if in doubt, lets you fall back to vanilla css.


these are pretty self-explanatory:

@nice-blue: #5b83ad;@light-blue: @nice-blue + #111;#header { color: @light-blue;}


#header { color: #6c94be;}

note that variables are actually "constants" in that they can only be defined once.


mixins are a way of including ("mixing in") a bunch of properties from one rule-set into another rule-set. so say we have the following class:

.bordered { border-top: dotted 1px black; border-bottom: solid 2px black;}

and we want to use these properties inside other rule-sets. well, we just have to drop in the name of the class where we want the properties, like so:

#menu a { color: #111; .bordered;}.post a { color: red; .bordered;}

the properties of the .bordered class will now appear in both #menu a and .post a. (note that you can also use #ids as mixins.)

learn more

nested rules

less gives you the ability to use nesting instead of, or in combination with cascading. let's say we have the following css:

#header { color: black;}#header .navigation { font-size: 12px;}#header .logo { width: 300px;}

in less, we can also write it this way:

#header { color: black; .navigation {  font-size: 12px; } .logo {  width: 300px; }}

the resulting code is more concise, and mimics the structure of your html.

you can also bundle pseudo-selectors with your mixins using this method. here's the classic clearfix hack, rewritten as a mixin (& represents the current selector parent):

.clearfix { display: block; zoom: 1; &:after {  content: " ";  display: block;  font-size: 0;  height: 0;  clear: both;  visibility: hidden; }}

see also

media query bubbling and nested media queries

media queries can be nested in the same way as selectors. selectors are copied into the body of the media query:

.screencolor{ @media screen {  color: green;  @media (min-width:768px) {  color: red;  }  } @media tv {  color: black; }}


@media screen { .screencolor {  color: green; }}@media screen and (min-width: 768px) { .screencolor {  color: red; }}@media tv { .screencolor {  color: black; }}


any number, color or variable can be operated on. here are a couple of examples:

@base: 5%;@filler: @base * 2;@other: @base + @filler;color: #888 / 4;background-color: @base-color + #111;height: 100% / 2 + @filler;

the output is pretty much what you expect—less understands the difference between colors and units. if a unit is used in an operation, like in:

@var: 1px + 5;

less will use that unit for the final output—6px in this case.


less provides a variety of functions which transform colors, manipulate strings and do maths. they are documented fully in the function reference.

using them is pretty straightforward. the following example uses percentage to convert 0.5 to 50%, increases the saturation of a base color by 5% and then sets the background color to one that is lightened by 25% and spun by 8 degrees:

@base: #f04615;@width: 0.5;.class { width: percentage(@width); // returns `50%` color: saturate(@base, 5%); background-color: spin(lighten(@base, 25%), 8);}

namespaces & accessors

(not to be confused with css @namespace or namespace selectors).

sometimes, you may want to group your mixins, for organizational purposes, or just to offer some encapsulation. you can do this pretty intuitively in less, say you want to bundle some mixins and variables under #bundle, for later reuse or distributing:

#bundle { .button {  display: block;  border: 1px solid black;  background-color: grey;  &:hover { background-color: white  } } .tab { ... } .citation { ... }}

now if we want to mixin the .button class in our #header a, we can do:

#header a { color: orange; #bundle > .button;}

note that variables declared within a namespace will be scoped to that namespace only and will not be available outside of the scope via the same syntax that you would use to reference a mixin (#namespace > .mixin-name). so, for example, you can't do the following: (#namespace > @this-will-not-work).


scope in less is very similar to that of programming languages. variables and mixins are first looked for locally, and if they aren't found, the compiler will look in the parent scope, and so on.

@var: red;#page { @var: white; #header {  color: @var; // white }}

variables and mixins do not have to be declared before being used so the following less code is identical to the previous example:

@var: red;#page { #header {  color: @var; // white } @var: white;}

see also


both block-style and inline comments may be used:

/* one hell of a blockstyle comment! */@var: red;// get in line!@var: white;


importing works pretty much as expected. you can import a .less file, and all the variables in it will be available. the extension is optionally specified for .less files.

@import "library"; // library.less@import "typo.css";

edit the markdown source for "variables"

control commonly used values in a single location.


it's not uncommon to see the same value repeated dozens if not hundreds of times across your stylesheets:

a,.link { color: #428bca;}.widget { color: #fff; background: #428bca;}

variables make your code easier to maintain by giving you a way to control those values from a single location:

// variables@link-color:  #428bca; // sea blue@link-color-hover:  darken(@link-color, 10%);// usagea,.link { color: @link-color;}a:hover { color: @link-color-hover;}.widget { color: #fff; background: @link-color;}

variable interpolation

the examples above focused on using variables to control values in css rules, but they can also be used in other places as well, such as selector names, property names, urls and @import statements.


version: 1.4.0

// variables@myselector: banner;// usage.@{myselector} { font-weight: bold; line-height: 40px; margin: 0 auto;}

compiles to:

.banner { font-weight: bold; line-height: 40px; margin: 0 auto;}


// variables@images: "../img";// usagebody { color: #444; background: url("@{images}/white-sand.png");}

import statements

version: 1.4.0

syntax: @import "@{themes}/tidal-wave.less";

note that currently, only variables which have been declared in the root or current scope will be considered and that only the current file and calling files will be considered when looking for a variable.this means that this usage is typically limited to when you inject a variable into the compile process or define a variable at the beginning of your root file.

when you are importing a css file and not using the inline option (e.g. the import statement will be kept intact) then the above does not apply.


// variables@themes: "../../src/themes";// usage@import "@{themes}/tidal-wave.less";


version: 1.6.0

@property: color;.widget { @{property}: #0ee; background-@{property}: #999;}

compiles to:

.widget { color: #0ee; background-color: #999;}

variable names

it is also possible to define variables with a variable name:

@fnord: "i am fnord.";@var:  "fnord";content: @@var;

which compiles to:

content: "i am fnord.";

lazy loading

variables are lazy loaded and do not have to be declared before being used.

valid less snippet:

.lazy-eval { width: @var;}@var: @a;@a: 9%;

this is valid less too:

.lazy-eval-scope { width: @var; @a: 9%;}@var: @a;@a: 100%;

both compile into:

.lazy-eval-scope { width: 9%;}

when defining a variable twice, the last definition of the variable is used, searching from the current scope upwards. this is similar to css itself where the last property inside a definition is used to determine the value.

for instance:

@var: 0;.class { @var: 1; .brass {  @var: 2;  three: @var;  @var: 3; } one: @var;}

compiles to:

.class { one: 1;}.class .brass { three: 3;}

default variables

we sometimes get requests for default variables - an ability to set a variable only if it is not already set. this feature is not required because you can easily override a variable by putting the definition afterwards.

for instance:

// library@base-color: green;@dark-color: darken(@base-color, 10%);// use of library@import "library.less";@base-color: red;

this works fine because of lazy loading - base-color is overidden and dark-color is a dark red.

edit the markdown source for "extend"

extend is a less pseudo-class which merges the selector it is put on with ones that match what it references.

released v1.4.0

nav ul { &:extend(.inline); background: blue;}

in the rule set above, the :extend selector will apply the "extending selector" (nav ul) onto the .inline class wherever the .inline class appears. the declaration block will be kept as-is, but without any reference to the extend (because extend isn't css).

so the following:

nav ul { &:extend(.inline); background: blue;}.inline { color: red;}


nav ul { background: blue;}.inline,nav ul { color: red;}

notice how the nav ul:extend(.inline) selector gets output as nav ul - the extend gets removed before output and the selector block left as-is. if no properties are put in that block then it gets removed from the output (but the extend still may affect other selectors).

extend syntax

the extend is either attached to a selector or placed into a ruleset. it looks like a pseudoclass with selector parameter optionally followed by the keyword all:


.a:extend(.b) {}// the above block does the same thing as the below block.a { &:extend(.b);}
.c:extend(.d all) { // extends all instances of ".d" e.g. ".x.d" or ".d.x"}.c:extend(.d) { // extends only instances where the selector will be output as just ".d"}

it can contain one more classes to extend, separated by commas.


.e:extend(.f) {}.e:extend(.g) {}// the above an the below do the same thing.e:extend(.f, .g) {}

extend attached to selector

extend attached to a selector looks like an ordinary pseudoclass with selector as a parameter. a selector can contain multiple extend clauses, but all extends must be at the end of the selector.

  • extend after the selector: pre:hover:extend(div pre).
  • space between selector and extend is allowed: pre:hover :extend(div pre).
  • multiple extends are allowed: pre:hover:extend(div pre):extend(.bucket tr) - note this is the same as pre:hover:extend(div pre, .bucket tr)
  • this is not allowed: pre:hover:extend(div pre).nth-child(odd). extend must be last.

if a ruleset contains multiple selectors, any of them can have the extend keyword. multiple selectors with extend in one ruleset:

.big-division,.big-bag:extend(.bag),.big-bucket:extend(.bucket) { // body}

extend inside ruleset

extend can be placed into rulesets body using &:extend(selector) syntax. placing extend into a body is a shortcut for placing it into every single selector of that ruleset.

extend inside a body:

pre:hover,.some-class { &:extend(div pre);}js随机数

is exactly the same as adding an extend after each selector:

pre:hover:extend(div pre),.some-class:extend(div pre) {}

extending nested selectors

extend is able to match nested selectors. following less:


.bucket { tr { // nested ruleset with target selector  color: blue; }}.some-class:extend(.bucket tr) {} // nested ruleset is recognized


.bucket tr,.some-class { color: blue;}

essentially the extend looks at the compiled css, not the original less.


.bucket { tr & { // nested ruleset with target selector  color: blue; }}.some-class:extend(tr .bucket) {} // nested ruleset is recognized


tr .bucket,.some-class { color: blue;}

exact matching with extend

extend by default looks for exact match between selectors. it does matter whether selector uses leading star or not. it does not matter that two nth-expressions have the same meaning, they need to have to same form in order to be matched. the only exception are quotes in attribute selector, less knows they have the same meaning and matches them.


.a.class,.class.a,.class > .a { color: blue;}.test:extend(.class) {} // this will not match the any selectors above

leading star does matter. selectors *.class and .class are equivalent, but extend will not match them:

*.class { color: blue;}.nostar:extend(.class) {} // this will not match the *.class selector


*.class { color: blue;}

order of pseudoclasses does matter. selectors link:hover:visited and link:visited:hover match the same set of elements, but extend treats them as different:

link:hover:visited { color: blue;}.selector:extend(link:visited:hover) {}


link:hover:visited { color: blue;}

nth expression

nth expression form does matter. nth-expressions 1n+3 and n+3 are equivalent, but extend will not match them:

:nth-child(1n+3) { color: blue;}.child:extend(:nth-child(n+3)) {}


:nth-child(1n+3) { color: blue;}

quote type in attribute selector does not matter. all of the following are equivalent.

[title=identifier] { color: blue;}[title='identifier'] { color: blue;}[title="identifier"] { color: blue;}.noquote:extend([title=identifier]) {}.singlequote:extend([title='identifier']) {}.doublequote:extend([title="identifier"]) {}


[title=identifier],.noquote,.singlequote,.doublequote { color: blue;}[title='identifier'],.noquote,.singlequote,.doublequote { color: blue;}[title="identifier"],.noquote,.singlequote,.doublequote { color: blue;}

extend "all"

when you specify the all keyword last in an extend argument it tells less to match that selector as part of another selector. the selector will be copied and the matched part of the selector only will then be replaced with the extend, making a new selector.


.a.b.test,.test.c { color: orange;}.test { &:hover {  color: green; }}.replacement:extend(.test all) {}


.a.b.test,.test.c,.a.b.replacement,.replacement.c { color: orange;}.test:hover,.replacement:hover { color: green;}

you can think of this mode of operation as essentially doing a non-destructive search and replace.

selector interpolation with extend

extend is not able to match selectors with variables. if selector contains variable, extend will ignore it.

there is a pending feature request for this but it is not an easy change. however, extend can be attached to interpolated selector.

selector with variable will not be matched:

@variable: .bucket;@{variable} { // interpolated selector color: blue;}.some-class:extend(.bucket) {} // does nothing, no match is found

and extend with variable in target selector matches nothing:

.bucket { color: blue;}.some-class:extend(@{variable}) {} // interpolated selector matches nothing@variable: .bucket;

both of the previous examples compile into:

.bucket { color: blue;}

however, :extend attached to an interpolated selector works:

.bucket { color: blue;}@{variable}:extend(.bucket) {}@variable: .selector;

previous less compiles into:

.bucket, .selector { color: blue;}

scoping / extend inside @media

extend written inside a media declaration should match only selectors inside the same media declaration:

@media print { .screenclass:extend(.selector) {} // extend inside media .selector { // this will be matched - it is in the same media  color: black; }}.selector { // ruleset on top of style sheet - extend ignores it color: red;}@media screen { .selector { // ruleset inside another media - extend ignores it  color: blue; }}

compiles into:

@media print { .selector, .screenclass { /* ruleset inside the same media was extended */  color: black; }}.selector { /* ruleset on top of style sheet was ignored */ color: red;}@media screen { .selector { /* ruleset inside another media was ignored */  color: blue; }}

extend written inside a media declaration does not match selectors inside nested declaration:

@media screen { .screenclass:extend(.selector) {} // extend inside media @media (min-width: 1023px) {  .selector { // ruleset inside nested media - extend ignores it color: blue;  } }}

compiles into:

@media screen and (min-width: 1023px) { .selector { /* ruleset inside another nested media was ignored */  color: blue; }}

top level extend matches everything including selectors inside nested media:

@media screen { .selector { /* ruleset inside nested media - top level extend works */  color: blue; } @media (min-width: 1023px) {  .selector { /* ruleset inside nested media - top level extend works */ color: blue;  } }}.toplevel:extend(.selector) {} /* top level extend matches everything */

compiles into:

@media screen { .selector, .toplevel { /* ruleset inside media was extended */  color: blue; }}@media screen and (min-width: 1023px) { .selector, .toplevel { /* ruleset inside nested media was extended */  color: blue; }}

duplication detection

currently there is no duplication detection.


.alert-info,.widget { /* declarations */}.alert:extend(.alert-info, .widget) {}


.alert-info,.widget,.alert,.alert { /* declarations */}

use cases for extend

classic use case

the classic use case is to avoid adding a base class. for example, if you have

.animal { background-color: black; color: white;}

and you want to have a subtype of animal which overrides the background color then you have two options, firstly change your html

<a class="animal bear">bear</a>
.animal { background-color: black; color: white;}.bear { background-color: brown;}

or have simplified html and use extend in your less. e.g.

<a class="bear">bear</a>
.animal { background-color: black; color: white;}.bear { &:extend(.animal); background-color: brown;}

reducing css size

mixins copy all of the properties into a selector, which can lead to unnecessary duplication. therefore you can use extends instead of mixins to move the selector up to the properties you wish to use, which leads to less css being generated.

example - with mixin:

.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;}

example (with extends):

.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;}

combining styles / a more advanced mixin

another use-case is as an alternative for a mixin - because mixins can only be used with simple selectors, if you have two different blocks on html, but need to apply the same styles to both you can use extends to relate two areas.


li.list > a { // list styles}button.list-style { &:extend(li.list > a); // use the same list styles}

edit the markdown source for "mixins"

"mix-in" properties from existing styles

you can mix-in class selectors and id selectors, e.g.

.a, #b { color: red;}.mixin-class { .a();}.mixin-id { #b();}

which results in:

.a, #b { color: red;}.mixin-class { color: red;}.mixin-id { color: red;}

notice that when you call the mixin, the parentheses are optional.

.a();  //these lines do the same thing.a;

not outputting the mixin

if you want to create a mixin but you do not want that mixin to be output, you can put parentheses after it.

.my-mixin { color: black;}.my-other-mixin() { background: white;}.class { .my-mixin; .my-other-mixin;}


.my-mixin { color: black;}.class { color: black; background: white;}

selectors in mixins

mixins can contain more than just properties, they can contain selectors too.

for example:

.my-hover-mixin() { &:hover {  border: 1px solid red; }}button { .my-hover-mixin();}


button:hover { border: 1px solid red;}


if you want to mixin properties inside a more complicated selector, you can stack up multiple id's or classes.

#outer { .inner {  color: red; }}.c { #outer > .inner;}

and again both > and whitespace are optional

// all do the same thing#outer > .inner;#outer > .inner();#outer .inner;#outer .inner();#outer.inner;#outer.inner();

one use of this is known as namespacing. you can put your mixins under a id selector and this makes sure it won't conflict with another library.


#my-library { .my-mixin() {  color: black; }}// which can be used like this.class { #my-library > .my-mixin();}

the !important keyword

use the !important keyword after mixin call to mark all properties inherited by it as !important:


.foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color;}.unimportant { .foo();}.important { .foo() !important;}

results in:

.unimportant { background: #f5f5f5; color: #900;}.important { background: #f5f5f5 !important; color: #900 !important;}

edit the markdown source for "mixins-parametric"

how to pass arguments to mixins

mixins can also take arguments, which are variables passed to the block of selectors when it is mixed in.

for example:

.border-radius(@radius) { -webkit-border-radius: @radius; -moz-border-radius: @radius;   border-radius: @radius;}

and here's how we can mix it into various rulesets:

#header { .border-radius(4px);}.button { .border-radius(6px);}

parametric mixins can also have default values for their parameters:

.border-radius(@radius: 5px) { -webkit-border-radius: @radius; -moz-border-radius: @radius;   border-radius: @radius;}

we can invoke it like this now:

#header { .border-radius;}

and it will include a 5px border-radius.

you can also use parametric mixins which don't take parameters. this is useful if you want to hide the ruleset from the css output, but want to include its properties in other rulesets:

.wrap() { text-wrap: wrap; white-space: -moz-pre-wrap; white-space: pre-wrap; word-wrap: break-word;}pre { .wrap }

which would output:

pre { text-wrap: wrap; white-space: -moz-pre-wrap
打零工的徐姓男子骑到一半撞车,竟把车丢在路边,加上现场留有一件外套,外套内有一张屏东地院的传票〖ticket〗,因此〖 yīn cǐ〗串起整起案情,警方5日持拘票〖ticket〗在复兴南路一处土地公庙内将人逮捕,但他始终否认犯小糶ǎn〗卸妓挡恢馈紌hī dao〗,不过得手的财物都已经〖have been〗拿去买毒,讯后依照公共危险、窃盗等罪嫌移送
由于〖yóu yú〗黄嫌等人讨债手段恶劣,又光天化日进入校园,行径大胆,案发后警察〖policeman〗局长胡木源立即指示刑警大队与瑞芳分局组成专案小糶ǎn〗∽椋⒈ㄇ牖〉丶焓鹬富诱彀
; white-space: pre-wrap; word-wrap: break-word;}

mixins with multiple parameters

parameters are either semicolon or comma separated. it is recommended to use semicolon. the symbol comma has double meaning: it can be interpreted either as a mixin parameters separator or css list separator.

using comma as mixin separator makes it impossible to create comma separated lists as an argument. on the other hand, if the compiler sees at least one semicolon inside mixin call or declaration, it assumes that arguments are separated by semicolons and all commas belong to css lists:

  • two arguments and each contains comma separated list: .name(1, 2, 3; something, else),
  • three arguments and each contains one number: .name(1, 2, 3),
  • use dummy semicolon to create mixin call with one argument containing comma separated css list: .name(1, 2, 3;),
  • comma separated default value: .name(@param1: red, blue;).

it is legal to define multiple mixins with the same name and number of parameters. less will use properties of all that can apply. if you used the mixin with one parameter e.g. .mixin(green);, then properties of all mixins with exactly one mandatory parameter will be used:

.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);}

compiles into:

.some .selector div { color-1: #008000; color-2: #008000; padding-2: 2;}

named parameters

a mixin reference can supply parameters values by their names instead of just positions. any parameter can be referenced by its name and they do not have to be in any special order:

.mixin(@color: black; @margin: 10px; @padding: 20px) { color: @color; margin: @margin; padding: @padding;}.class1 { .mixin(@margin: 20px; @color: #33acfe);}.class2 { .mixin(#efca44; @padding: 40px);}

compiles into:

.class1 { color: #33acfe; margin: 20px; padding: 20px;}.class2 { color: #efca44; margin: 10px; padding: 40px;}

the @arguments variable

@arguments has a special meaning inside mixins, it contains all the arguments passed, when the mixin was called. this is useful if you don't want to deal with individual parameters:

.box-shadow(@x: 0; @y: 0; @blur: 1px; @color: #000) { -webkit-box-shadow: @arguments; -moz-box-shadow: @arguments;   box-shadow: @arguments;}.big-block { .box-shadow(2px; 5px);}

which results in:

.big-block { -webkit-box-shadow: 2px 5px 1px #000; -moz-box-shadow: 2px 5px 1px #000;   box-shadow: 2px 5px 1px #000;}

advanced arguments and the @rest variable

you can use ... if you want your mixin to take a variable number of arguments. using this after a variable name will assign those arguments to the variable.

.mixin(...) {  // matches 0-n arguments.mixin() {  // matches exactly 0 arguments.mixin(@a: 1) { // matches 0-1 arguments.mixin(@a: 1; ...) { // matches 0-n arguments.mixin(@a; ...) {  // matches 1-n arguments


.mixin(@a; @rest...) {  // @rest is bound to arguments after @a  // @arguments is bound to all arguments}


sometimes, you may want to change the behavior of a mixin, based on the parameters you pass to it. let's start with something basic:

.mixin(@s; @color) { ... }.class { .mixin(@switch; #888);}

now let's say we want .mixin to behave differently, based on the value of @switch, we could define .mixin as such:

.mixin(dark; @color) { color: darken(@color, 10%);}.mixin(light; @color) { color: lighten(@color, 10%);}.mixin(@_; @color) { display: block;}

now, if we run:

@switch: light;.class { .mixin(@switch; #888);}

we will get the following css:

.class { color: #a2a2a2; display: block;}

where the color passed to .mixin was lightened. if the value of @switch was dark,the result would be a darker color.

here's what happened:

  • the first mixin definition didn't match because it expected dark as the first argument.
  • the second mixin definition matched, because it expected light.
  • the third mixin definition matched because it expected any value.

only mixin definitions which matched were used. variables match and bind to any value.anything other than a variable matches only with a value equal to itself.

we can also match on arity, here's an example:

.mixin(@a) { color: @a;}.mixin(@a; @b) { color: fade(@a; @b);}

now if we call .mixin with a single argument, we will get the output of the first definition,but if we call it with two arguments, we will get the second definition, namely @a faded to @b.

edit the markdown source for "mixins-as-functions"

return variables or mixins from mixins

variables and mixins defined in a mixin are visible and can be used in caller's scope. there is only one exception, variable is not copied if the caller contains a variable with the same name (that includes variables defined by another mixin call). only variables present in callers local scope are protected. variables inherited from parent scopes are overridden.


.mixin() { @width:  100%; @height: 200px;}.caller { .mixin(); width: @width; height: @height;}

results in:

.caller { width: 100%; height: 200px;}

thus variables defined in a mixin can act as its return values. this allows us to create a mixin that can be used almost like a function.


.average(@x, @y) { @average: ((@x + @y) / 2);}div { .average(16px, 50px); // "call" the mixin padding: @average;  // use its "return" value}

results in:

div { padding: 33px;}

<<<<<<< head

variable defined directly in callers scope can not be overriden. however, variable defined in callers parent scope is not protected and will be overriden:

.mixin() { @size: in-mixin;  @definedonlyinmixin: in-mixin;}.class { margin: @size @definedonlyinmixin; .mixin(); }@size: globaly-defined-value; // callers parent scope - no protection

results in:

.class { margin: in-mixin in-mixin;}

finally, mixin defined in mixin acts as return value too:

.unlock(@value) { // outer mixin .dosomething() { // nested mixin  declaration: @value; }}#namespace { .unlock(5); // unlock dosomething mixin .dosomething(); //nested mixin was copied here and is usable }

results in:

#namespace { declaration: 5;}


edit the markdown source for "detached-rulesets"

allow wrapping of a css block, defined in a mixin

released v1.7.0

detached ruleset is a group of css properties, nested rulestes, media declarations or anything else stored in a variable. you can include it into a ruleset or another structure and all its properties are going to be copied there. you can also use it as a mixin argument and pass it around as any other variable.

simple example:

// declare detached ruleset@detached-ruleset: { background: red; };// use detached ruleset.top {  @detached-ruleset(); }

compiles into:

.top { background: red;}

parentheses after detached ruleset call are mandatory. the @detached-ruleset; call would not work.

it is usefull when you want to define a mixin that abstracts out either wrapping a piece of code in a media query or a non supported browser class name. the rulesets can be passed to mixin so that the mixin can wrap the content. e.g.

.desktop-and-old-ie(@rules) { @media screen and (min-width: 1200) { @rules(); } html.lt-ie9 &    { @rules(); }}header { background-color: blue; .desktop-and-old-ie({  background-color: red; });}

here the desktop-and-old-ie mixin defines the media query and root class so that you can use a mixin to wrap a piece of code. this will output..

header { background-color: blue;}@media screen and (min-width: 1200) { header {  background-color: red; }}html.lt-ie9 header { background-color: red;}

a ruleset can be now assigned to a variable or passed in to a mixin and can contain the full set of less features, e.g.

@my-ruleset: {  .my-selector { background-color: black;  } };

you can even take advantage of media query bubbling, for instance

@my-ruleset: {  .my-selector { @media tv {  background-color: black; }  } };@media (orientation:portrait) {  @my-ruleset();}

which will output

@media (orientation: portrait) and tv { .my-selector {  background-color: black; }}

detached ruleset call unlocks (returns) all its mixins into caller the same way as mixin calls do. however, it does not return variables.

returned mixin:

// detached ruleset with a mixin@detached-ruleset: { .mixin() {  color:blue;  }};// call detached ruleset.caller {  @detached-ruleset(); .mixin();}

results in:

.caller { color: blue;}

private variables:

detached-ruleset: { @color:blue; //this variable is private};.caller {  color: @color; //syntax error}


detached ruleset can use all variables and mixins accessible on place where it is defined and where it is called. otherwise said, both definition and caller scopes are available to it. if both scopes contains the same variable or mixin, declaration scope value takes precedence.

declaration scope is the one where detached ruleset body is defined. copying detached ruleset from one variable into another can not modify its scope. ruleset does not gain access to new scopes just by being referenced there.

lastly, detached ruleset can gain access to scope by being unlocked (imported) into it.

definition and caller scope visibility

detached mixin sees callers variables and mixins:

@detached-ruleset: { caller-variable: @callervariable; // variable is undefined here .callermixin(); // mixin is undefined here};selector { // use detached ruleset @detached-ruleset();  // define variable and mixin needed inside the detached ruleset @callervariable: value; .callermixin() {  variable: declaration; }}

compiles into:

selector { caller-variable: value; variable: declaration;}

variable and mixins accessible form definition win over those available in caller:

@variable: global;@detached-ruleset: { //will use global variable, because it is accessible //from detached-ruleset definition variable: @variable; };selector { @detached-ruleset(); @variable: value; //variable defined in caller - will be ignored}

compiles into:

selector { variable: global;}

referencing wont modify detached ruleset scope

ruleset does not gain access to new scopes just by being referenced there:

@detached-1: { scope-detached: @one @two; };.one { @one: visible; .two {  @detached-2: @detached-1; // copying/renaming ruleset   @two: visible; // ruleset can not see this variable }}.useplace { .one > .two();  @detached-2();}

throws an error:

error 1:32 the variable "@one" was not declared.

unlocking will modify detached ruleset scope

detached ruleset gains access by being unlocked (imported) inside a scope:

#space { .importer1() {  @detached: { scope-detached: @variable; }; // define detached ruleset }}.importer2() { @variable: value; // unlocked detached ruleset can see this variable #space > .importer1(); // unlock/import detached ruleset}.useplace { .importer2(); // unlock/import detached ruleset second time  @detached();}

compiles into:

.useplace { scope-detached: value;}

edit the markdown source for "import-directives"

import styles from other style sheets

in standard css, @import at-rules must precede all other types of rules. but less.js doesn't care where you put @import statements.


.foo { background: #900;}@import "this-is-valid.less";

file extensions

@import statements may be treated differently by less depending on the file extension:

  • if the file has a .css extension it will be treated as css and the @import statement left as-is (see the inline option below).
  • if it has any other extension it will be treated as less and imported.
  • if it does not have an extension, .less will be appended and it will be included as a imported less file.


@import "foo"; // foo.less is imported@import "foo.less"; // foo.less is imported@import "foo.php"; // foo.php imported as a less file@import "foo.css"; // statement left in place, as-is

the following options can be used to override this behavior.

import options

less offers several extensions to the css @import css at-rule to provide more flexibility over what you can do with external files.

syntax: @import (keyword) "filename";

the following import directives have been implemented:

  • reference: use a less file but do not output it
  • inline: include the source file in the output but do not process it
  • less: treat the file as a less file, no matter what the file extension
  • css: treat the file as a css file, no matter what the file extension
  • once: only include the file once (this is default behavior)
  • multiple: include the file multiple times


use @import (reference) to import external files, but without adding the imported styles to the compiled output unless referenced.

released v1.5.0

example: @import (reference) "foo.less";

reference is one of the most powerful features in the less language. imagine that reference marks every directive and selector with a reference flag in the imported file, imports as normal, but when the css is generated, "reference" selectors (as well as any media queries containing only reference selectors) are not output. reference styles will not show up in your generated css unless the reference styles are used as mixins or extended.

additionally, reference produces different results depending on which method was used (mixin or extend):

  • extend: when a selector is extended, only the new selector is marked as not referenced, and it is pulled in at the position of the reference @import statement.
  • mixins: when a reference style is used as an implicit mixin, its rules are mixed-in, marked "not reference", and appear in the referenced place as normal.

reference example

this allows you to pull in only specific, targeted styles from a library such as bootstrap by doing something like this:

.navbar:extend(.navbar all) {}

and you will pull in only .navbar related styles from bootstrap.


use @import (inline) to include external files, but not process them.

released v1.5.0

example: @import (inline) "not-less-compatible.css";

you will use this when a css file may not be less compatible; this is because although less supports most known standards css, it does not support comments in some places and does not support all known css hacks without modifying the css.

so you can use this to include the file in the output so that all css will be in one file.


use @import (less) to treat imported files as less, regardless of file extension.

released v1.4.0


@import (less) "foo.css";


use @import (css) to treat imported files as regular css, regardless of file extension. this means the import statement will be left as it is.

released v1.4.0


@import (css) "foo.less";js随机数


@import "foo.less";


the default behavior of @import statements. it means the file is imported only once and subsequent import statements for that file will be ignored.

released v1.4.0

this is the default behavior of @import statements.


@import (once) "foo.less";@import (once) "foo.less"; // this statement will be ignored


use @import (multiple) to allow importing of multiple files with the same name. this is the opposite behavior to once.

released v1.4.0


// file: foo.less.a { color: green;}// file: main.less@import (multiple) "foo.less";@import (multiple) "foo.less";


.a { color: green;}.a { color: green;}

edit the markdown source for "mixin-guards"

conditional mixins

guards are useful when you want to match on expressions, as opposed to simple values or arity. if you are familiar with functional programming, you have probably encountered them already.

in trying to stay as close as possible to the declarative nature of css, less has opted to implement conditional execution via guarded mixins instead of if/else statements, in the vein of @media query feature specifications.

let's start with an example:

.mixin (@a) when (lightness(@a) >= 50%) { background-color: black;}.mixin (@a) when (lightness(@a) < 50%) { background-color: white;}.mixin (@a) { color: @a;}

the key is the when keyword, which introduces a guard sequence (here with only one guard). now if we run the following code:

.class1 { .mixin(#ddd) }.class2 { .mixin(#555) }

here's what we'll get:

.class1 { background-color: black; color: #ddd;}.class2 { background-color: white; color: #555;}

guard comparison operators

the full list of comparison operators usable in guards are: >, >=, =, =<, <. additionally, the keyword true is the only truthy value, making these two mixins equivalent:

.truth (@a) when (@a) { ... }.truth (@a) when (@a= true) { ... }

any value other than the keyword true is falsy:

.class { .truth(40); // will not match any of the above definitions.}

note that you can also compare arguments with each other, or with non-arguments:

@media: mobile;.mixin (@a) when (@media= mobile) { ... }.mixin (@a) when (@media= desktop网站地图  手机端) { ... }.max (@a; @b) when (@a > @b) { width: @a }.max (@a; @b) when (@a < @b) { width: @b }

guard logical operators

you can use logical operators with guards. the syntax is based on css media queries.

use the and keyword to combine guards:

.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }

you can emulate the or operator by separating guards with a comma ,. if any of the guards evaluate to true, it's considered a match:

.mixin (@a) when (@a > 10), (@a < -10) { ... }

use the not keyword to negate conditions:

.mixin (@b) when not (@b > 0) { ... }

type checking functions

lastly, if you want to match mixins based on value type, you can use the is functions:

.mixin (@a; @b: 0) when (isnumber(@b)) { ... }.mixin (@a; @b: black) when (iscolor(@b)) { ... }

here are the basic type checking functions:

  • iscolor
  • isnumber
  • isstring
  • iskeyword
  • isurl

if you want to check if a value is in a specific unit in addition to being a number, you may use one of:

  • ispixel
  • ispercentage
  • isem
  • isunit

conditional mixins

(fixme) additionally, the default function may be used to make a mixin match depending on other mixing matches, and you may use it to create "conditional mixins" similar to else or default statements (of if and case structures respectively):

.mixin (@a) when (@a > 0) { ... }.mixin (@a) when (default()) { ... } // matches only if first mixin does not, i.e. when @a <=0

edit the markdown source for "css-guards"

"if"'s around selectors

released v1.5.0

guards can also be applied to css selectors, which is syntactic sugar for declaring the mixin and then calling it immediately.

for instance, before 1.5.0 you would have had to do this:

.my-optional-style() when (@my-option= true) { button {  color: white; }}.my-optional-style();

now, you can apply the guard directly to a style.

button when (@my-option= true) { color: white;}

you can also achieve an if type statement by combining this with the & feature, allowing you to group multiple guards.

& when (@my-option= true) { button {  color: white; } a {  color: blue; }}

edit the markdown source for "loops"

creating loops

in less a mixin can call itself. such recursive mixins, when combined with guard expressions and pattern matching, can be used to create various iterative/loop structures.


.loop(@counter) when (@counter > 0) { .loop((@counter - 1));  // next iteration width: (10px * @counter); // code for each iteration}div { .loop(5); // launch the loop}


div { width: 10px; width: 20px; width: 30px; width: 40px; width: 50px;}

a generic example of using a recursive loop to generate css grid classes:

.generate-columns(4);.generate-columns(@n, @i: 1) when (@i=< @n) { .column-@{i} {  width: (@i * 100% / @n); } .generate-columns(@n, (@i + 1));}


.column-1 { width: 25%;}.column-2 { width: 50%;}.column-3 { width: 75%;}.column-4 { width: 100%;}

edit the markdown source for "merge"

combine properties

the merge feature allows for aggregating values from multiple properties into a comma or space separated list under a single property. merge is useful for properties such as background and transform.


append property value with comma

released v1.5.0


.mixin() { box-shadow+: inset 0 0 10px #555;}.myclass { .mixin(); box-shadow+: 0 0 20px black;}


.myclass { box-shadow: inset 0 0 10px #555, 0 0 20px black;}


append property value with space

released v1.7.0


.mixin() { transform+_: scale(2);}.myclass { .mixin(); transform+_: rotate(15deg);}


.myclass { transform: scale(2) rotate(15deg);}

to avoid any unintentional joins, merge requires an explicit + or +_ flag on each join pending declaration.

edit the markdown source for "parent-selectors"

referencing parent selectors with &

the & operator represents the parent selectors of a nested rule and is most commonly used when applying a modifying class or pseudo-class to an existing selector:

a { color: blue; &:hover {  color: green; }}

results in:

a { color: blue;}a:hover { color: green;}

notice that without the &, the above example would result in a :hover rule (a descendant selector that matches hovered elements inside of <a> tags) and this is not what we typically would want with the nested :hover.

the "parent selectors" operator has a variety of uses. basically any time you need the selectors of the nested rules to be combined in other ways than the default. for example another typical use of the & is to produce repetitive class names:

.button { &-ok {  background-image: url("ok.png"); } &-cancel {  background-image: url("cancel.png"); } &-custom {  background-image: url("custom.png"); }}


.button-ok { background-image: url("ok.png");}.button-cancel { background-image: url("cancel.png");}.button-custom { background-image: url("custom.png");}

multiple &

& may appear more than once within a selector. this makes it possible to repeatedly refer to a parent selector without repeating its name.

.link { & + & {  color: red; } & & {  color: green; } && {  color: blue; } &, &ish {  color: cyan; }}

will output:

.link + .link { color: red;}.link .link { color: green;}.link.link { color: blue;}.link, .linkish { color: cyan;}

note that & represents all parent selectors (not just the nearest ancestor) so the following example:

.grand { .parent {  & > & { color: red;  }  & & { color: green;  }  && { color: blue;  }  &, &ish { color: cyan;  } }}

results in:

.grand .parent > .grand .parent { color: red;}.grand .parent .grand .parent { color: green;}.grand .parent.grand .parent { color: blue;}.grand .parent,.grand .parentish { color: cyan;}

changing selector order

it can be useful to prepend a selector to the inherited (parent) selectors. this can be done by putting the & after current selector.for example, when using modernizr, you might want to specify different rules based on supported features:

.header { .menu {  border-radius: 5px;  .no-borderradius & { background-image: url('images/button-background.png');  } }}

the selector .no-borderradius & will prepend .no-borderradius to its parent .header .menu to form the.no-borderradius .header .menu on output:

.header .menu { border-radius: 5px;}.no-borderradius .header .menu { background-image: url('images/button-background.png');}

combinatorial explosion

& can also be used to generate every possible permutation of selectors in a comma separated list:

p, a, ul, li {border-top: 2px dotted #366; & + & { border-top: 0; }}

this expands to all possible (16) combinations of the specified elements:

p,a,ul,li { border-top: 2px dotted #366;}p + p,p + a,p + ul,p + li,a + p,a + a,a + ul,a + li,ul + p,ul + a,ul + ul,ul + li,li + p,li + a,li + ul,li + li { border-top: 0;}