最近我想知道当使用 rem 作为字体大小单位时哪个后备是最好的。像素似乎是合适的,但如果你想在特定媒体查询中更改全局字体大小,则需要重新定义每个基于 px 的字体大小。
这是一个例子:没有任何旧浏览器的回退,我们可以使用
/* Mobile Styles */
html { font-size: 1rem; }
h1 { font-size: 2rem; }
/* Specific desktop styles */
@media only screen and (min-width: 992px) {
html { font-size: 1.25rem; }
}
并且所有字体在桌面上都会放大 1.25 倍。到现在为止还挺好。
但是由于我们需要为 IE8 提供 rem 的替代方案,我们必须使用某种后备,例如:
/* Mobile Styles */
html { font-size: 16px; font-size: 1rem; }
h1 { font-size: 32px; font-size: 2rem; }
/* Specific desktop styles */
@media only screen and (min-width: 992px) {
html { font-size: 20px; font-size: 1.25rem; }
h1 { font-size: 40px; font-size: 2rem; }
}
缺点:这样做我们需要重新定义桌面媒体查询中的所有字体大小声明。另一种方法是使用 em 作为后备,但随后字体大小会复合。
很高兴听到您对此的建议。