Calculate a percent with SCSS/SASS

2019-01-22 01:21发布

问题:

I want to set a width in percentage in scss via calculation, but it gives me errors..

Invalid CSS after "...-width: (4/12)%": expected expression (e.g. 1px, bold), was ";"

I want to do something like

$my_width: (4/12)%;

div{
width: $my_width;
}

how do I add the % sign in there?

Same question with px and em

回答1:

Have you tried the percentage function ?

$my_width: percentage(4/12);
div{
width: $my_width;
}


回答2:

Another way:

$my_width: 4/12*100%;

div{
width: $my_width; // 33.33333%
}

Sass will output the value in %.



标签: css sass