I am looking to use an HTML data attribute to assign a value
to a CSS property.
Essentially my HTML is like this:
<div class='dynamic-color' data-assigned-color='red'>lorem ipsum</div>
<div class='dynamic-color' data-assigned-color='blue'>lorem ipsum</div>
<div class='dynamic-color' data-assigned-color='green'>lorem ipsum</div>
And I want my CSS to be something like:
.dynamic-color {
color: [data-assigned-color];
}
My current working solution is:
.dynamic-color[data-assigned-color='red'] {
color:red;
}
...
However, there are several issues with this.
- when users add values through the UI to the database, then I also need to update the CSS
- Highly repetitive code
NOTE: I am using LESS so a LESS or pure CSS solution is good.