javascript - 将 rem 转换为 px 而不回流

标签 javascript html css

我在 rem 中有一些常量,但 rem 本身不是常量(取决于媒体查询和 vmin)。出于某种原因,我需要 px 中的这个值。

当然,我可以创建一个临时 div,将其大小设置为 rem 中的值,并使用 getComputedStyle 获取 px 中的值.但我想通过读取计算样式来做到这一点而不会导致回流。

最佳答案

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

你可以很容易地用 1 rem =

parseInt(getComputedStyle(document.documentElement).fontSize))

关于javascript - 将 rem 转换为 px 而不回流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45001097/

相关文章:

html - 悬停不适用于子 div

javascript - 如何从单独文件中的另一个 View 调用主干 View 函数

javascript - 背景图片未覆盖整个网页

javascript - TypeError : ConnectionService. isOnline 不是函数

html - 页脚栏不起作用

javascript - 如何使用 JS 函数在 Polymer 3 中设置 CSS 样式

javascript - 获取复选框 ID 列表

javascript - "if (location.href != example.com/page1 || example.com/page2)"更改 CSS 元素

html - 将 HTML 内容渲染到 Google 电子表格

html - 对齐行底部的元素