Reputation: 345
I would like to define a couple of linear gradients and apply them with only CSS rules: I tried this:
console.log(
window
.getComputedStyle(document.querySelector('body'))
.getPropertyValue('--btnPrimary')
);
:root{
--primary: #4169e1;
--secondary :#ef3c3a;
--grey: #585f74;
--g-light: #b6bbc8;
--p2s-3: #c44764;
--s2p-3: var(--p2s-1);
--p-btngrad-a: var(--primary);
--p-btngrad-b: var(--p2s-3);
--lgrad-angle: 75;
--lgrad-stop-a: 45;
--lgrad-stop-b: 99;
--btnPrimary: linear-gradient(
var(--lgrad-angle)deg,
var(--p-btngrad-a) var(--lgrad-stop-a)%,
var(--p-btngrad-b) var(--lgrad-stop-b)%
);
--btnSecondary: linear-gradient(
var(--lgrad-angle)deg,
var(--s-btngrad-a) var(--lgrad-stop-a)%,
var(--s-btngrad-b) var(--lgrad-stop-b)%
);
}
div.demo{
height:200px;
width:200px;
background:var(--btnPrimary);
}
div.demo:hover{
background:var(--btnSecondary);
}
<h1>css only linear gradient by parameters</h1>
<div class="demo">hover me</div>
But it doesn't work. The console shows why:
linear-gradient(75/**/deg, #4169e1 45/**/%, #c44764 99/**/% )
Why are the CSS variables followed by comment markup ("/**/")?
How can I fix this?
Upvotes: 5
Views: 113
Reputation: 882
You have not specified the unit (deg and %) for the variables; and it says here that:
Note that var() substitution takes place at the level of CSS tokens [css-syntax-3], not at a textual level; you can’t build up a single token where part of it is provided by a variable:
Give Dimension Directly in variable instead of concatenating it afterwards.
//console.log(window.getComputedStyle(document.querySelector('body')).getPropertyValue('--btnPrimary'));
:root{
--primary: #4169e1;
--secondary :#ef3c3a;
--grey: #585f74;
--g-light: #b6bbc8;
--p2s-3: #c44764;
--s2p-3: var(--p2s-1);
--p-btngrad-a: var(--primary);
--p-btngrad-b: var(--p2s-3);
--lgrad-angle: 75;
--lgrad-stop-a: 45;
--lgrad-stop-b: 99;
--lgrad-angle:75deg;
--lgrad-stop-a:45%;
--lgrad-stop-b:99%;
--btnPrimary:linear-gradient(var(--lgrad-angle), var(--p-btngrad-a) var(--lgrad-stop-a), var(--p-btngrad-b) var(--lgrad-stop-b));
--btnSecondary:linear-gradient(var(--lgrad-angle), var(--s-btngrad-a) var(--lgrad-stop-a), var(--s-btngrad-b) var(--lgrad-stop-b) );
}
div{
height:200px;
width:200px;
background:var(--btnPrimary);
}
div:hover{
background:var(--btnSecondary);
}
<h1> css only linear gradient by parameters</h1>
<div>hover me</div>
Upvotes: 2
Reputation: 106038
you need to multiplicate your var with the unit you want to use.(chrome seems not to need it)
//console.log(
// window
// .getComputedStyle(document.querySelector('body'))
// .getPropertyValue('--btnPrimary')
//);
:root{
--primary: #4169e1;
--secondary :#ef3c3a;
--grey: #585f74;
--g-light: #b6bbc8;
--p2s-3: #c44764;
--s2p-3: var(--p2s-1);
--p-btngrad-a: var(--primary);
--p-btngrad-b: var(--p2s-3);
--lgrad-angle: 75;
--lgrad-stop-a: 45;
--lgrad-stop-b: 99;
--btnPrimary: linear-gradient(
calc(var(--lgrad-angle) * 1deg),
var(--p-btngrad-a) calc(var(--lgrad-stop-a) * 1%),
var(--p-btngrad-b) calc(var(--lgrad-stop-b) * 1%)
);
--btnSecondary: linear-gradient(
calc(var(--lgrad-angle) * 1deg),
var(--s-btngrad-a) calc(var(--lgrad-stop-a) * 1%),
var(--s-btngrad-b) calc(var(--lgrad-stop-b) * 1%)
);
}
div{
height:200px;
width:200px;
background:var(--btnPrimary);
}
div:hover{
background:var(--btnSecondary);
}
<h1> css only linear gradient by parameters</h1>
<div>hover me </div>
<code> var(--s-btngrad-a) & var(--s-btngrad-b)</code> are missing
Upvotes: 3