Help:CSS variables

What is a CSS variable, and why do we care?
CSS variables are a magical syntax that allow you to define a value in one place and use it in many other places. This has two main benefits:


 * We can write the same thing fewer times, which makes changing it easier. Instead of changing in 10000 places, we can change in 1 place.
 * We can "interrupt" the cascade, so to speak—the order in which CSS does things, we get to violate that in a way, by putting CSS variables near the bottom, so that it has to go all the way back to the top again! Woah! Cool! Super cool!

Defining variables
Well if you're using ThemeDesigner, TD will just do it all for you, magically. Yay! But you should also know the syntax.

Explanation of cascade
We now have variables called  and   that are defined in the ROOT scope. This means they are available EVERYWHERE, but also at the least level of specificity. Remember what I said about the cascade in the intro? Yeah, the variable definitions cascade too.

Now we've defined these variables in, so that they're a much lighter color. Where we have BOTH of these definitions at the same time (aka in light skin) the light ones will apply due to the cascade, and the default ones will apply in the dark skin.

How you would actually do this IRL
When defining variables for real, don't use the  scope unless you really mean it. Simply define one scope for  and one for , like this:

However, sometimes really you do want to use the cascade, such as redefining part of your theme within a special  class that you apply to one page on April Fools' Day. So our first example did things slightly incorrectly, in order to teach you how the cascade works.

Using variables
Now, this class will have either the dark blue or the light blue, no matter which theme is selected! The cascade finds  and is like "yay I'm done! I got to the background-color!" But then it's like "awwwwwww I got a VARIABLE nowwwwwwwwwwww what" and it has to go ALL THE WAY BACK and look at all of the applicable variable definitions til it finds the most specific one, thereby redoing the entire cascade! omg! Then it gets to either the light variable definition or the dark variable definition, and THEN it applies it and it's done. Hooray!

Note that the alternative would have been to hard-code the colors TWICE here, once for  and once for. So by giving us the ability to interrupt the cascade, LITERALLY EVERY SINGLE color definition gets to be written only a single time. So the first bullet point is magnified like 100x (or, okay, 2x).

Inline variables
We just showed how to use variables inside a CSS file like MediaWiki:Common.css. But can you use variables inline, like this?


 * On originally-Gamepedia wikis: Yes, but please make every attempt to use classes instead.
 * On originally-Fandom wikis: No

If you want to empower your non-admin users to style elements flexibly, I'd suggest defining one class per variable that ONLY applies it as a background color or font color, depending on what it's intended for. (If it's a pair of variables intended to be used together under all circumstances, background-color & color, you could consider pairing them together for a class with two rules instead of just one.) Then document this list of classes somewhere that your users can find, and ensure they are always using these classes when they need the color you've made!