Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define constants in CSS?

Tags:

css

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; } 
like image 423
zmol Avatar asked Jan 31 '11 05:01

zmol


People also ask

Can I use constants in CSS?

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.

Can we declare variable in CSS?

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.

Can we define variable as constant?

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.

How constant can be defined?

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.


1 Answers

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; } 
like image 76
probabilityzero Avatar answered Sep 28 '22 05:09

probabilityzero