Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negate a numerical variable and add 'px' to it in LessCSS

Tags:

less

I would like to create a function which does the following:

.sprite-size (@width,@height,@x,@y) {
  width:~'@{width}px';
  height:~'@{height}px';
  background: @sprites no-repeat  -~'@{x}px' -~'@{y}px';
}

I would like to pass a positive value, in @x and @y and then negate them in the output. The above LESS function outputs the following for the given example:

//LESS
.header-language-selection {
  .sprite-size(44,21,312,0);
}

//Outputs CSS
.header-language-selection {
  width: 44px;
  height: 21px;
  background: url('/Content/images/sprites.png') no-repeat - 312px - 0px;
}

As you can see the output result includes a space between the - and the px. Is there any way where one can remove this and achieve what I want?

I want the output of that line to be: background: url('/Content/images/sprites.png') no-repeat -312px -0px;

like image 656
Mark Cassar Avatar asked Jan 17 '13 17:01

Mark Cassar


2 Answers

Just multiply by 1 in the sign and units you want. So:

.sprite-size(@width, @height, @x, @y) {
  width: @width*1px;
  t: @height*1px;
  background: @sprites no-repeat  @x*-1px  @y*-1px;
}
like image 153
ScottS Avatar answered Nov 19 '22 14:11

ScottS


You can also try this:

.sprite-size (@width,@height,@x,@y) {
  width: ~"@{width}px";
  height: ~"@{height}px";
  background: @sprites no-repeat  @x*(-1px)  @y*(-1px);
}
like image 38
VyseExhale Avatar answered Nov 19 '22 13:11

VyseExhale