css 将px单位转换为rem以及为什么?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了css 将px单位转换为rem以及为什么?相关的知识,希望对你有一定的参考价值。

We need one simple setting to convert pixles to relative rem units. 

We need any easy way to change the measurements when for example we need to change the font size in a mobile window.
Instead of changing a hundred different elements and tweaking our code by adding more line and giving us a headache we just use
this technique wiith global font size.

(this example all code is in one main.css file)


STEP 1
--Our root font size is 16px and that's the absolute default.
So in order to give us the abillity to change the unit's and give the user the ability to zoom in our page or even change
the default font size we se the font-size to 100%;

STEP 2.
-- Now that we set to 100% if we divide by 16 equals to 0.625;

STEP 3.

---Go to  your css file and set your default font size to 62.5%;
  
html{
  font-size:62.5%; //equal to 1rem. we now divide all our calculations by 10
}

以上是关于css 将px单位转换为rem以及为什么?的主要内容,如果未能解决你的问题,请参考以下文章

rem与px的转换

CSS3中的rem、em与px间的换算关系

css常用单位总结: px / em / rem / vw / vh / vmax / vmin

px-em-rem单位转换

用nodejs把目录下所有用px做单位的css文件转化为用rem做单位的css文件

px与rem关系及转换