Introducing a new way to review your PRs.

Read the announcement

less / less.js

Less. The dynamic stylesheet language.

View on GitHub

17 documents · Updated Sep 7, 2026

docs / less-language-features

Less language features

Less is a CSS language that adds reusable values, scoped rules, selector composition, and value operations to CSS authoring. It turns nested source rules into CSS rules while resolving Less expressions in the current scope. This page gives the broad user-facing model; see Variables, mixins, and namespaces for value and mixin evaluation, Selectors, extends, and at-rules for selector and at-rule composition, Parser and syntax tree for source structure, Evaluation and CSS generation for the evaluation-to-output flow, and Functions and value processing for value functions.

Source structure

Less source uses a small set of related constructs. A declaration produces a property and value. A ruleset groups declarations under selectors. A block can contain declarations or more rulesets, so the same structure supports ordinary CSS output and nested Less composition.

Declarations

A declaration binds a property name to a value, or binds an @variable to a value when its name starts with @. Property names and values can contain interpolation. A declaration can also carry !important and a merge marker for values that must join with other declarations. During evaluation, Less rejects a ruleset or mixin result used directly as a CSS property value; a lookup must select a declaration or value first. This keeps ruleset values available for composition without emitting them as property text.

References: packages/…/declaration.js• L27-57 packages/…/declaration.js• L63-75 packages/…/declaration.js• L94-116 packages/…/parser.js• L1775-1855

Rulesets

A ruleset connects one or more selectors to a block of source rules. During evaluation, its block becomes the local scope for variables, mixins, properties, imports, and nested rules. Declarations become CSS output under the resolved selectors.

References: packages/…/ruleset.js• L39-100 packages/…/ruleset.js• L103-193 packages/…/ruleset.js• L250-287

Values

A value is a comma-separated list of expressions. An expression preserves the order and spacing of its parts until Less evaluates the parts that have Less meaning. This lets CSS values remain CSS-like while still allowing variables, functions, interpolation, and operations inside them.

References: packages/…/parser.js• L2525-2550 packages/…/value.js• L5-57 packages/…/expression.js• L9-91

Scope and composition

Less resolves source in nested scopes. The current ruleset and its parent scopes provide the context for variable and mixin lookup. A nested construct can use values declared in an outer scope while also defining more local values.

Nesting

Nesting places child rules inside a parent ruleset so authors can keep related styles together. A child selector normally combines with the parent selector. The & parent selector gives explicit control over that combination, which supports pseudo-classes, modifier selectors, and other selector forms.

The following diagram shows how a nested source tree becomes separate CSS rules.

flowchart TD
    A[Parent ruleset] --> B[Child selector]
    A --> C[Parent declarations]
    B --> D[Combine parent and child selectors]
    C --> E[Emit parent CSS rule]
    D --> F[Emit child CSS rule]

References: packages/…/nesting.less• L1-19 packages/…/ruleset.js• L250-287 packages/…/ruleset.js• L631-666

Variables

Variables name values for reuse across declarations, selectors, property names, and at-rule parts. Less resolves a reference from the nearest matching scope and can resolve a variable more than once as later declarations in the same scope become visible. An @@name reference can use one variable to select another variable name. See Variables, mixins, and namespaces for the detailed evaluation model.

References: packages/…/variable.js• L28-68 packages/…/variables.less• L1-20 packages/…/variables.less• L27-37

Interpolation

Interpolation inserts a resolved value into text that Less treats as a name or other CSS fragment. @{name} is the variable form. ${property} is the property-reference form and does not carry variable lookup chains. Interpolation can build selectors, property names, at-rule names or preludes, quoted values, URLs, and custom property values.

References: packages/…/interpolated-variable.js• L11-32 packages/…/interpolated-variable.js• L47-105 packages/…/lookup-interpolation.less• L34-108

Mixins

A mixin is a named ruleset intended for reuse. A definition can accept positional, named, default, or variadic parameters. A call finds matching definitions in the current scope and parent scopes. Guards select definitions by a condition, and a successful call contributes its declarations and nested rules to the caller. See Variables, mixins, and namespaces for matching, closure, and lookup behavior.

References: packages/…/parser.js• L1095-1177 packages/…/parser.js• L1339-1408 packages/…/mixin-call.js• L146-229 packages/…/mixin-definition.js• L223-315

Namespaces

A namespace is a ruleset path used to group mixins and values. A namespaced mixin call selects a definition below that path. A lookup chain can also select a variable, property, or final declaration from a ruleset value. This makes a ruleset usable as a structured value without making its internal declarations global. See Variables, mixins, and namespaces for lookup and detached-ruleset evaluation.

References: packages/…/namespace-value.js• L8-91 packages/…/parser.js• L988-1020 packages/…/mixins.less• L6-27

Selectors and at-rules

Selectors and at-rules are part of the same composition model as declarations. Less resolves their dynamic parts in scope, combines nested selectors with their parents, and keeps CSS constructs that can contain rules in the output structure.

Selectors

A selector is a sequence of elements joined by combinators. Less supports standard CSS selector forms plus variable interpolation, nested parent references, selector guards, and :extend() declarations. A comma-separated selector list creates one ruleset with several output selector paths. See Selectors, extends, and at-rules for selector joining and extension rules.

References: packages/…/selector.js• L10-39 packages/…/selector.js• L156-199 packages/…/parser.js• L1602-1666

Extends

An :extend() clause adds the current selector to matching target selectors instead of copying declarations into the current block. The default form matches the target in its direct selector context. The all option also permits matches across selector context boundaries. See Selectors, extends, and at-rules for the composition algorithm.

References: packages/…/parser.js• L1026-1093 packages/…/extend.js• L6-55 packages/…/extend.js• L64-84

At-rules

