Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate a number and "%" in sass

Tags:

css

sass

I would like to do the following using SASS:

width: #{$percent}%;

Where $percent is a variable containing a number. If $percent is equal to 50, the precompiled CSS would be:

width: 50%;

What syntax should I use?

like image 951
Gabriel Caron L'Écuyer Avatar asked Jul 13 '13 04:07

Gabriel Caron L'Écuyer


People also ask

How do I concatenate in sass?

String Operators The single Sass string operator is concatenation. Strings can be concatenated (linked together) using the + operator. If you mix quoted and unquoted strings when concatenating, the result will be whichever is on the left side of the operator.

What is interpolation in sass?

Interpolation allows us to interpolate sass expressions into a simple SASS or CSS code. Means, you can define ( some part or the whole ) selector name, property name, CSS at-rules, quoted or unquoted strings etc, as a variable. Interpolation is a new principle and it is widely used in SASS.


2 Answers

Multiply by 1%:

$percent: 50

.foo
  width: $percent * 1%

Result:

.foo {
  width: 50%;
}
like image 88
Andrey Mikhaylov - lolmaus Avatar answered Oct 11 '22 05:10

Andrey Mikhaylov - lolmaus


If anyone is looking answer for SCSS below code will work perfectly, just use css calc.

$percent: 50

.foo{
   width: calc(#{$percent} * 1%)
 }
like image 27
Amit Rajbhandari Avatar answered Oct 11 '22 03:10

Amit Rajbhandari