Less 4 changed how division works
Here's a bug that doesn't look like one. You upgrade a Less project to version 4, the build passes, and some spacing on the page is just gone. No error, no warning.
Here’s a bug that doesn’t look like one. You upgrade a Less project to version 4, the build passes, and some spacing on the page is just gone. No error, no warning. Open the compiled CSS and you find this:
.card {
padding: 16px / 2;
}
That isn’t valid CSS, so the browser quietly drops the whole declaration. The source was padding: @gutter / 2;, which Less 3 turned into 8px. Less 4 leaves it alone.
Why Less stopped dividing
The slash means something in plain CSS, and it’s used more every year:
.item { grid-area: 1 / 3; }
.video { aspect-ratio: 16 / 9; }
.text { font: 14px/1.5 sans-serif; }
If Less treats every / as division, it breaks these. With the old behaviour (math: always in Less 4), grid-area: 1 / 3 compiles to 0.33333333 and aspect-ratio: 16 / 9 to 1.77777778. Both are wrong, and both fail just as quietly.
So Less 4 made parens-division the default. Division only happens inside parentheses, or with the new ./ operator. Other maths (+, -, *) works everywhere as before.
@gutter: 16px;
.a { padding: @gutter / 2; } // 16px / 2 (left as is)
.b { padding: (@gutter / 2); } // 8px
.c { padding: @gutter ./ 2; } // 8px
.d { width: @gutter * 2; } // 32px
.e { grid-area: 1 / 3; } // 1 / 3 (correct now)
The parentheses that don’t count
This one caught me out. You might expect a function call’s parentheses to be enough:
.col { width: percentage(@n / @columns); }
They aren’t. That fails with “Error evaluating function percentage: argument must be a number”, because @n / @columns reaches the function as a value with a slash in it, not a number. It needs its own pair:
.col { width: percentage((@n / @columns)); }
The good news is that this one fails loudly. The bare / in a property value is the one that fails silently.
And inside calc()
Less doesn’t do maths inside calc() at all, and hasn’t since version 3. It only fills in variables:
.main { width: calc(100% - (@gutter / 2)); }
// calc(100% - (16px / 2))
That’s fine, because the browser’s calc() can divide. Just don’t be surprised when the output still has a slash in it.
Finding the broken lines
The quickest way I know is to build the CSS and search the output, not the source. A number, a unit, a slash and another number is almost always a division Less skipped:
npx lessc src/styles.less | grep -nE '[0-9](px|rem|em|%)? / [0-9]'
You’ll get some false positives (a font shorthand, a grid-area), but it’s a short list to read by eye. Fix each one with parentheses or ./.
If you need the old behaviour while you migrate, you can still ask for it:
// webpack: less-loader
{
loader: 'less-loader',
options: {
lessOptions: { math: 'always' },
},
}
Less prints a deprecation warning for it, and it brings back the grid-area and aspect-ratio problems. Treat it as a way to ship the upgrade today, not as the fix.
Inline JavaScript is off too
If you use a component library written in Less, like Ant Design 4, you may also hit this:
SyntaxError: Inline JavaScript is not enabled. Is it set in your options?
Less turned inline JavaScript (code between backticks) off by default in version 3, and some libraries still use it for colour functions. You have to opt back in:
// webpack
options: { lessOptions: { javascriptEnabled: true } }
// vite.config.js
css: {
preprocessorOptions: {
less: { javascriptEnabled: true },
},
},
The feature that makes Less theming work
While you’re in there, it’s worth remembering the one Less behaviour that has no equivalent in Sass: variables are lazy. The last definition in a scope wins, even if it comes after the place the variable is used:
.button { color: @brand; }
@brand: red;
@brand: blue;
// .button { color: blue; }
That looks odd, but it’s exactly how theming in Less libraries works. The library uses @primary-color everywhere, and you redefine it after importing the library (or with modifyVars), and every use picks up your value. It’s also why moving a Less theme to another tool is harder than it looks: the other tool usually doesn’t work this way.
The short version
- A bare / in a value is no longer division. Wrap it in parentheses or use ./.
- Function parentheses don’t count: percentage((@a / @b)).
- Search the compiled CSS for leftover slashes, because nothing else will warn you.
- math: ‘always’ is a temporary bridge, not a fix.
The change is a little annoying for old code, but it’s the right call. CSS keeps adding syntax that uses /, and a preprocessor that guesses wrong silently is worse than one that asks you to be explicit.