Variables, mixins, and namespaces
Less variables, mixins, and namespaces provide the main ways to share values and style rules across a stylesheet. Their evaluation model combines nearby lexical scopes with lazy value resolution, so a reference can use the value that the surrounding scope defines at evaluation time. This page expands the Less language features overview and connects with selectors, extends, and at-rules, functions and value processing, and evaluation and CSS generation.
Evaluation model
Lexical scopes
Less evaluates a reference against a stack of surrounding rulesets. It checks the nearest scope first, then moves outward until it finds a matching variable, mixin, or property. A mixin and a detached ruleset retain the scope in which they are defined, which lets shared rules use values from their defining context when they run.
The scope model has two useful effects:
- A nested scope can hide a value from an outer scope.
- A reusable rule can keep access to values from its definition site while also seeing the scope of its caller.
References: packages/…/variable.js• L28-68 packages/…/mixin-definition.js• L209-227 packages/…/detached-ruleset.js• L27-42
Lazy values
Less stores variable declarations as values that it resolves when a reference needs them. The scope therefore collects declarations before it evaluates the referenced value. A later declaration in the same scope can affect an earlier reference, while an inner scope still takes priority over an outer scope.
Lazy evaluation also permits variables to refer to other variables that are declared later. Recursive references do not resolve indefinitely. Less reports a recursive variable definition, or a missing name, when it cannot produce a value.
References: packages/…/ruleset.js• L349-405 packages/…/variable.js• L28-68
Variables
Variable references
Variables use the @name form for values and can hold ordinary CSS values or a ruleset. A second @ performs indirection: Less first resolves the inner variable to obtain the name of the variable to read. This supports maps of variable names without changing the surrounding scope rules.
Use @{name} when a variable must be inserted into text such as a selector, property name, or URL. The same form can carry a lookup chain, such as @{map[key]}, when the variable contains a ruleset or map-like value.
References: packages/…/variable.js• L28-33 packages/…/parser.js• L790-853 packages/…/interpolated-variable.js• L19-32
Variable declarations
An @name: value declaration is a configuration value for the scope in which it appears. It does not become CSS output by itself. A variable can instead hold a detached ruleset when its value is a block, which makes the block available for a later call or lookup.
Variables also pass through mixin parameter frames. This is why a mixin body can use both its named parameters and the values visible around the mixin definition and call.
References: packages/…/declaration.js• L27-56 packages/…/parser.js• L1775-1825 packages/…/mixin-definition.js• L96-193
Mixins
Mixin matching
A mixin is a reusable ruleset selected by a class-like or id-like name. A call searches the current and enclosing scopes for definitions with the same selector. Nested selectors create namespaces for mixin lookup, so a call can select a definition through a child or descendant path.
Less can define several mixins with the same name. The call keeps the definitions whose argument patterns and guards match, then emits the rules from the selected definitions. A call with no matching definition fails instead of producing partial CSS.
References: packages/…/mixin-call.js• L163-249 packages/…/ruleset.js• L469-540 packages/…/parser.js• L1097-1176
Guard conditions
A guarded mixin adds a when condition to its parameter pattern. Less evaluates the condition with the call arguments bound to the mixin parameters, so guards can select a rule set by value or by argument shape. Conditions use Less comparisons and boolean operators before the mixin body runs.
The default() function resolves competing guarded definitions as a group. Less checks the guards in both default states before it chooses a result. If the result remains ambiguous, the call fails with an ambiguity error.
References: packages/…/mixin-definition.js• L246-261 packages/…/mixin-call.js• L110-143 packages/…/condition.js• L30-62 packages/…/default.js• L4-23
Mixin arguments
Mixin parameters can be positional, named, optional, or variadic. A named call argument binds directly to the parameter with the same name. Positional arguments fill the remaining parameters, and a default value supplies an omitted optional parameter. The call exposes the complete evaluated argument list through the special @arguments value.
Argument values are evaluated for the call and then bound in a fresh mixin scope. Less rejects unknown named arguments, missing required arguments, and calls with extra positional arguments. A variadic parameter gathers the remaining values into one list.
References: packages/…/mixin-definition.js• L96-193 packages/…/mixin-definition.js• L263-315 packages/…/parser.js• L1210-1336
Detached rulesets
Captured rulesets
A detached ruleset is a block stored as a value rather than emitted at its declaration site. It is useful when a stylesheet needs to pass a group of declarations or nested rules into another rule or mixin. Evaluation captures the defining scope, while calling the ruleset adds the current scope for the call.
The call form is @ruleset();. Less also permits a detached ruleset as a mixin argument. A ruleset must be called before its contents can act as CSS rules; using it as an ordinary property value is an error.
References: packages/…/detached-ruleset.js• L7-42 packages/…/variable-call.js• L25-57 packages/…/declaration.js• L94-101 packages/…/parser.js• L1716-1745
Namespaces and property accessors
Namespace lookup
Namespace lookup reads a value from the rules returned by a mixin or detached ruleset. A bracket chain selects one item at a time, so a lookup can move from a ruleset to a variable, property, or nested ruleset. An empty lookup selects the last declaration in the current ruleset.
Lookup names beginning with @ select variables. Other names select properties and use the last matching property value after Less applies property merge behavior. Lookup names can themselves use variable indirection, which lets a caller choose the item to read at evaluation time.
The following flow shows the relationship between a call, its returned rules, and a lookup chain.
flowchart LR
A[Mixin or detached ruleset call] --> B[Ruleset value]
B --> C{Lookup key}
C -->|@variable| D[Variable value]
C -->|property| E[Property value]
C -->|nested rule| F[Nested ruleset]
F --> C
References: packages/…/namespace-value.js• L25-91 packages/…/parser.js• L980-1020 packages/…/parser.js• L1107-1169
Property accessors
A property accessor reads a declaration as a value by using $name. It lets a rule reuse a property that is already present in the same or an enclosing scope without copying the value into a variable. ${name} provides the interpolation form when the property reference is part of text.
Property access follows the same nearest-scope rule as variables. A property can have more than one declaration because Less supports property merging. A missing property, or a recursive property reference, produces a named error instead of CSS output.
Property accessors do not form lookup chains. Bracket lookups belong to variable or ruleset lookup, not to the $name property reference itself.
References: packages/…/property.js• L24-79 packages/…/ruleset.js• L379-413 packages/…/interpolated-variable.js• L24-32 packages/…/parser.js• L855-865
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to variable or property scope lookup must preserve the nearest-scope rule for mixin bodies, detached rulesets, and namespace values.
- Changes to mixin argument parsing must stay aligned with parameter matching, default guards, variadic capture, and the
@argumentsvalue. - Changes to lookup grammar must keep bare and interpolated variable references consistent, including chained lookups and variable indirection.
- Changes to detached-ruleset evaluation must preserve both the defining scope and the caller scope at call time.
- Changes to property collection must preserve the distinction between one value and multiple mergeable declarations.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A variable or property refers to itself through the active scope | Less reports a recursive reference and stops evaluation | Keep the dependency graph for reusable values acyclic |
| A mixin call has no definition that matches its name, arguments, and guards | Less reports an undefined or no-matching mixin error | Check the namespace path and the required parameter patterns |
More than one guarded definition remains selected by default() | Less reports an ambiguous default() use | Make the guard groups mutually exclusive |
| A named mixin argument does not match a parameter | Less reports a named-argument error | Keep call names equal to declared parameter names |
| A variable or property lookup names an item that the returned ruleset does not contain | Less reports a missing variable or property | Keep lookup keys aligned with the ruleset contract |
| A detached ruleset is used as a normal CSS property value | Less rejects the property because rulesets cannot be emitted as values | Call the detached ruleset or use a supported ruleset lookup |
Key files
| File | Why to read it |
|---|---|
| packages/…/variable.js | Defines scoped variable lookup, indirection, lazy value evaluation, and recursion errors. |
| packages/…/ruleset.js | Shows how scopes collect variables and properties and how nested rulesets are searched. |
| packages/…/mixin-definition.js | Defines parameter binding, guards, defaults, closures, and mixin calls. |
| packages/…/mixin-call.js | Shows how Less finds candidate mixins and resolves guarded matches. |
| packages/…/detached-ruleset.js | Defines captured rulesets and their call-time evaluation. |
| packages/…/namespace-value.js | Defines chained lookup into variables, properties, and nested rulesets. |
| packages/…/property.js | Defines property accessor lookup and property merge evaluation. |
| packages/…/parser.js | Shows the user-facing grammar for variables, mixins, detached rulesets, and lookup chains. |