CSS 如何计算类型的 REM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11352783/
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
How to calculate REM for type?
提问by alyus
How do I convert PX into REM for my type? I've read Jonathan Snook's article about using REM, but he was using font-size:62.5%, which defaults to your font size to 10px (for easier calculating). How would I convert my PX units into REM if I was to use 100%, 75% and so forth?
我如何将 PX 转换为我的类型的 REM?我读过 Jonathan Snook 关于使用 REM 的文章,但他使用的是 font-size:62.5%,默认字体大小为 10px(为了更容易计算)。如果我要使用 100%、75% 等等,我如何将我的 PX 单位转换为 REM?
回答by nEx.Software
Target Size / Base Size = Value
Target Size / Base Size = Value
Since we can assume that browsers use 16px as default (after all, that's what Jonathan Snook did to assume that 62.5% is 10px), then your Base is 16. If you want 32px, then 32 / 16 = 2
, want 40px then 40 / 16 = 2.5
, want 24 then 24 / 16 = 1.5
.
因为我们可以假设浏览器使用 16px 作为默认值(毕竟,Jonathan Snook 就是这样做的,假设 62.5% 是 10px),那么你的 Base 是 16。如果你想要 32px,那么32 / 16 = 2
,然后想要 40px,然后40 / 16 = 2.5
想要 24 24 / 16 = 1.5
。
The same goes for 75%... Determine what 75% is (it's 12) and perform the same calculation. If you want 32px, then 32 / 12 = 2.666
, want 40px then 40 / 12 = 3.333
, want 24 then 24 / 12 = 2
.
75% 也是如此……确定 75% 是什么(它是 12)并执行相同的计算。如果你想要 32px,那么32 / 12 = 2.666
,想要 40px,然后40 / 12 = 3.333
想要 24 24 / 12 = 2
。
回答by Yilmaz
if u just add this code to your style file
如果您只是将此代码添加到您的样式文件中
html {
font-size: 62.5%;
}
it will convert 1 rem=10px. Now you will be working 10px base. So
它将转换 1 rem=10px。现在您将使用 10px 基础。所以
body {
font-size: 1.6 rem;
}
u will have font size 1.6*10=16 px.
你的字体大小为 1.6*10=16 px。