简体   繁体   English

使用ems时奇怪但明确具体的字体大小

[英]Strange, but clearly specific font-sizes when using ems

I have noticed that websites that use ems for fonts generally have strange, but clearly specific numbers for example: in a popular reset css - normalize.css I found that has a h3 set to 1.17em, h4 to 1.33em and h5 to 1.67em, why is this? 我注意到使用ems进行字体的网站通常有奇怪但明确具体的数字,例如:在流行的重置css - normalize.css中,我发现h3设置为1.17em,h4设置为1.33em,h5设置为1.67em , 为什么是这样? Is there a calculator or something that I should be using to calculate every font-size? 有计算器或我应该用来计算每个字体大小的东西吗?

I have been adjusting the font-size in ems for headings, p, captions, etc, but with no calculator or anything, I simply make it bigger or smaller until I think it looks good. 我一直在调整标题,p,标题等的字体大小,但没有计算器或任何东西,我只是让它变大或变小,直到我觉得它看起来不错。 In other words, my website has font-sizes 0.7em,0.9em, 1em, 1.1em, etc, even a 4em (i like big h1). 换句话说,我的网站有字体大小0.7em,0.9em,1em,1.1em等,甚至是4em(我喜欢大h1)。

Is this bad? 这不好吗? Have I been ignoring an important fact when using ems for font sizes? 使用ems进行字体大小时,我是否忽略了一个重要的事实?

As you know, em units in font sizes allow the sizes of text elements to retain their proportions relative to other text elements. 如您所知,字体大小的em单位允许文本元素的大小保持其相对于其他文本元素的比例。

There is a method for scalable text in which a "baseline" font size is set on the HTML or BODY element. 有一种可伸缩文本的方法,其中在HTML或BODY元素上设置“基线”字体大小。 I generally set this "baseline" font size to 10px because it's a nice round number and makes ems throughout the site easier to calculate. 我通常将这个“基线”字体大小设置为10px,因为它是一个很好的整数,并使整个站点的ems更容易计算。 Say a title is 15px in my design. 在我的设计中说标题是15px。 I would set that title to 1.5em (1.5 x 10px = 15px). 我将该标题设置为1.5em(1.5 x 10px = 15px)。 You might notice seemingly odd values due to such calculations. 由于这样的计算,您可能会注意到看似奇怪的值。

"Fractional" values might also look unusual. “分数”值也可能看起来不寻常。 For example, "1.33em" is essentially "1 ⅓ em" and "1.67em" is essentially "1 ⅔ em". 例如,“1.33em”基本上是“1⅓em”而“1.67em”基本上是“1⅔em”。 These accomplish font sizes that are one third and two thirds larger than the baseline, respectively. 它们分别实现了比基线大三分之一和三分之二的字体大小。

If you are designing on-the-fly (without a design comp), you're probably okay just adjusting until it looks right. 如果你是在动态设计(没有设计补偿),你可能只是调整直到看起来正确。 It's always a good idea to test things by changing your browser's zoom and/or font-size defaults to see how your pages react. 通过更改浏览器的缩放和/或字体大小默认值来测试事物总是一个好主意,以查看页面的反应。

Showdev does a good job of telling what ems do, however to answer your question about why you see things set this way. Showdev很好地说明了ems做了什么,但回答了你为何看到这样设置的问题。

This article does a good job explaining how to achieve a consistent sizing of text across all browsers. 本文很好地解释了如何在所有浏览器中实现一致的文本大小调整。 This jist is that 14px in IE7 is not the same as 14px in Firefox. 这个问题是IE7中的14px与Firefox中的14px不同。 Ems give a more consistent result. Ems提供更一致的结果。 http://alistapart.com/article/howtosizetextincss http://alistapart.com/article/howtosizetextincss

Also, just to give an expanded explanation. 另外,只是给出一个扩展的解释。 Ems are relative to the font size of the parent element. Ems相对于父元素的字体大小。 So if I have: 所以,如果我有:

body {
  font-size: 14px;
}
p {
  font-size: 1em;  /* 14px */
}
h2 {
  font-size: 1.5em; /* 21px */
}
#myDiv {
  font-size: 16px;
}
#myDiv h1 {
  font-size: 2em; /* 32px */
} 

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

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