简体   繁体   English

CSS中的字体大小 - %或em?

[英]Font size in CSS - % or em?

When setting the size of fonts in CSS, should I be using a percent value ( % ) or em ? 在CSS中设置字体大小时,我应该使用百分比值( % )还是em Can you explain the advantage? 你能解释一下这个优势吗?

There's a really good article on web typography on A List Apart . A List Apart关于网页排版的非常好的文章。

Their conclusion: 他们的结论:

Sizing text and line-height in ems, with a percentage specified on the body (and an optional caveat for Safari 2), was shown to provide accurate, resizable text across all browsers in common use today. 在ems中调整文本和行高,在主体上指定百分比(以及Safari 2的可选警告),显示可以在当今常用的所有浏览器中提供准确,可调整大小的文本。 This is a technique you can put in your kit bag and use as a best practice for sizing text in CSS that satisfies both designers and readers. 这是一种技术,您可以将其放入工具包中,并将其用作CSS中调整文本大小的最佳实践,以满足设计人员和读者的需求。

From http://archivist.incutio.com/viewlist/css-discuss/1408 来自http://archivist.incutio.com/viewlist/css-discuss/1408

%: Some browsers doesn't handle percent for font-size but interprets 150% as 150px. %:某些浏览器不处理font-size的百分比,但将150%解释为150px。 (Some NN4 versions, for instance.) IE also has problems with percent on nested elements. (例如,某些NN4版本。)IE在嵌套元素上的百分比也存在问题。 It seems IE uses percent relative to viewport instead of relative to parent element. 似乎IE使用相对于视口的百分比而不是相对于父元素。 Yet another problem (though correct according to the W3C specs), in Moz/Ns6, you can't use percent relative to elements with no specified height/width. 另一个问题(虽然根据W3C规范是正确的),在Moz / Ns6中,你不能使用相对于没有指定高度/宽度的元素的百分比。

em: Sometimes browsers use the wrong reference size, but of the relative units it's the one with least problems. em:有时候浏览器会使用错误的参考大小,但是相对单位却是问题最少的。 You might find it interpreted as px sometimes though. 您可能会发现它有时会被解释为px。

pt: Differs greatly between resolutions, and should not be used for display. pt:分辨率差异很大,不应该用于显示。 It's quite safe for print use though. 不过,这对于打印使用来说非常安全。

px: The only reliable absolute unit on screen. px:屏幕上唯一可靠的绝对单位。 It might be wrongly interpreted in print though, as one point usually consist of several pixels, and thus everything becomes ridiculously small. 它可能在印刷中被错误地解释,因为一个点通常由几个像素组成,因此一切都变得非常小。

Both adjust the font-size relative to what it was. 两者都相对于它的大小调整字体大小。 1.5em is the same as 150%. 1.5em与150%相同。 The only advantage seems to be readability, choose whichever you are most comfortable with. 唯一的优势似乎是可读性,选择最适合您的方式。

Given that (nearly?) all browsers now resize the page as a whole, rather than just the text, previous issues with px vs. % vs. em s in terms of accessible font resizing are rather moot. 鉴于(几乎?)所有浏览器现在整个页面调整大小,而不仅仅是文本,在可访问字体大小调整方面, px% vs em的先前问题相当没有实际意义。

So, the answer is that it probably doesn't matter. 所以,答案是它可能并不重要。 Use whatever works for you. 使用适合你的任何东西。

% is nice because it allows for relative resizing. %很好,因为它允许相对大小调整。

px is nice because it's fairly easy to manage expectations when using it. px很不错,因为使用它时管理期望相当容易。

em can be useful when also used for layout elements as it can allow for proportional sizing related to the text size. 当用于布局元素时, em也很有用,因为它可以允许与文本大小相关的比例大小。

The real difference comes apparent when you use it not for font-sizes. 当你使用它不是字体大小时,真正的区别显而易见。 Setting a padding of 1em is not the same as 100% . 设置1empadding100% em is always relative to the font-size. em总是相对于font-size。 But % might be relative to font-size, width, height and probably some other things I don't know about. %可能与字体大小,宽度,高度有关,可能还有其他一些我不知道的事情。

Regarding the difference between the css units % and em . 关于css单位%em之间的差异。

As far as I understand (at least theoretically/conceptually, but possibly not how these two units might be implemented in browsers) these two units are equivalent, ie if you multiply your em value with 100 and then replace em with % it should be the same thing ? 据我所知(至少在理论上/概念上,但可能不是这两个单元如何在浏览器中实现)这两个单位是等价的,即如果你将你的em值乘以100然后用%替换em它应该是一样 ?

If there actually is some real difference between em and % then can someone explain it (or provide a link to an explanation) ? 如果em和%之间确实存在一些真正的区别,那么有人可以解释它(或提供解释的链接)吗?

(I wanted to add this comment of mine where it would belong, ie indented just below the answer by "Liam, answered Sep 25 '08 at 11:21" since I also want to know why his answer was downvoted, but I could not figure out how to put my comment there and therefore had to write this "thread global" reply) (我想在我的评论中添加它的归属,即"Liam, answered Sep 25 '08 at 11:21"因为我也想知道为什么他的答案被低估了,但我不能弄清楚如何把我的评论放在那里,因此不得不写这个“线程全球”的答复)

As Galwegian mentions, px is the most reliable for web typography, as everything else you do on the page is mostly laid out in reference to a computer monitor. 正如Galwegian所提到的,px对于Web排版来说是最可靠的,因为您在页面上执行的其他操作大部分都是针对计算机显示器进行布局的。 The problem with absolute sizes is that some browsers (IE) won't scale pixel-value elements on a web-page, so when you try to zoom in/out, everything adjusts except for those elements. 绝对大小的问题是某些浏览器(IE)不会缩放网页上的像素值元素,因此当您尝试放大 /缩小时,除了这些元素之外,所有内容都会进行调整。

I do not know whether IE8 handles this properly, but all other browser vendors handle pixels just fine and it is still a minority case where a user needs to enlarge/diminish text (this text box on SO perhaps being the exception). 我不知道IE8是否正确处理这个问题,但所有其他浏览器供应商处理像素都很好,而且仍然是用户需要放大/缩小文本的少数情况(SO上的这个文本框可能是例外)。 If you want to get really dirty, you could always add a javascript function for making your text size larger and offer a "small"/"larger" button to the user. 如果你想变得非常脏,你总是可以添加一个javascript函数来增加文本大小,并为用户提供一个“小”/“更大”的按钮。

Yahoo User Interface library ( http://developer.yahoo.com/yui/ ) has a nice set of base css classes used to "reset" the browser specific settings so that the basis for displaying the site is same for all (supported) browsers. Yahoo用户界面库( http://developer.yahoo.com/yui/ )有一组很好的基本css类,用于“重置”浏览器特定的设置,以便显示站点的基础是相同的(支持)浏览器。

With YUI one is supposed to use percentages. 使用YUI,应该使用百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM