Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stylus. Negative variables

Tags:

stylus

How can I use a negative variables in Stylus ?

I write mixin for a sprite:

sprite-medium(col,row)
  width = 40px
  height = 40px
  width: width
  height: height
  background: url('../img/medium-sprite.png') no-repeat
  background-position: -col*width -row*height

And I have an error. Of course I can write negative values in call of mixins, but it's not a perfect decision. Anyone can help? Thanks.

like image 683
Denys Kurbatov Avatar asked Apr 09 '26 16:04

Denys Kurbatov


1 Answers

Stylus is treating the - before col and row as part of the name - they need to be separated for that to work like -(col * width), however you also need to avoid subtracting the two values that you want for background position. Here's a solution with a working background calculation and a bit simplified through the use of property lookups:

sprite-medium(col, row)
  width: 40px
  height: 40px
  background: url('../img/medium-sprite.png') no-repeat
  background-position: -(col * @width) -1 * (row * @height)

Hope this helps.

like image 72
Alexander Futekov Avatar answered Apr 17 '26 14:04

Alexander Futekov



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!