CSS em 与 px(舍入误差)
我一直在开发一个个人网站(所以,时间在这里不是问题),我制作了一个基本样式表,在其中处理所有字体大小、边框宽度、行高等。 我一直努力使用 ems,但是当我在其他浏览器(例如 Chromium)上测试该网站时,内容与我的“像素完美”网格不匹配。
所以,我的问题是,我应该使用 px 吗?我的意思是,我知道 ems 是“必经之路”,但现在大多数浏览器都实现了全页缩放(它们不只调整文本大小),并且在处理边框宽度和行高时,px 更舒服,因为我可以完全避免子像素舍入。
您对此有何看法? (顺便说一句,支持 IE 不是我的目标之一;我对此一点也不关心)
I've been working on a personal website (so, time is not an issue here) and I made a base stylesheet where I take care of all the font-sizes, border-widths, line-heights and the like.
I made an effort to use ems all the way, but when I tested the website on other browsers (eg. Chromium) the content didn't match my "pixel-perfect" grid.
So, my question here is, should I use px instead? I mean, I know ems are "the way to go" but nowadays most browsers implement fullpage zoom (they don't resize just the text) and when it comes to dealing with border-width and line-heights, px are more comfortable, because I can avoid subpixel rounding altogether.
What's your take on this? (btw, supporting IE is not one of my goals; i couldn't care less about it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用像素完美的网格,请使用像素值。
em
是一个相对值,仅当布局能够适应不同的内容大小时才有效。If you're working with a pixel-perfect grid, use pixel values.
em
is a relative value that works well only if the layout is able to adapt to different content sizes.我只会将
em
用于大小取决于字体大小的元素。边框(及其宽度)等元素通常不依赖于字体大小。
I would use
em
only for elements which size depends on font size.Elements like borders (and its width) usually does not depend on font size.
当使用
em
作为边距和填充时,我发现相对测量可以做一些有趣的事情。em
可以在嵌套元素中累积,您会发现自己将一些值向后推以与外部元素对齐。这看起来很好,直到您非常缓慢地缩小浏览器窗口并发现有时嵌套元素的值舍入为与外部父元素不同的像素。
为了解决这个问题,我在父级别使用了 rem 来设置字体大小,并发现这允许父级和子级使用相同的基本相对值。
When using
em
for margin and padding, I've found that the relative measurement can do some funny things.The
em
can accumulate in nested elements and you find yourself bumping some values back up to align them with outer elements.This looks fine until you shrink the browser window in very slowly and find that sometimes the values for the nested elements round to a pixel different to the outer parent.
To combat this, I've used
rem
at the parent level to set the font size and found that this allows parent and descendents to work from the same base relative value.