How to use data-attr value for linear-gradient background
Asked Answered
A

1

11

given this input

.prettyRange {
  -webkit-appereance: none;
}

.prettyRange::-webkit-slider-runnable-track {
  background: -webkit-linear-gradient(right, #fff 0%, green 50%, #fff 0%);
}
<input class="prettyRange" type="range" name="capability" data-percent="100%">

But if i'm trying to set the % via attr(data-percent) it doesn't work.

background: -webkit-linear-gradient(right, #fff 0%, green attr(data-percent), #fff 0%)

How can I use the data-percent property of the item?

PS: I want to use the data-property because I need to update this value using JS and we cannot select pseudo-elements with JS.

Athodyd answered 20/4, 2018 at 7:39 Comment(1)
This feature is not yet available and even at risk for the next version of CSS drafts.csswg.org/css-values/#statusJackal
J
4

An alternative is to use CSS variable that you can easily adjust with JS:

.prettyRange {
  -webkit-appereance: none;
}

.prettyRange::-webkit-slider-runnable-track {
  background: -webkit-linear-gradient(right,#fff 0%,green var(--p,50%), #fff 0%);
}
<input class="prettyRange" type="range" name="capability" style="--p:80%">
<br>
<input class="prettyRange" type="range" name="capability" style="--p:100%">
<br>
<input class="prettyRange" type="range" name="capability" >

Using JS to change the value:

document.querySelector('.prettyRange').addEventListener('input',function(e) {
  e.target.style.setProperty('--p', e.target.value*10+'%');
})
.prettyRange {
  -webkit-appereance: none;
}

.prettyRange::-webkit-slider-runnable-track {
  background: -webkit-linear-gradient(right, #fff 0%, green var(--p, 50%), #fff 0%);
}
<input class="prettyRange" min='1' max='10' type="range" name="capability">
Jaipur answered 20/4, 2018 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.