Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my CSS variables add comment markup?

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?

like image 928
Roland Gautier Avatar asked Jan 08 '20 09:01

Roland Gautier


People also ask

How do you set a variable value in CSS?

The var() function is used to insert the value of a CSS variable. CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries.

Are CSS variables case sensitive?

CSS variables can be used in HTML's style attribute. You can choose to set the value of your variables inline, and they'll still work as expected. CSS variables are case-sensitive.

What do CSS custom properties variables mean?

Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document.

Should you use CSS variables?

CSS variables, more accurately known as CSS custom properties, are landing in Chrome 49. They can be useful for reducing repetition in CSS, and also for powerful runtime effects like theme switching and potentially extending/polyfilling future CSS features.


Video Answer


2 Answers

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 
like image 191
G-Cyrillus Avatar answered Sep 28 '22 05:09

G-Cyrillus


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>
like image 38
CaffeinatedCod3r Avatar answered Sep 28 '22 06:09

CaffeinatedCod3r