CSS 使用 SCSS/SASS 计算百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13359419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:46:06  来源:igfitidea点击:

Calculate a percent with SCSS/SASS

csssass

提问by Nick Ginanto

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

我想通过计算在 scss 中设置一个百分比宽度,但它给了我错误..

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

"...-width: (4/12)%" 之后的 CSS 无效:预期的表达式(例如 1px,粗体),是 ";"

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

与 px 和 em 相同的问题

回答by Tomas

Have you tried the percentagefunction ?

你试过百分比功能吗?

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

回答by rlejnieks

Another way:

其它的办法:

$my_width: 4/12*100%;

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

Sass will output the value in %.

Sass 将输出以 % 为单位的值。

回答by Enrique Cuchetti

I was able to make it work this way:

我能够让它以这种方式工作:

div{
   width: (4/12)* 1%;
   }

This way you don't need to use any special function.

这样您就不需要使用任何特殊功能。