Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use the negative value of a variable? #64

Open
axelson opened this issue Aug 20, 2016 · 9 comments
Open

Is it possible to use the negative value of a variable? #64

axelson opened this issue Aug 20, 2016 · 9 comments

Comments

@axelson
Copy link

axelson commented Aug 20, 2016

So if I have:

@value button-size: 55px;

Can I do something like:

position: absolute;
left: -button-size;
@joshwnj
Copy link
Member

joshwnj commented Aug 21, 2016

I haven't tried this, but you might be able to use postcss-calc and do:

left: calc(button-size * -1);

@joshwnj
Copy link
Member

joshwnj commented Aug 26, 2016

Edited my above comment to remove the incorrect part :)

@axelson just did a quick try using calc as above, and it works. The only catch I found is you need to make sure the postcss-calc plugin runs after CSS Modules.

@axelson
Copy link
Author

axelson commented Sep 3, 2016

Thanks, although I would prefer a solution that doesn't require adding in another postcss plugin. For now I'm just using scss variables when I need to negate them.

@apacsa
Copy link

apacsa commented Apr 10, 2017

You can achieve that with CSS variables:

:root {
  --button-size: 55px;
}

.someclass {
  position: absolute;
  left: calc(-1 * var(--button-size));
}

@wilmer2000
Copy link

You can achieve that with CSS variables:

:root {
  --button-size: 55px;
}

.someclass {
  position: absolute;
  left: calc(-1 * var(--button-size));
}

Thank you so much!!!

@vmaksym
Copy link

vmaksym commented Mar 2, 2020

left: calc(- var(--button-size));

@y0nd0
Copy link

y0nd0 commented Jul 22, 2020

Weird, this does not work.

does not work

--dash: 500;
stroke-dashoffset: calc(-var(--dash));
/* or */
stroke-dashoffset: calc(var(--dash) * -1);

works

stroke-dashoffset: -500;

also works (positive value / wrong direction)

stroke-dashoffset: var(--dash);

What's wrong?

Update: Ok I think it's because this value cannot be animated. (btw. it's in a keyframe animation) My solution is to create two variables. Positive and the negative one.

@brenndap
Copy link

brenndap commented Nov 9, 2020

just leaving something that can help anyone

transform: translateX(calc(-1 * calc(var(--sidebar-width) - var(--sidebar-collapsed-width))));

this works for me :)

@CreativeDive
Copy link

@apacsa really nice trick ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants