Built-in functions and values
Less evaluates built-in functions as part of turning expressions into CSS-ready values. The function registry connects names to operations for colors, lists, numbers, strings, URLs, SVG data, and type checks. These operations can return a new Less value or leave a CSS function for the browser when Less cannot resolve it at compile time. See Compiler pipeline for the full flow and Evaluation and CSS generation for the surrounding evaluation stage.
Function evaluation
Function evaluation is the boundary between a parsed call and a value in the evaluated tree. It uses the current scope and evaluation context, so a function can consume resolved variables, respect math and URL settings, and return a value that later CSS generation serializes. This page describes the value side of the process; Variables, mixins, and namespaces describes the scope that supplies those values.
Function registry
The function registry maps case-insensitive function names to callable operations. Less creates the built-in set from grouped function families, then gives nested evaluation scopes inherited registries so plugins or local code can add functions without losing access to the parent set. packages/…/index.js• L1-34 packages/…/function-registry.js• L1-36 packages/…/ruleset.js• L181-193
The built-in groups have different jobs:
| Group | Value purpose |
|---|---|
| Boolean and default | Select values, test whether a name is defined, and resolve guarded default() choices. |
| Color and blending | Create colors, read color channels, change color properties, mix colors, and blend two colors. |
| List | Read, count, create, and iterate over expression values. |
| Math and number | Calculate with dimensions, convert units, compare values, and produce numeric constants. |
| String | Quote, escape, replace, and format text. |
| URL and SVG | Embed resources, build SVG gradient data, and preserve URL values for CSS. |
| Type and style | Test value kinds or units, change a number's unit, read a unit, and preserve CSS style queries. |
Plugins use the same registry boundary to add functions. A plugin can receive the shared registry or a registry created for its own scope, so function names and registration scope are part of the extension contract. packages/…/plugin-manager.js• L29-41 packages/…/abstract-plugin-loader.js• L48-62
References: packages/…/index.js• L1-34 packages/…/function-registry.js• L1-36 packages/…/ruleset.js• L181-193 packages/…/plugin-manager.js• L29-41
Function calls
When Less evaluates a call, it looks up the lower-case name in the registry and normally evaluates its arguments before invoking the operation. Control functions such as if() and isdefined() opt out of this normal argument step so they can choose which value to evaluate or test. packages/…/function-caller.js• L3-51 packages/…/boolean.js• L8-27
The result becomes a Less value for later evaluation. If the name is not registered, or the operation returns no result, Less keeps the call and emits it as a CSS function. This preserves browser functions and lets CSS resolve values that Less cannot know yet, such as some runtime custom property expressions. packages/…/call.js• L35-45 packages/…/call.js• L67-112
Math context also crosses this boundary. calc() keeps its arithmetic for CSS, while nested Less functions can still evaluate where their inputs are known. Math functions with a CSS equivalent can remain as calls for runtime values; functions without one must receive a number during compilation. packages/…/call.js• L50-65 packages/…/math-helper.js• L3-20
The call path is shown below. A successful built-in produces a typed value, while an unknown or unresolved call continues to CSS generation.
flowchart LR
A[Parsed function call] --> B[Lookup name in current registry]
B -->|Found| C[Evaluate required arguments]
C --> D[Run built-in operation]
D --> E[Less value]
B -->|Missing| F[Evaluate arguments]
D -->|No result| F
F --> G[CSS function call]
E --> H[CSS generation]
G --> H
References: packages/…/function-caller.js• L3-51 packages/…/call.js• L50-112 packages/…/math-helper.js• L3-20
Value operations
Built-in operations work on the typed values created by the Less expression tree. A dimension carries a number and unit. A color carries channels and alpha. An expression or value can hold an ordered list of items. Functions use these forms to perform compile-time work while preserving the value form needed by CSS output.
Color operations
Color functions create colors from RGB, HSL, or HSV inputs and expose their channels as dimensions. They also support color adjustments such as saturation, lightness, alpha, hue rotation, mixing, tinting, shading, contrast selection, and grayscale conversion. Blending functions combine two colors with modes such as multiply, screen, overlay, difference, and exclusion. packages/…/color.js• L59-86 packages/…/color.js• L200-449 packages/…/color-blending.js• L3-85
Color operations require values that Less can treat as colors and use percentages or dimensions according to the operation. Invalid color inputs do not become arbitrary CSS text through the color operation; they produce an evaluation error, while a browser filter function can remain a CSS call when the name is not claimed by Less. packages/…/color.js• L24-50 packages/…/color.js• L241-268
References: packages/…/color.js• L24-50 packages/…/color.js• L59-86 packages/…/color.js• L200-449 packages/…/color-blending.js• L3-85
List operations
List functions treat a single value as a one-item list and preserve the order of expression items. extract() reads a one-based position. length() counts items. range() creates numeric values with the requested end unit. each() applies a ruleset to each item and exposes the current value, key, and index to that ruleset, which connects list processing to mixins and scoped declarations. packages/…/list.js• L12-69 packages/…/list.js• L71-157
The list result is still part of the Less tree. A caller can use the result as a CSS value or use each() to produce a set of evaluated rules. This makes list functions a bridge between value processing and the scoped rule model described in Variables, mixins, and namespaces.
References: packages/…/list.js• L12-69 packages/…/list.js• L71-157 packages/…/expression.js• L9-91
Math and number operations
Math functions evaluate common numeric work such as rounding, roots, absolute values, trigonometry, and powers. Number functions add unit conversion, min(), max(), mod(), percentage(), and the pi constant. Results retain or assign units according to the operation, so the output remains a valid Less dimension or a CSS expression. packages/…/math.js• L3-29 packages/…/number.js• L5-57 packages/…/number.js• L60-94
min() and max() can compare compatible dimensions and can preserve a CSS min() or max() call when Less must defer the comparison. Other operations need compile-time numbers unless they have a CSS form that the browser can evaluate. Unit mismatch or a non-numeric input is an argument error rather than a guessed result. packages/…/number.js• L19-57 packages/…/math-helper.js• L3-20
References: packages/…/math.js• L3-29 packages/…/math-helper.js• L3-20 packages/…/number.js• L5-57 packages/…/number.js• L60-94
String operations
String functions control how text moves from Less values into CSS. e() produces quoted text. escape() URI-encodes selected CSS characters. replace() applies a regular-expression replacement. The %() function inserts values with string, CSS, or encoded placeholders. packages/…/string.js• L5-37
These operations preserve useful quote and escape state where possible. The result can therefore act as a CSS value or as text for interpolation without being parsed again as unintended Less syntax.
References: packages/…/string.js• L5-37 packages/…/quoted.js• L1-66 packages/…/anonymous.js• L1-44
Type operations
Type functions let Less branch on the kind of a value before another operation uses it. The is* family checks rulesets, colors, numbers, strings, keywords, URLs, and units such as pixels, percentages, and em; each check returns the Less true or false keyword. unit() changes the unit on a number without changing its numeric value, and get-unit() returns the current unit as CSS text. packages/…/types.js• L10-50 packages/…/types.js• L51-69
The related boolean functions provide value selection and definition checks. if() evaluates only the selected branch. isdefined() tests a variable without turning a missing variable into a compile error. The special default() result supports guarded mixin selection rather than ordinary value conversion. packages/…/boolean.js• L4-29 packages/…/default.js• L4-23
References: packages/…/types.js• L10-69 packages/…/boolean.js• L4-29 packages/…/default.js• L4-23
Resource and CSS value operations
Some built-ins connect expression evaluation to resources or to CSS features that Less should preserve. These functions use the compilation environment for file access and encoding, or return URL-like values that CSS generation writes in url(...) form.
URL operations
The data-uri() function reads a resource through the active file manager and embeds its contents as a data: URL. It can infer a MIME type, choose URL encoding or Base64, retain a fragment, and fall back to an ordinary URL when the file manager cannot load the resource or the environment cannot encode it. packages/…/data-uri.js• L6-35 packages/…/data-uri.js• L38-72
Ordinary url(...) values are evaluated as URL nodes rather than as a color or math value. URL evaluation normalizes paths, applies root-path rewriting when enabled, and adds configured URL arguments except for data URLs. These rules make URL output depend on both the expression and the render environment. packages/…/url.js• L13-29 packages/…/url.js• L37-80
References: packages/…/data-uri.js• L6-72 packages/…/url.js• L13-80 packages/…/index.js• L23-30
SVG operations
The svg-gradient() function turns a direction and a set of color stops into an inline SVG gradient. It supports linear directions such as to right and to bottom, plus a centered ellipse form. The result is URL-encoded as an image/svg+xml data URL so a CSS property can use it as an image value. packages/…/svg.js• L7-61 packages/…/svg.js• L62-86
Each stop must contain a color and can contain a dimension for its position. The function supplies start and end positions when they are omitted, but invalid directions, too few stops, or invalid stop values stop evaluation with an argument error.
References: packages/…/svg.js• L7-61 packages/…/svg.js• L62-86
Change checklist
When these files or sections are changed, remember to consider these:
- Keep built-in registration aligned with lower-case lookup and inherited scope registries. A name can silently stop resolving if registration and lookup use different names or scopes.
- Preserve the unresolved-call contract. Returning no result must still allow a valid CSS function to reach output when the browser owns the operation.
- Keep lazy argument evaluation for
if(),isdefined(), and other control functions. Eager evaluation can turn a safe branch or definition check into an error. - Keep math behavior aligned with
calc()and runtime CSS values. A function that has no CSS equivalent must not accept an unresolved value as if it were a number. - Keep list iteration variable bindings aligned with the ruleset scope. Changes to value, key, or index names can alter generated declarations without changing the list itself.
- Keep URL functions aligned with file-manager, MIME, encoding, root-path, and URL-argument services. A change in one service can change whether output is embedded, rewritten, or left as a URL.
- Keep type checks and unit changes aligned with the value classes used by expressions. Returning a raw host boolean or changing a numeric value during
unit()breaks later Less conditions or CSS output.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A function is absent from the active registry or is registered under the wrong case | Less preserves the call as CSS text instead of computing the intended value | Register the name through the function registry and keep scope inheritance intact |
| A color, number, or unit function receives the wrong value kind | Evaluation stops with an argument error or produces no usable value | Check the value type before calling the operation and use compatible units |
An unused if() branch or missing variable is evaluated eagerly | A branch that should be safe raises an undefined-variable or other evaluation error | Preserve non-eager argument handling for control functions |
A non-CSS math function receives var() or env() | Less cannot produce a number and compilation stops | Resolve the value in Less or use an operation that CSS can evaluate |
data-uri() cannot read a file or encode its contents | Less warns and emits a fallback URL instead of embedded data | Provide a file manager and Base64 encoder when embedding is required |
svg-gradient() receives an unsupported direction or invalid stop | Compilation stops with an SVG gradient argument error | Use a supported direction and color stops with valid dimensions |
| URL rewrite settings do not match the current file context | Generated asset paths point to the wrong location or receive wrong URL arguments | Keep root path, current directory, rewrite mode, and URL arguments consistent |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.js | Shows the built-in function families that form the registry. |
| packages/…/function-registry.js and packages/…/function-caller.js | Define name lookup, inherited scopes, argument evaluation, and plugin-facing registration behavior. |
| packages/…/call.js | Shows how a call becomes a Less value or remains a CSS function. |
| packages/…/color.js and packages/…/color-blending.js | Define color creation, channel access, color changes, and blend modes. |
| packages/…/list.js | Shows list access, range creation, and ruleset-based iteration. |
| packages/…/math.js, packages/…/math-helper.js, and packages/…/number.js | Define numeric evaluation, units, CSS deferral, and number helpers. |
| packages/…/string.js and packages/…/types.js | Define text conversion, replacement, type checks, and unit operations. |
| packages/…/data-uri.js and packages/…/url.js | Show resource embedding and URL rewriting at the runtime boundary. |
| packages/…/svg.js | Shows how gradient values become SVG data URLs. |
| packages/…/boolean.js and packages/…/default.js | Define lazy branching, definition checks, and guarded default selection. |