by nashashmi · 1y ago · 0 comments
:root { --color: blue; }
div { --color: green; }
#alert { --color: red; }
* { color: var(--color); }
<p>I inherited blue from the root element!</p>
<div>I got green set directly on me!</div>
<div id='alert'>
While I got red set directly on me!
<p>I’m red too, because of inheritance!</p>
</div>
Excuse my negativity, but this is messed up. I am trying to rationalize whyyyy???.It seems every object is given variables (--abc). And then there are global variables and local variables. I guess this is the "cascading" feature. var is a function computed at the time of instantiation. And refers to local variables first. Then looks at global variable. Inheritance comes from ?? The p tag seems it is not root so therefore it is not blue.
Having explained it, I think about it better, but this really messes up how I thought of CSS. CSS is where the second stanza overwrites the first stanza. Yet global and local variables really hurts my head. A few complex CSS files later, it is bound to be unusable to determine result without getting a computer program to help.
In the last line of CSS, you went on to equate a built-in property with the value of the variable everywhere. So you’ve essentially made a new “property” that works just like the builtin color property.
(Disclaimer: I’m not an expert in modern CSS, so this could be imprecise.)
Nit: only inherited properties are inherited.
For example color and font-family are, while padding and background-color are not.
One of the special rules you kind of just need to get a feel for that makes CSS a little inscrutable to those who don’t practise it every day.
#alert { color: red }
* { color: blue }
Variables behave slightly differently, in that they don't follow specificity rules. The definition of the variable / property behaves like a scope, and affects everything within it, hence the <p> being colored red instead of blue.Variables don't follow specificity in the same sense that other properties don't follow specificity. Specificity only applies to rules in the first place, not to properties.
I am out of my comfort zone here, but if that were true then at least I’d find it understandable. Variables act like properties and rules still act like rules.
Variables don't use specificity like CSS selectors. I prefer to create a few layers for variables; one for color names, one that maps color name variables to global semantic names (i.e. --primary-cta-color: var(--blue-50) or something), and maybe go so far as to add another that maps component-specific semantics to global specific semantics (i.e. --date-picker-hover-color: var(--primary-cta-color)).
That way, you can add something like
.theme-1 {
--primary-cta-color: var(--blue-50);
}
.theme-2 {
--primary-cta-color: var(--brown-40);
}
and / or @media (prefers-color-scheme: dark) {
.theme-1 {
--primary-color: var(--grey-30);
}
.theme-2 {
--primary-color: var(--brown-10);
}
}
as a purely hypothetical example. Slap the class name your user wants on the body tag, and you're good to go.A css property is set at a context, for example
body {color: blue}
div { color: green }
#alert {color: red}
https://codepen.io/bryanrasmussen/pen/PwNQyOm
this would be preferable to what was done variables because it is more straightforward. The dumb example of custom property using you highlight does not help make clearer code, nor does it help make css easier to understand, but I guess they were hoping people would see that custom properties inherit basically the same way that other properties inherit, except that there is this freaky :root context instead of using body.
obviously things can be complicated because only some properties inherit, but the claim holds. CSS properties are set by context.