将rem转换为px而无需重排

时间:2017-07-09 21:16:34

标签: javascript html css

我在rem中有一些常量,但rem本身不是常数(取决于媒体查询和vmin)。对于某些人来说,我需要px中的这个值。

当然,我可以创建一个临时div,将其大小设置为remgetComputedStyle中的该值,以获得px中的值。但是我想通过读取计算样式而不引起重排来做到这一点。

2 个答案:

答案 0 :(得分:1)

rem单位等于根元素上'font-size'的计算值。 (https://www.w3.org/TR/2013/CR-css3-values-20130730/#font-relative-lengths

你可以通过1 rem =

轻松搞定
parseInt(getComputedStyle(document.documentElement).fontSize))

答案 1 :(得分:0)

根据定义,rem是

Equal to the computed value of font-size on the root element.
When specified on the font-size property of the root element,
the rem units refer to the property’s initial value.

所以你应该能够通过以下方式获得它的大小:

window.getComputedStyle(document.documentElement)['font-size']
相关问题