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.

Nested calc with css variables

See original GitHub issue

thanks @evilebottnawi for redirection. #816

I’m encountering an issues while using calc() with CSS Custom variables where presets automatically remove my nested calc while custom variables is unable to resolve in build time.

Given that:

body {
  top: calc(100% - calc(10px + 10px));
}
/* will compile to*/
body {
  top: calc(100% - 20px);
}

when it comes to a css variables

body {
  top: calc(var(--pic-height) - calc(var(--width-height) + var(--border-gap)));
}
/* will compile to*/
body {
  top: calc(var(--pic-height) - var(--width-height) + var(--border-gap));
}

Capture

which is incorrect in mathematical stand points. To solve this problem, I end up by setting calc preset to false in plugin options.

Is there any possible to omit any clearing unintended nested calc while encountering css variable or if there is any better solution while dealing with variables value?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:5

github_iconTop GitHub Comments

3reactions
abstractvectorcommented, Mar 27, 2020

I raised an issue on cssnano (https://github.com/cssnano/cssnano/issues/628) but believe the issue lies upstream from there in this repo.

I am able to reproduce the same behavior, that the following two CSS styles produce different outputs (confirmed in Chrome 80 and Firefox 74 on Mac OS 10.15.3):

padding-bottom: calc(100% / (var(--aspect-ratio)));

Computed: padding-bottom: 276.906px (correct)

Screen Shot 2020-03-27 at 8 05 44 AM
padding-bottom: calc(100% / var(--aspect-ratio));

Computed: padding-bottom: 0px (incorrect)

Screen Shot 2020-03-27 at 8 05 28 AM

I’m not sure why the parentheses matter in this situation, but the empirical evidence suggests they do. I suggest therefore the parentheses should not be removed.

0reactions
ludofischercommented, Jan 10, 2022

So the first issue is already fixed in 8.2, while the issue in https://github.com/postcss/postcss-calc/issues/91#issuecomment-605059796 is a duplicate of #113

Read more comments on GitHub >

github_iconTop Results From Across the Web

calc() function inside another calc() in CSS - Stack Overflow
It is possible to use calc() inside another calc(). An example: div{ width: calc(100% - (1% + 30px));/* calc(1% + 30px) is nested...
Read more >
calc() - CSS: Cascading Style Sheets - MDN Web Docs
The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used with <length> , <frequency> ...
Read more >
A Complete Guide to calc() in CSS
calc () is a native CSS way to do simple math right in CSS as a replacement for any length value (or pretty...
Read more >
How and when to use CSS calc(): Tutorial with examples
The calc() function allows you to perform computations when specifying CSS property values. It's especially useful for calculating length, ...
Read more >
CSS: nested calc() for Custom Properties - CodePen
CSS: nested calc() for Custom Properties. (margin-top for vertical aligning). CSS variable is defined using calc();; Another variable/property is defined ...
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