Math Not Evaluated in CSS Custom Properties
See original GitHub issueI’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:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Closing as expected behavior.
@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.