I use a few colors throughout my CSS style sheet. For example,
#testdiv{ background: #123456; }
Is it possible to define that color by name so I can reference it in the CSS sheet like the following?
#testdiv{ background: COLORNAME; }
CSS constants don't exist in a spec, they're currently just variables that aren't re-assigned. Whether CSS is a programming language or not, it results in a different environment for variables to live, compared to something like Javascript.
To declare a variable in CSS, come up with a name for the variable, then append two hyphens (–) as the prefix. The element here refers to any valid HTML element that has access to this CSS file. The variable name is bg-color , and two hyphens are appended.
Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.
Constants can be defined using the const keyword, or by using the define()-function. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined.
Not with plain CSS, but there are some CSS extensions that you could use, like Sass or less-css.
Here's an example of Less CSS:
@color: #4D926F; #header { color: @color; } h2 { color: @color; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With