简体   繁体   English

标题元素中文本周围的空格

[英]Space around text in header elements

I have been inspecting a header element using firebug, and have noticed that the header container does not hug its contained text, even though I have no padding set. 我一直在使用firebug检查标题元素,并且注意到即使我没有设置填充,标题容器也不会拥抱其包含的文本。

<h2>test</h2>

http://jsfiddle.net/BTwk6/ http://jsfiddle.net/BTwk6/

The top and bottom spacing is greater than that on the left and right, the result of this is that my h2 does not sit squarely in its containing box. 顶部和底部的间距大于左侧和右侧的间距,其结果是我的h2不能正好坐在其容纳盒中。

Is this normal? 这正常吗? Is there anyway to have the h2 box hug its text? 无论如何,是否有h2框拥抱其文本?

Thanks 谢谢

Try adjusting the line-height property as well, by default different fonts have different spaces above and below them. 也尝试调整line-height属性,默认情况下,不同的字体在其上方和下方具有不同的空格。 You may not be able to entirely fix this. 您可能无法完全解决此问题。 For example, the word 例如,这个词

Tempura

Is a whole lot taller than the word 比这个词高很多

mum

But the word 'mum' will still have to have the same height above and below the line as 'Tempura'. 但是,“妈妈”一词在线条上方和下方仍必须具有与“天妇罗”相同的高度。

Yes, it is normal, and things are supposed to be that way. 是的,这很正常,事情应该是那样的。 If you eg set a border on a heading element, you will see that characters in the heading text will normally not touch the border. 例如,如果您在标题元素上设置边框,则会看到标题文本中的字符通常不会碰到边框。

There are two reasons to this. 这有两个原因。 First, the height of a font is a design concept and does not (normally) correspond to the height of any letter; 首先,字体的高度是一种设计概念,并且(通常)不与任何字母的高度相对应。 there is empty space above even capital letters as well as below the baseline of text. 在大写字母上方和文本基线下方均存在空白区域。 Descenders like that of “j” or “g” may, in some designs, reach the bottom of the font, and diacritic marks like that of “Ê” may reach the top, or even go beyond it. 在某些设计中,像“ j”或“ g”这样的后缀可能会到达字体的底部,而像“Ê”这样的变音符号可能会到达字体的顶部,甚至超出字体的顶部。

Second, there is normally some leading above and below a line, typically a total of 15–20% of the font height. 其次,通常在一行的上下都有一些前导,通常是字体高度的15–20%。 The default depends on the font (and on the browser). 默认值取决于字体(和浏览器)。 In practice, this is meant improve readability and to prevent characters from touching characters on the previous or next line. 实际上,这意味着提高可读性并防止字符触摸上一行或下一行上的字符。

The leading is indirectly set by setting line-height , since line-height sets the total height that is the sum of font height (font size) and leading. 领先是通过设置line-height间接设置的,因为line-height设置的总高度是字体高度(字体大小)和领先地位的总和。 By setting line-height: 1 , you set leading to zero; 通过将line-height: 1设置line-height: 1 ,将前导值设置为零; this is called “setting solid” in typography, and it can be suitable for isolated lines, like a one-line heading. 这在版式中称为“设置实线”,它适用于孤立的行,例如单行标题。 But there is still spacing caused by the first reason mentioned, the spacing that has been “built in” into the font. 但是仍然存在由提到的第一个原因引起的间距,间距是“内置”在字体中的。 You can even set line-height to a smaller value, like 0.85, making the leading negative. 您甚至可以将line-height设置为较小的值,例如0.85,从而使前导为负。 Beware that this may cause vertical truncation of characters if the font used differs from the one you suggest on your page. 请注意,如果所使用的字体与您在页面上建议的字体不同,则这可能会导致字符的垂直截断。

by default a h2 element has a margin of 19px top an bottom. 默认情况下,h2元素的顶部和底部的边距为19px。 Try to reset it 尝试重设

Try to reset margin and padding : 尝试重置边距和填充:

h2{margin:0;padding:0}

replace 0 with your own values ... 用您自己的值替换0 ...

mimiz mimiz

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

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