Is it possible to change a variable used in CSS using jQuery? Simple example:
html {
--defaultColor: teal;
}
.box {
background: var(--defaultColor);
width: 100px;
height: 100px;
margin: 5px;
float: left;
}
.circle {
background: #eee;
border: 2px solid var(--defaultColor);
width: 100px;
height: 100px;
margin: 5px;
float: left;
border-radius: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="clickToChange" type="button" style="width: 100%;">Click to Change</button>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<br/>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
How can I change the variable color from teal to red for example? This one doesn't work.
$("#clickToChange").click(function(){
$(html).css("--defaultColor", "red");
});
.cssmethod gist.github.com/jcubic/9ef9fa2561de8430e953e2fe62011c20$(body).css('--css-var', 'var-value').html('This is body element').addClass('some-class').$(":root").css("--defaultColor", "red");. . . or you can Set multiple css variables:$(":root").css({"--myVar0":myVal0, "--myVar1":myVal1});, etc... much tidier than non-jQuery solutions IMHO. (source).