81

In preprocessors, like SASS, you can use negative values like:

$margin-md: 10px;

.class {
  margin-bottom: -$margin-md;
}

How do I do this using custom properties?

// This doesn't work
.class {
  margin-bottom: -var(--margin-md);
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
bholtbholt
  • 11,281
  • 6
  • 22
  • 32

1 Answers1

227

As of this posting, March 2018, the only way to use negative custom properties is by multiplying it by -1 with the calc function.

// Vanilla CSS
.class {
  margin-bottom: calc(var(--margin-md) * -1);
}
idmean
  • 14,540
  • 9
  • 54
  • 83
bholtbholt
  • 11,281
  • 6
  • 22
  • 32
  • 4
    It's not the only way... you can subtract it from 0px aswell, i.e. margin-bottom: calc( 0px - var(--myvalue) ); While it's fundimentally identical in and of itself, since units like vh and vw don't allow multiplication or devision by other units, so in some rare cases the existence of a multiplication in the same may cause problems, although I'm only guessing, but why take the chance, or have an alternative on the off chance it does? – GibsonFX Jul 03 '23 at 13:06