question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Math Not Evaluated in CSS Custom Properties

See original GitHub issue

I’ve noticed that variable calculations do not happen inside css var declarations. This came up when I wanted to do this:

--gutter: @gutter-width / 2;

This got translated into:

--gutter: 24px / 2;

I know the process of deciding when it should and shouldn’t automatically do math is complicated, but is there a way to force it to do that math on a per-property basis? Something like escaping literal strings but for escaping expressions? I’d be fine with using a function like math() or less().

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
matthew-deancommented, Oct 9, 2018

This is by design. CSS Custom property values are not parsed the same as other values, because they can contain almost any value whatsoever. Variables are replaced, however.

You can easily work around this by performing math outside the custom property, and assigning to a new variable.

@gutter: 12px;
.custom {
  @gutter-width: @gutter / 2;
  --gutter: @gutter-width;
}

Closing as expected behavior.

0reactions
rjgottencommented, Jun 4, 2021

@jlixerkun That trades compile-time math done by the Less compiler for runtime math done by the browser, as well as increasing the size of the compiled CSS. If you’re dealing with a compile-time constant, then you should handle it at compile time and leave the runtime calc expressions as simple as possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Complete Guide to Custom Properties | CSS-Tricks
A custom property is most commonly thought of as a variable in CSS. ... Perhaps the most valuable reason to use them: not...
Read more >
Using CSS custom properties (variables) - MDN Web Docs
Using the var() function, you can define multiple fallback values when the given variable is not yet defined; this can be useful when...
Read more >
CSS variables with arithmetic operations don't work in ...
All the properties font-size , line-height , and font-family are set to browser defaults. If I split font shorthand property into three ...
Read more >
The future of CSS: Higher Level Custom Properties to control ...
Having one CSS Custom Property control a varying set of other properties — such as both colors and numbers — is not exactly...
Read more >
Everything you need to know about CSS Variables
CSS Variables are also called “Custom Properties” ... and do math like you want — you need to be careful with CSS variables....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found