r/programming • • Jul 18 '21

5 Levels of Using CSS Variables

https://medium.com/techtofreedom/5-levels-of-using-css-variables-a4b01538279a
0 Upvotes

3 comments sorted by

13

u/[deleted] Jul 18 '21

I feel like (4) misses the point of cascading style sheets:

:root {
  --color: green;
  --small-screen-color: red;
}
div {
  color: var(--color);
}
@media screen and (max-width: 768px) {
  div {
    color: var(--small-screen-color);
  }
}

Instead of redefining the rules, you should redefine the variable:

:root {
  --color: green;
}
div {
  color: var(--color);
}
@media screen and (max-width: 768px) {
  :root {
    --color: red
  }
}

3

u/winkerback Jul 18 '21

I have never thought of doing it this way

1

u/c-smile Jul 19 '21

In Sciter you can do

:root {
  var(color): green;
}
div {
  color: var(color);
}
@media screen and (max-width: 768px) {
   :root {
     var(color): red
   }
}

so that weird -- are not needed.