Choosing Between Less Variables and CSS Custom Properties for Theming in Modern Less Projects
A decision guide comparing Less variables and CSS custom properties for theming, covering browser support, runtime flexibility, build-time optimization, and a hybrid migration strategy.
31 May 2026, 17:42 UTC

The Decision: Compile-Time Constants vs Live Browser Values
Modern Less projects (v3.x/4.x) face a fundamental theming choice: stick with Less variables (@var) that resolve at build time, or adopt CSS custom properties (--var) that live in the browser and can change at runtime. The right pick depends on whether you need build-time logic (loops, conditionals, math) or zero-rebuild theme switching. A hybrid approach—defining tokens as Less variables and emitting them as CSS custom properties—often delivers the best of both worlds.
Constraints That Shape the Choice
- Browser support: CSS custom properties work in all modern browsers; IE11 requires a polyfill or a separate fallback stylesheet. Less variables compile to static values, so they work everywhere.
- Runtime flexibility: Only CSS custom properties can be updated via JavaScript or media queries without a rebuild. Less variables are frozen after compilation.
- Build-time optimization: Less variables enable compile-time loops, conditionals, and math that CSS custom properties cannot express (e.g.,
@media (min-width: @breakpoint)or@keyframes @name). - Migration path: Existing Less codebases can incrementally adopt CSS custom properties by adding a plugin that emits
:root { --token: @token; }.
Quick Comparison
| Aspect | Less Variables (@var) | CSS Custom Properties (--var) |
|---|---|---|
| Evaluation time | Compile-time (build) | Runtime (browser) |
| Browser support | Universal (compiles to static CSS) | Modern browsers; IE11 needs polyfill |
| Runtime mutability | No (requires rebuild or client-side Less compiler) | Yes (via JS setProperty or media queries) |
| Use in media queries / @keyframes | Supported | Not supported |
| Performance | Zero runtime overhead | Minimal overhead; enables dynamic updates without CSSOM manipulation |
| Tooling integration | Native in Less; works with any bundler | Requires Less 3.5+ plugin (less-plugin-css-variables) or --js flag; PostCSS for fallbacks |
Trade-offs in Detail
Build-Time Logic
Less variables shine when you need to generate repetitive styles, calculate sizes, or conditionally include rules. For example, a loop that creates utility classes for a spacing scale (.m-@{i} { margin: @i * 1rem; }) only works with Less variables. CSS custom properties cannot drive such logic because they are resolved after the CSS reaches the browser.
Runtime Theme Switching
If your product requires user-selectable themes (light/dark, brand colors) without a page reload, CSS custom properties are the only practical choice. A single line of JavaScript—document.documentElement.style.setProperty('--primary', '#cc0000')—updates every component that references var(--primary). Achieving the same with Less variables would mean compiling a separate stylesheet per theme or shipping the ~100 KB less.js runtime, which blocks rendering and is not recommended for production.
Browser Support and Fallbacks
Less variables sidestep browser compatibility entirely because they become plain CSS values. CSS custom properties require a fallback strategy for IE11: either a polyfill (adds weight) or a PostCSS pass (postcss-custom-properties with preserve: false) that replaces var(--token) with static values in a dedicated legacy stylesheet. Note that PostCSS does not polyfill calc() expressions containing custom properties for IE11.
Performance Overhead
Less variables add no runtime cost—the browser sees only the computed values. CSS custom properties introduce a tiny overhead for the cascade and inheritance machinery, but this is negligible in modern engines. The real performance win comes from avoiding full CSSOM manipulation when themes change: swapping a custom property value is far cheaper than replacing a <link> or injecting a new <style> block.
Hybrid Implementation: Design Tokens as Less Variables, Output as CSS Custom Properties
The recommended pattern for design systems is a clear token layer:
- Define all design tokens as Less variables in a central file (e.g.,
tokens.less). - Use a Less plugin to emit those tokens as CSS custom properties on
:root. - Reference
var(--token)in component stylesheets.
Example tokens.less
// tokens.less
@primary: #0066cc;
@spacing-unit: 8px;
@font-base: 1rem;
// Emit as CSS custom properties (requires less-plugin-css-variables)
:root {
--primary: @primary;
--spacing-unit: @spacing-unit;
--font-base: @font-base;
}
Installation and Compilation
Run in your project root (requires Node 14+ and npm/yarn):
npm install --save-dev less less-plugin-css-variables
# or yarn add -D less less-plugin-css-variables
Compile with the plugin enabled (Less 3.5+):
npx lessc --plugin=less-plugin-css-variables tokens.less tokens.css
Expected check: Open tokens.css and verify it contains :root { --primary: #0066cc; --spacing-unit: 8px; --font-base: 1rem; }. Risk: Less 4.x changes the plugin API; confirm less-plugin-css-variables compatibility before upgrading.
Component Usage
// button.less
@import 'tokens.less'; // only for @primary if needed for compile-time math
.button {
background-color: var(--primary);
padding: calc(var(--spacing-unit) * 2);
font-size: var(--font-base);
}
Runtime Theme Switch
In your application JavaScript (runs in browser):
function setTheme(primaryColor) {
document.documentElement.style.setProperty('--primary', primaryColor);
}
// Example: setTheme('#cc0000');
No rebuild, no reload—every .button updates instantly.
Validating the Setup
- Compile and inspect output: Run the
lessccommand above. Confirm the generated CSS includes the:rootblock with custom properties. - Test theme switch in browser: Load a page that includes the compiled CSS, open DevTools console, execute
document.documentElement.style.setProperty('--primary', '#cc0000'), and observe immediate color change on.buttonelements. - Measure CSS size difference: Compare gzipped size of a build using only Less variables vs. the hybrid output. Use
gzip-size-cli:
The hybrid version will be slightly larger due to thenpx gzip-size-cli tokens.cssvar()references, but the difference is typically under 1 KB gzipped. - Validate IE11 fallback (if required): Run PostCSS with
postcss-custom-properties:
Check thatnpx postcss tokens.css --use postcss-custom-properties --postcss-custom-properties.preserve=false -o tokens.ie11.csstokens.ie11.csscontains static values (e.g.,background-color: #0066cc;) and novar()functions.
Limitations and Cautions
- Less 4.x plugin API changes: Verify that
less-plugin-css-variables(or any custom plugin) is compatible before upgrading Less major version. - CSS custom properties cannot be used in media query definitions or
@keyframesnames. Keep those in Less variables. - Mixing both systems without clear boundaries causes duplication. Enforce the token layer: Less variables → CSS custom properties → component usage. Never reference a Less variable directly in a component if it has been emitted as a custom property.
- Runtime Less compilation (
less.js) is ~100 KB gzipped and blocks rendering. Avoid in production. - PostCSS autoprefixer does not polyfill custom property values inside
calc()for IE11. If you need IE11 support forcalc(var(--spacing) * 2), generate a separate IE stylesheet with resolved values.
Practical Verification Checklist
| Step | Command (run in project root) | Expected Result | Risk / Note |
|---|---|---|---|
| Confirm Less version | npx lessc --version | Output shows 3.5+ or 4.x | Plugin support requires 3.5+ |
| Install plugin | npm i -D less-plugin-css-variables | Package added to devDependencies | Check compatibility with Less 4.x |
| Compile tokens | npx lessc --plugin=less-plugin-css-variables tokens.less tokens.css | tokens.css contains :root { --primary: #0066cc; ... } | Ensure no syntax errors in Less file |
| Test runtime switch | In browser console: document.documentElement.style.setProperty('--primary', '#cc0000') | Components using var(--primary) update instantly | Requires page to include compiled CSS |
| Check gzipped size | npx gzip-size-cli tokens.css | Size reported; compare with Less-only build | Install gzip-size-cli if missing |
| Generate IE11 fallback | npx postcss tokens.css --use postcss-custom-properties --postcss-custom-properties.preserve=false -o tokens.ie11.css | tokens.ie11.css has static values, no var() | Does not polyfill calc() with custom properties |
Use this checklist each time you add or modify design tokens to ensure the hybrid pipeline remains intact.