Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using negative CSS Custom Properties [duplicate]

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); } 
like image 484
bholtbholt Avatar asked Mar 24 '18 20:03

bholtbholt


People also ask

Are negative margins bad CSS?

The short answer is no, as long as you are only using negative CSS margins to bring elements closer together than their box model properties would allow.

Can you use CSS custom properties in media queries?

Just to clarify for people finding this through Google: you can use CSS custom properties inside the scope of a media query, you just cannot use them in a media query declaration.

How will you declare a custom property in CSS?

The @property “at-rule” in CSS allows you to declare the type of a custom property, as well its as initial value and whether it inherits or not.

What do CSS custom properties variables mean?

Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document.


1 Answers

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); } 

If you're using a preprocessor with custom properties, you'll need to escape the custom property within the calc function.

// SASS .class {   margin-bottom: calc(#{var(--margin-md)} * -1); } 
like image 179
bholtbholt Avatar answered Sep 24 '22 07:09

bholtbholt