-2

//style.sass

$common-color: #333;

body {
  font: 100% $font-stack;
  color: $common-color;
}

can I do the similar in css? because I am not using .sass for my project.

nano dev
  • 335
  • 4
  • 6

3 Answers3

1
:root {
    --maincolor: red;
    --mainfont: Arial;
}

body {
   color: var(--maincolor);
   font: var(--mainfont);
{
Kolja F
  • 31
  • 3
1

You can write it as below

:root {
  --common-color: #333;
}

body {
  font: 100% $font-stack;
  color: var(--common-color);
}

Here :root is the scope of the variable

More information can be found in the link - https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties

viki
  • 1,178
  • 1
  • 17
  • 22
1

In CSS3 you're able to use variables by declaring them with --name-of-variable and access them with var(--name-of-variable). Your example would look like:

:root {
  --common-color: #333;
  --font-stack: 'Roboto', sans-serif;
}

body {
  font: 100% var(--font-stack);
  color: var(--common-color);
}

See https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties for more information about scoping and fallbacks.

Edblocker
  • 430
  • 1
  • 3
  • 15