DEV Community

Discussion on: CSS Variables

Collapse
 
samanthaming profile image
Samantha Ming

that's super cool! ...btw, do you mind explaining nested calc? πŸ™‚

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Sure, it's not widely known or atleast it wasn't usefull to nest calc function given that before variables, values where essentially hard coded.

:root {
   --grid-colomns: 12;
}

.thing {
    width: calc(100vw / var(--grid-columns) * calc(var(--grid-columns) * 2)
}

Okay this is a terrible example but it does illustrate how CSS can become config based and calc can do most of the work.

Thread Thread
 
samanthaming profile image
Samantha Ming

Oh cool!!! That’s super neat...I got to read this a few more time to understand it πŸ˜…...thanks so much for explaining it! Love learning new things ⭐️⭐️⭐️

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Your not the only one, I found this out recently, didn't know calc was nestable, your most welcome.

Thread Thread
 
frontendprof profile image
AbdulMalik

Can you explain what the code snippet does, please. Or can you direct me to some articles in which it is elaborated your topic please.

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

I wasn't sure what specifically you wanted to know but, AFAIK, there are no specific posts on the topic, I may have one (I have hundreds of posts so it might take some time to find).

What does it do? its a simple implementation of a 12 column grid layout normally calculated via a pre-processor, what it does is not so important, how it does it is whats interesting here.

The short story is:

CSS is calculated by the browser, you might consider this "run-time" for css, at this point a few things are possible, bellow is a broken down list of the things I have use and what they do:

vw units: 100 vw is the full viewport width as computed pixels, it is not 100% of the parent container.
css variables: --some-variable: red; stores a value, var(--some-variable) computes the value
calc(): runtime maths such as calc(100% + 20px) which is cool, but you can also just add or subtract numbers and make calculations normally computed in css pre-processors

Oh yes and lastly, I nested a calc() inside another calc to achieve complex maths

Thread Thread
 
frontendprof profile image
AbdulMalik

Thank you for the reply.
But I was implying explanation of your nested calc function. It would be great if you could elaborate on that one as well please, if you don't mind of course.

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

developer.mozilla.org/en-US/docs/W...

CSS calc 'calculate' is built into CSS and can be nested, the full documention can be found above.

However nesting refers to the fact that calc exaluates to a unit so then calc(calc(2px + 4px) + 4px)

10 pixels

But this is particularly useful for complex math with variables because variables can change in media query and stateful circumstances (:hover, :focus etc)

Thread Thread
 
frontendprof profile image
AbdulMalik

thanx