CSS 中的字体大小 - % 还是 em?

发布于 2024-07-05 18:19:28 字数 78 浏览 8 评论 0原文

在 CSS 中设置字体大小时,应该使用百分比值 (%) 还是 em? 能解释一下优点吗?

When setting the size of fonts in CSS, should I be using a percent value (%) or em? Can you explain the advantage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心的憧憬 2024-07-12 18:19:28

两者都根据实际情况调整字体大小。 1.5em 与 150% 相同。 唯一的优点似乎是可读性,选择你最舒服的。

Both adjust the font-size relative to what it was. 1.5em is the same as 150%. The only advantage seems to be readability, choose whichever you are most comfortable with.

回忆凄美了谁 2024-07-12 18:19:28

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

%:有些浏览器不处理
字体大小的百分比但解释
150% 为 150 像素。 (某些 NN4 版本,
例如。)IE也有问题
嵌套元素上的百分比。 它
似乎 IE 使用相对于的百分比
视口而不是相对于
父元素。 还有一个问题
(虽然根据 W3C 是正确的
规格),在 Moz/Ns6 中,你不能使用
相对于没有元素的百分比
指定的高度/宽度。

em:有时浏览器使用错误
参考尺寸,但相对
单位是最少的单位
问题。 你可能会找到它
但有时会解释为 px。

pt:之间差异很大
决议,并且不应使用
用于显示。 对于
不过打印使用。

px:唯一可靠的绝对单位
屏幕。 可能是错误的
尽管在印刷品中解释为一个
点通常由几个点组成
像素,因此一切都变成了
小得可笑。

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

%: Some browsers doesn't handle
percent for font-size but interprets
150% as 150px. (Some NN4 versions,
for instance.) IE also has problems
with percent on nested elements. It
seems IE uses percent relative to
viewport instead of relative to
parent element. 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.

em: Sometimes browsers use the wrong
reference size, but of the relative
units it's the one with least
problems. You might find it
interpreted as px sometimes though.

pt: Differs greatly between
resolutions, and should not be used
for display. It's quite safe for
print use though.

px: The only reliable absolute unit on
screen. It might be wrongly
interpreted in print though, as one
point usually consist of several
pixels, and thus everything becomes
ridiculously small.

猫七 2024-07-12 18:19:28

当您不将其用于字体大小时,真正的区别就会显现出来。 将 padding 设置为 1em100% 不同。 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%. em is always relative to the font-size. But % might be relative to font-size, width, height and probably some other things I don't know about.

温柔嚣张 2024-07-12 18:19:28

鉴于(几乎?)所有浏览器现在都会调整整个页面的大小,而不仅仅是文本的大小,之前的问题是 px%em< /code> 在可访问的字体大小调整方面是相当没有意义的。

所以,答案是,这可能并不重要。 使用任何对你有用的东西。

% 很好,因为它允许相对调整大小。

px 很好,因为使用它时管理期望相当容易。

em 在也用于布局元素时非常有用,因为它可以允许与文本大小相关的比例大小调整。

Given that (nearly?) all browsers now resize the page as a whole, rather than just the text, previous issues with px vs. % vs. ems in terms of accessible font resizing are rather moot.

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.

em can be useful when also used for layout elements as it can allow for proportional sizing related to the text size.

她说她爱他 2024-07-12 18:19:28

正如 Galwegian 提到的,px 对于网页排版来说是最可靠的,因为您在页面上所做的所有其他操作大部分都是参考计算机显示器进行布局的。 绝对大小的问题是某些浏览器 (IE) 不会缩放网页上的像素值元素,因此当您尝试放大时,除了这些元素之外,一切都会调整。

我不知道 IE8 是否可以正确处理这个问题,但所有其他浏览器供应商都可以很好地处理像素,并且用户需要放大/缩小文本的情况仍然是少数(SO 上的这个文本框可能是例外)。 如果你真的想变得更脏,你总是可以添加一个 javascript 函数来增大文本大小,并向用户提供一个“小”/“更大”按钮。

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. 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.

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). 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.

窝囊感情。 2024-07-12 18:19:28

关于css单位%em之间的区别。

据我了解(至少在理论上/概念上,但可能不是这两个单位在浏览器中的实现方式),这两个单位是等效的,即如果您将 em 值乘以 100 然后用 % 替换 em 应该是一样的吗?

如果 em 和 % 之间确实存在一些真正的区别,那么有人可以解释它(或提供解释的链接)吗?

(我想将我的评论添加到它所属的位置,即在“Liam,于 2008 年 9 月 25 日 11:21 回答”的答案下方缩进,因为我也想知道为什么他的答案被否决了,但我不知道如何将我的评论放在那里,因此不得不写这个“线程全局”回复)

Regarding the difference between the css units % and 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, i.e. if you multiply your em value with 100 and then replace em with % it should be the same thing ?

If there actually is some real difference between em and % then can someone explain it (or provide a link to an explanation) ?

(I wanted to add this comment of mine where it would belong, i.e. 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)

旧伤还要旧人安 2024-07-12 18:19:28

Yahoo 用户界面库 (http://developer.yahoo.com/yui/) 有一组很好的基本 css 类,用于“重置”浏览器特定设置,以便显示所有(支持的)浏览器的站点都是相同的。

对于 YUI,应该使用百分比。

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.

With YUI one is supposed to use percentages.

帅的被狗咬 2024-07-12 18:19:28

A List Apart 上有一篇关于网页排版的非常好的文章。

他们的结论是:

调整文本大小和行高(以 em 为单位),
与指定的百分比
正文(以及可选的警告
Safari 2),被证明可以提供
准确、可调整大小的文本
当今常用的浏览器。 这是
可以放入工具包中的技术
装袋并作为最佳实践使用
在 CSS 中调整文本大小以满足两者
设计师和读者。

There's a really good article on web typography on 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文