Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using html data-attributes as css-variable (i.e. text-shadow)

i'm trying to use inline data-attributes as variables for css... is there any known option to get this run:

.mycss-class {text-shadow: attr(data-textshadow); }
<div class="mycss-class" data-textshadow="0 0 0 #000">lorem ipsum</div>

chrome dev-tool just reports "invalid property value"

many thanks & kind regards

like image 881
Gunnar Kaplick Avatar asked Sep 20 '17 13:09

Gunnar Kaplick


1 Answers

You can do this with CSS Custom Properties.

Support is pretty good, including Edge (but no IE)

p {
  width:80%;
  margin:1em auto;
  text-shadow: 2px 6px 2px grey;
}

p.colored {
  color: var(--mycolor)
}

p.shadowed {
  text-shadow: 2px 6px 2px var(--shadowcolor);
}
<p class="colored" style="--mycolor:red;">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam numquam aut aperiam excepturi id quaerat, fugiat, impedit natus maxime voluptates officia? Fuga earum quis exercitationem et fugiat, amet nam officiis?</p>

<p class="shadowed" style="--shadowcolor:green;">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quibusdam numquam aut aperiam excepturi id quaerat, fugiat, impedit natus maxime voluptates officia? Fuga earum quis exercitationem et fugiat, amet nam officiis?</p>
like image 124
Paulie_D Avatar answered Oct 14 '22 14:10

Paulie_D