Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a dynamic property name in var()

I am trying to use custom properties based on dynamic values, such as attr().

For instance, in below code, I tried to access both custom-properties based on the current element's id attribute.

:root{
  --app-foo: 'Hello';
  --app-bar: 'World';
  }
div::after{
/* expected :
            'Hello' for #foo
            'World' for #bar    */
  content:  var('--app-'attr(id));
  }
<div id="foo"></div>
<div id="bar"></div>

But it obviously fails.

Is there some way to achieve this?

like image 259
Tygari Avatar asked Dec 08 '17 04:12

Tygari


People also ask

Can we add dynamically named properties to JavaScript object?

In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array. To do, so you can create a variable and assign it a particular value.

Can I create dynamic variable in JavaScript?

The simplest JavaScript method to create the dynamic variables is to create an array. In JavaScript, we can define the dynamic array without defining its length and use it as Map. We can map the value with the key using an array and also access the value using a key.


1 Answers

The grammar does not permit the first argument of a var() expression to be anything but a single custom property name that is hardcoded. This means that you cannot choose which custom property to reference in a var() expression using CSS, since it doesn't accept a string (or an attr() expression returning a string) for a property name. You'll need to set the value containing the var() expression using JS.

like image 142
BoltClock Avatar answered Nov 03 '22 00:11

BoltClock