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

View all comments

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