7

My stylesheets have large amounts of styles declared, often with a lot of repeated values. I read about variables in CSS to solve that problem.

I tried to use them, but it is not working:

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

body {
  background-color: var --main-bg-color;
}

What am I doing wrong?

1
  • For beginners, don't use single quote in variable value. //Doesn't work --main-bg-color: 'brown'; //works --main-bg-color: brown; Commented Mar 3, 2023 at 7:30

6 Answers 6

16

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

element {
  --main-bg-color: brown;
}
body {
  background-color: var(--main-bg-color);
}
Sign up to request clarification or add additional context in comments.

2 Comments

this doesnt work on chromium-edge browser 06/2021
It doesn't also work on Firefox, Chrome, Opera and Microsoft Edge browsers. I had the same problem the questioner had and applied your suggestion and it didn't work on any browser so, i simply inserted my color directly instead of doing this and it worked. 9/2022
7

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

Comments

0

Refer to MDN reference page. A brief, to use custom variables you need to place them inside :root selector:

:root {
  --main-bg-color: brown
}

In order to use it in another selector yo use var():

body {
  background-color: var(--main-bg-color)
}

1 Comment

no, you don't. You can declare them at whatever CSSOM level you intent to use them. No need to go with :root unless you intend it to be a "global" variable
0

You need to add var(--my-variable) when using the variables.

But that's not something you should use CSS custom properties (variables) for.

Bear in mind some browser can't understand CSS variables, most noticeably IE. So using any pre-processor instead will be better for compatibility, as they are compiled to regular CSS values. Either SASS, LESS, POSTCSS... whatever floats your boat.

CSS custom properties are much more powerful than pre-processor ones, as they can be changed at runtime with javascript and be used in all sorts of awesome ways, but when you're using them as regular variables, pre-processor variables are always better for compatibility.

1 Comment

As of June 2021, global compatibility is just over 95%.
0

If you want to declare them globally, I would recommend to use it in: * { --var : #colorName; }. This has actually helped me in Angular application.

Comments

-1

For me, the problem was that @charset "UTF-8"; was not the very first characters in the css file, and this messed up the :root{--my-variable: large }.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.