Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Variable arrays - possible?

I understand how to use variables in CSS. They have helped heaps with speeding up my work flow for my clients' sites.

I have noticed on more than a few occasions I would write down variables with a number next to it to assign to a particular color or look because there is no array system in place. I ended up creating a quasi-array so to speak.

Like this

--c1: color:#ddd;  //link color
--c2: color:#aaa;  //quote color
--c3: color:#444;  //body text

Is it possible to do something like this in CSS?

--c new array (color:#ddd, color:#aaa, color:#444);

Then call array

arr(--c,3); 

instead of

var(--c3);

I get that CSS is not a OOP, but it would be nice to be able to handle data like this, especially if I can extend it across two dimensions. I also understand this may be a pointless exercise in theory as typing --c3 is faster than doing --c,3 or even --c[3]. Food for thought.

like image 631
DeveloperDoug Avatar asked Oct 27 '16 04:10

DeveloperDoug


People also ask

Can you have an array of variables?

An array is a variable containing multiple values. Any variable may be used as an array. There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously.

Can we override CSS variables?

Moreover, these have either a global scope or a local scope and can be accessed using the var() function. You can avoid writing down repeated CSS values by using these variables, furthermore, these are easier to understand. It is also possible to override CSS variables with one another.

Are CSS variables case sensitive?

CSS variables can be used in HTML's style attribute. You can choose to set the value of your variables inline, and they'll still work as expected. CSS variables are case-sensitive.

Can you assign variables in CSS?

CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries. A good way to use CSS variables is when it comes to the colors of your design.


Video Answer


1 Answers

Properties in CSS cannot be grouped in arrays in the way you describe. This includes custom properties. Preprocessors such as Sass and LESS have maps and lists, but the way custom properties work (see section 3 of the spec) means it's not possible to group them and index off of them using the same syntax found in preprocessors.

like image 71
BoltClock Avatar answered Oct 20 '22 08:10

BoltClock