Given the <style> I wrote, I expected the 1st <h1> and 2nd <h1> to have same font-size, but it is not true:
:root {
--default-size: calc(1rem + 0.5vw);
font-size: var(--default-size);
}
.font-size\:default {
font-size: var(--default-size) !important;
}
<h1>DIMENSION</h1>
<h1 class="font-size:default">DIMENSION</h1>
Digging around, I found this comment on stackoverflow https://stackoverflow.com/a/48415933/7253377 , and the problem seems related to how var() are evaluated, or better, when they are evaluated by the browser.
So, I wrote this simple snippet and it seems to work correctly:
:root {
--default-size: calc(1rem + 0.5vw);
}
* {
font-size: var(--default-size);
}
.font-size\:default {
font-size: var(--default-size) !important;
}
<h1>DIMENSION</h1>
<h1 class="font-size:default">DIMENSION</h1>
The only problem is that I don't think to get at 100% what is going on. The 1st approach didn't work because:
- The :root var() is evaluated when the :root is rendered.
- The * var() is evaluated when any element is rendered.
- The .font-size:default var() is evaluated when an element with this class is rendered
And the :root var() value happens to be different from the .font-size:default var() value? If yes, why?
The first issue is related to the default
font-sizeapplied toh1by the browser so to do your testing you need to use a "neutral" tag to avoid dealing with default styles.Try with divs
Now you will see a slight difference because of how
1remworks. It can be confusing but1remis relative to thefont-sizeyou set on the:rootelement and you set this one to be equal tocalc(1rem + 0.5vw)and we haveSo inside root the value is equal to
calc(16px + .5vw)and this value is inherited by the first div.Now the second div is also using
1remand this time1remwill equal tocalc(16px + .5vw)(the root font-size) so the value for the second div is equal tocalc(16px + .5vw) + .5vw = 16px + 1vw. It's.5vwbigger than the first oneIf you move the styles to the body for example, both will be equal because in both cases the reference will be the root font-size that is not defined so the initial value
16pxwill be used