At-rules represent CSS directives with either a statement form or a block form. Less gives special structure to imports, media queries, container queries, @charset, @namespace, keyframes, layers, and common conditional blocks. Other at-rules remain available through a permissive CSS-compatible form. At-rule names and preludes support @{...} interpolation. See Selectors, extends, and at-rules for nesting and composition.

References: packages/…/parser.js• L2015-2050 packages/…/parser.js• L2236-2275 packages/…/parser.js• L2395-2523

At-rule nesting

Nested media and container rules combine with their parent conditions. A nested rule keeps its own block but receives the selector context from the surrounding ruleset. Media query lists form the relevant combinations when several nested conditions apply.

The following diagram shows the relation between a nested conditional rule and the CSS it emits.

flowchart LR
    A[Outer selector] --> B[Nested at-rule]
    C[Outer condition] --> B
    B --> D[Combine conditions]
    B --> E[Carry selector context]
    D --> F[CSS at-rule]
    E --> F

References: packages/…/media.js• L12-84 packages/…/container.js• L19-98 packages/…/nested-at-rule.js• L122-203 packages/…/media.less• L39-88

Imports

An import brings another source file into the Less evaluation scope or preserves it as a CSS @import, depending on its form and options. Imported Less rules participate in the same scope and composition model as local rules. CSS imports remain in the generated CSS for the browser to handle.

References: packages/…/parser.js• L2015-2049 packages/…/import.js• L33-109 packages/…/import.js• L131-175 packages/…/ruleset.js• L290-312

Operations and value output

Less evaluates operations only where the active math mode permits them. The result keeps the value type and unit rules needed for valid CSS output. Values that Less does not evaluate remain available as CSS expressions.

Arithmetic

Arithmetic supports addition, subtraction, multiplication, and division for numeric dimensions. Parentheses make the intended operation explicit. Less converts compatible units for addition and subtraction, carries units through multiplication and division, and reports invalid combinations when strict unit checks apply.

References: packages/…/parser.js• L2556-2634 packages/…/operation.js• L33-60 packages/…/dimension.js• L86-127

Colors

Color values take part in the same operation model as dimensions. Less represents colors as RGB values so arithmetic and color functions can produce a new color value before CSS output.

References: packages/…/color.js• L8-58 packages/…/operation.js• L36-58 packages/…/operations.less• L45-52

Property accessors

Property accessors read a property value from a ruleset or mixin result. The $property form selects a property by name, while a lookup chain can select a property or declaration inside a ruleset value. This supports mixins that expose both declarations and reusable values.

References: packages/…/property.js• L7-80 packages/…/namespace-value.js• L41-90 packages/…/property-accessors.less• L1-73

Value merging

The + and +_ merge markers combine declarations with the same property name. The comma form joins values as a comma list. The spaced form joins the parts with spaces inside each list item.

References: packages/…/merge-rules.js• L6-45 packages/…/parser.js• L1800-1806 packages/…/merge.less• L24-82

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the distinction between a variable declaration, a property declaration, and a detached ruleset. They use related syntax but have different scope and lookup effects.
  • When changing interpolation grammar, check all structural positions: selector elements, property names, at-rule names or preludes, quoted text, URLs, and custom property values.
  • When changing nested at-rule handling, check both selector bubbling and condition combination. A rule can be nested inside another at-rule or inside a selector context.
  • When changing mixin matching, preserve the contract between argument binding, guards, default selection, and the closure scope captured by the definition.
  • When changing merge markers, keep comma merging and space merging distinct. Both depend on the order of declarations and can carry !important.
  • When changing arithmetic or unit rules, check the boundary between Less-evaluated math and CSS-native expressions such as calc().
  • When changing declaration evaluation, preserve the boundary between values that can become CSS property text and ruleset results that require a lookup.

Important failure modes

TriggerConsequenceGuard
Reference to an undefined or recursive variableEvaluation stops with a name errorDefine the value in the current or an outer scope and avoid a cycle.
Mixin call matches no definition or has the wrong argumentsEvaluation stops with a mixin errorMatch the call shape and satisfy any guard.
More than one default() mixin candidate remains validEvaluation stops with an ambiguous default errorMake the guarded definitions select one clear candidate.
Arithmetic uses incompatible values or invalid unitsEvaluation stops with an operation or unit errorUse compatible dimensions or leave the expression for CSS.
A mixin call or detached ruleset is used directly as a property valueEvaluation stops because a ruleset cannot become CSS property textUse a detached-ruleset call or a ruleset lookup to select a declaration or value.
:extend() has no target selectorParsing stops with a syntax errorPut :extend() on a selector and provide a target.

Key files

FileWhy to read it
packages/…/parser.jsDefines the user-facing grammar for declarations, rulesets, variables, mixins, selectors, at-rules, interpolation, and operations.
packages/…/ruleset.jsShows how scopes, nested rules, imports, mixin calls, and declarations relate during evaluation.
packages/…/declaration.jsDefines the common behavior for CSS declarations and Less variable declarations. It also prevents a ruleset result from being emitted as a property value.
packages/…/variable.jsShows scoped variable lookup and recursive-reference detection.
packages/…/mixin-definition.jsShows parameter binding, guards, captured scope, and mixin expansion.
packages/…/mixin-call.jsShows how a call finds candidates and applies matching definitions.
packages/…/selector.jsDefines selector evaluation and CSS selector output.
packages/…/extend.jsDefines the selector extension relation.
packages/…/atrule.jsDefines the shared shape of statement and block at-rules.
packages/…/nested-at-rule.jsDefines how nested conditional at-rules combine and carry selectors.
packages/…/operation.jsDefines when arithmetic evaluates and how invalid operands fail.
packages/…/interpolated-variable.jsDefines variable and property interpolation forms and lookup chains.