Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS: Subtract from variable

Tags:

css

less

Using LESS, how can I subtract values with "px" at the end of the variable. I have the following variable:

@bpMobile: 600px 

What I want to do is subtract this by 1px

@media only screen and (max-width: @bpMobile - 1px ) { } 

How can I achieve this with LESS?

like image 233
cusejuice Avatar asked Jul 02 '13 19:07

cusejuice


People also ask

What is the process of subtracting the mean of each variable from its variable called?

What is the process of subtracting the mean of each variable from its variable called ? Mean Normalization — Correct. For different parameters of the hypothesis function we get the same hypothesis function.

Can you add coefficients with different variables?

If no number appears before the variable, then you can assume that the number is 1. So a = 1a and x = 1x. You add terms that have the same variables because they represent the same amounts. You don't try to add the terms with different variables.


2 Answers

Sorry for answering this late, but I had this very problem and it seems LESS is picky about the spacing. You also need () around your calculation.

This will not work:

@media screen and (max-width: (@widthSmall-2)) { } 

However, this will (notice the space between the variable and the digit):

@media screen and (max-width: (@widthSmall - 2)) { } 
like image 92
Coreus Avatar answered Sep 22 '22 15:09

Coreus


You can always use the calc function for this.

Syntax: calc(expression)

Eg:

abc {   width:calc(100%-20px) } 

Here are the list of browser that supports this function

EDIT 1:

you can use it in the following two ways:

LESS Input:

@bpMobile: 600px; max-width: calc(~'@{bpMobile} - 1px'); 

CSS Output:

max-width: calc(600px - 1px); 

2nd Way: Less Input:

@bpMobile: 600px; max-width: calc(@bpMobile - 1px); 

CSS Output :

max-width: calc(599px); 

With the first option,the calc arguments are escaped in order to prevent them from being evaluated on compilation.The values will remain totally static until it's evaluated by the client.

With the second option, the calc value will be evaluated on compilation. And it would be the same as

max-width: @bpMobile - 1px;  

which will result in

max-width: 599px; 
like image 39
heretolearn Avatar answered Sep 20 '22 15:09

heretolearn