Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using css custom properties variables not working

Tags:

css

stylesheet

style sheet have very large amounts of CSS, often with a lot of repeated values. i read somewhere about variable in CSS . below my code is . but it is not working

element {
      --main-bg-color: brown;
    }

and i am using the variable here but it is not working

body {
  background-color: var --main-bg-color;
}
like image 728
ravins Avatar asked Feb 05 '18 02:02

ravins


2 Answers

You did everything right, just keep the variables in (put variable here)

element {
  --main-bg-color: brown;
}
body {
  background-color: var(--main-bg-color);
}
like image 143
Hasanuzzaman Sattar Avatar answered Oct 16 '22 12:10

Hasanuzzaman Sattar


var() notation works like a method

var(<custom-property-name>)

might consider putting your variables in a :root selector...

:root {
  --main-bg-color: brown;
}
/* The rest of the CSS file */
body {
  background-color: var(--main-bg-color);
}

:root is similar to global scope, but the element itself (ie body { --myvar: ... }) or ancestor elements (ie html { --myvar: ... }) can also be used to define variables

like image 7
MikeM Avatar answered Oct 16 '22 11:10

MikeM