I wonder if there is a possibility to use custom-attribute
of type data-*=" "
as a value for a CSS style or even use this custom date as value for a variable in the CSS.
For example, I'll explain my idea. I'd like to put in the element type <div data-opacity="0.5" ></div>
and this div
get 0.5 opacity. So I need CSS to somehow recognize this value of 0.5% of data-opacity
as the value of opacity:
in the class
I tried to use attr
direct as value of style opacity: attr(data-opacity)
and also tried to declare as variable, however tb did not work --data-opacity: attr(data-opacity)
This is just an example use, but could be for color, margins, etc. I wonder if there is any way to use this custom-attribute
as value within CSS?
Here's an example of what I'd like to do, but it does not work as 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>