How do I access the value of a data-attribute and use it in a CSS declaration
How do I access the value of a data-attribute and use it in a CSS declaration
Q: How can I access the value of a data-attribute and use it inside the declaration?
data-attribute
Edit: I'm expanding the scope to include Sass.
For example, assume I have the following HTML:
<div class="black box">
<div data-color="green">
This text should be green
</div>
<div data-color="red">
This text should be red
</div>
<div data-color="white">
This text should be white
</div>
</div>
That renders to this:

using the following CSS:
/* Ignore this */
.black.box
background-color: black;
/* Refactor the three rules below into a single rule */
div[data-color="green"]
color: green;
div[data-color="red"]
color: red;
div[data-color="white"]
color: white;
How can I refactor the three repetitive CSS rules into something like this pseudocode:
div[data-color=VALUE]
color: VALUE;
Here's fiddle to help: https://jsfiddle.net/gilani/54de0zn6/7/
@JBDouble05 no he don't need JS, he need SASS or LESS
– Temani Afif
Aug 24 at 0:02
@JBDouble05, I was afraid of that.
– Amin Shah Gilani
Aug 24 at 0:03
@TemaniAfif I'd settle for a Sass way of doing it.
– Amin Shah Gilani
Aug 24 at 0:03
wait for a SASS guy and you will get it ;) something like this is easily doable with SASS
– Temani Afif
Aug 24 at 0:07
1 Answer
1
In this particular case using inline style is one solution since the values are already added inline:
.black
background:black;
font-size:30px;
<div class="black box">
<div style="color:green">
This text should be green
</div>
<div style="color:red">
This text should be red
</div>
<div style="color:white">
This text should be white
</div>
</div>
You can also consider the use of CSS variable for more complex situations (also relying on inline style)
.black
background:black;
font-size:30px;
.black > div
color:var(--c);
border:5px solid var(--c);
<div class="black box">
<div style="--c:green">
This text should be green
</div>
<div style="--c:red">
This text should be red
</div>
<div style="--c:white">
This text should be white
</div>
</div>
I appreciate the help, however, it has to be a
data attribute because the real reason I'm using this is to change the number on a li tag :) I'm definitely going to keep this in mind going forward, though.– Amin Shah Gilani
Aug 24 at 0:25
data
li
@AminShahGilani you mean what by changing the number of li ? I am pretty sure It can be done without data-attribute ;)
– Temani Afif
Aug 24 at 0:32
In normal circumstances, yes, but they're limited to numerics, or single alpha (1,2,3 and a,b,c respectively), but in the docs I'm converting, some lists have items like 1,2, 2A, 3, 4, 5, etc. It's insane how bad legislature is. Here: stackoverflow.com/questions/51980074/…
– Amin Shah Gilani
Aug 24 at 0:36
@AminShahGilani CSS variable can do it jsfiddle.net/rw4ga3dz/1 I don't see how data-attribue will help in this case
– Temani Afif
Aug 24 at 0:39
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You can't do it with CSS, you need to use JavaScript. Would you like an explanation of how to do it with JavaScript?
– JBDouble05
Aug 24 at 0:01