Here we need to consider two things: How custom properties works and how they are evaluated using var()
.
- The first part is trivial because custom property behave the same as any other property. From the specification we can read:
Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc.
Considering this, the last custom property defined in your case will override the first one:
:root {
--size-of-font: 5rem;
}
.logo{
font-size: var(--size-of-font);
}
:root {
--size-of-font: 1.2rem; /* This one will win !*/
}
.outer {
font-size: var(--size-of-font);
cursor: pointer;
text-align: center;
}
- When using
var()
we need to also consider some rules like defined in the same specification:
To substitute a var() in a property’s value:
- If the custom property named by the first argument to the var()
function is animation-tainted, and the var() function is being used in
the animation property or one of its longhands, treat the custom
property as having its initial value for the rest of this algorithm.
- If the value of the custom property named by the first argument to the
var() function is anything but the initial value, replace the var()
function by the value of the corresponding custom property. Otherwise,
- if the var() function has a fallback value as its second argument,
replace the var() function by the fallback value. If there are any
var() references in the fallback, substitute them as well.
- Otherwise, the property containing the var() function is invalid at
computed-value time
In our situation, we will consider the (2) because .logo
will inherit the custom property defined inside :root
with its value 1.2rem
(not an initial value).
In other words, the evaluation of a custom property doesn't consider the order of their appearance in the CSS file. It considers the value of the custom property that is resolved as an ordinary property.
Here another useful question where you can get more details and more examples: CSS scoped custom property ignored when used to calc var in outer scope