Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile non-root CSS custom property

Are there any tools to compile CSS custom properties declared at not :root rule? I want following code with custom properties

.dark {
  --bg-color: black;
  --fg-color: white;
}

.light {
  --bg-color: white;
  --fg-color: black;
}

.foo {
  background: var(--bg-color);
  display: block;
}

.bar {
  color: var(--fg-color);
  display: inline;
}

be compiled to their non-custom-prop equivalents like that

.light .foo, .light.foo {
  background: white;
}

.dark .foo, .dark.foo {
  background: black;
}

.light .bar, .light.bar {
  color: black;
}

.dark .bar, .dark.bar {
  color: white;
}

.foo {
  display: block;
}

.bar {
  display: inline;
}

The goal is to

  • switch color schemes by switching dark/light class on root DOM element
  • use valid css syntax (no sass less)
  • keep rules code compact
like image 368
exe-dealer Avatar asked Apr 17 '26 20:04

exe-dealer


1 Answers

It's actually not safe to do that. I can tell you because I tried so hard to make a safe transformation. But I failed.

https://github.com/postcss/postcss-custom-properties/issues/1

like image 56
MoOx Avatar answered Apr 19 '26 09:04

MoOx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!