5
I would like to know if there is a possibility of using a custom-attribute
of the kind data-*=" "
as value for a CSS style or even use this custom date as a value for a variable in the CSS.
To illustrate better I will explain my idea. I would like to put in the type element <div data-opacity="0.5" ></div>
and that div
0.5 opacity. So I need the CSS to somehow recognize this 0.5 value of data-opacity
as the value of opacity:
in class
I tried to use the attr
direct as style value opacity: attr(data-opacity)
and also tried to declare as variable, but tb did not work --data-opacity: attr(data-opacity)
This is just an example of use, but could be for color, margins, etc I wonder if there’s any way to use that custom-attribute
as a value within the CSS?
Here’s an example of what I’d like to do, but it doesn’t work the way it is.
:root {
--data-opacity: attr(data-opacity);
--cor: #fff;
}
[data-opacity] {
color: var(--cor);
opacity: var(--data-opacity); /* não funciona */
/* opacity: attr(data-opacity); */ /* também não funciona */
}
.box {
width: 100px;
height: 100px;
background-color: #f00;
}
<div class="box" data-opacity="0.5">cor branca mas sem opacidade</div>
I’d even do a version on
js
but I believe that soon a version with onlyCSS
, rs– Jorge.M