Skip to content

Instantly share code, notes, and snippets.

@halfninja
Last active October 20, 2018 20:09
Show Gist options
  • Save halfninja/5741836b2b04e314b4bf64284dd8247e to your computer and use it in GitHub Desktop.
Save halfninja/5741836b2b04e314b4bf64284dd8247e to your computer and use it in GitHub Desktop.
Progress bar that changes width AND color using a CSS variable
/**
* Progress bar that changes width AND color using a CSS variable
*/
div {
display: flex;
height: .8em;
margin: 1em 0;
background: hsl(220,10%,80%);
}
div::before {
content: "";
width: calc(var(--value) * 100%);
background: hsl(calc(var(--value) * 90), 50%, 50%);
}
<div class="progress" data-value=".02"></div>
<div class="progress" data-value=".1"></div>
<div class="progress" data-value=".3"></div>
<div class="progress" data-value=".5"></div>
<div class="progress" data-value=".7"></div>
<div class="progress" data-value=".9"></div>
<div class="progress" data-value="1"></div>
document.querySelectorAll('[data-value]').forEach(element => {
element.style.setProperty('--value', element.getAttribute('data-value'));
});
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"","page":"javascript"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment