简体   繁体   English

CSS-行高属性,它如何工作(简单)

[英]CSS - Line height property, how it works (simple)

So in this simple example I have as final result: 因此,在这个简单的示例中,我得到了最终结果:

替代文字

This is a very simple question but I simply can't get my head around it. 这是一个非常简单的问题,但我根本无法解决。

To achieve the vertical centering of the numbers I used: 要实现我使用的数字的垂直居中:

line-height:100px;

Which works great and have been doing it trial and error basis. 哪个效果很好,并一直在做反复试验的基础。

My question is specifically why the line-height:50px; 我的问题特别是为什么line-height:50px; just gets if half of the way. 只是获得一半。

If the small div has a height of 100px and I an positioning relative to it, shouldn't the half of it center it to the half. 如果small div的高度为100px并且我的位置相对于它,则不应将其一半居中。

This specially puzzles me since, when I center a div : 因为当我居中div时 ,这特别使我感到困惑:

I would use: margin:50px 0 0 50px; 我将使用: margin:50px 0 0 50px; to get this: 得到这个:

替代文字


I realize this question might be an overkill since the answer might be (probably will be very simple), so sorry! 我知道这个问题可能是一个矫kill过正的问题,因为答案可能是(可能非常简单),非常抱歉! but I guess it is better the "why doesn't this work" questions ;) 但我想最好是“为什么不行”的问题;)

Thanks in advance!! 提前致谢!!

简单的答案是,使用line-height时,字符将位于行的垂直中间,因此,高度为100px的行的文本位于中间,因此为50px。

Ok I'm quite sure I got it now, I'll answer my own question, thanks to all you answers this would be a more graphical explanation (which always helps me): 好的,我很确定我现在明白了,我会回答我自己的问题,感谢大家的回答,这将是一个更加图形化的解释(它总是对我有帮助):

替代文字

This is just a complement to jarret's answer (the first one to nail it IMHO) 这只是对jarret答案的补充(第一个将它钉牢的恕我直言)

Thanks for all the helpful answers 感谢所有有用的答案

BTW: Sorry for the horrid artwork. 顺便说一句:对不起的艺术品。

I think line-height is just what is sounds like. 我认为line-height就是听起来的样子。 It would be different if it was just half of the height, as you would have to account for the font-size , etc. line-height just makes a blocky thing with a height of 100px , in your case, and centers the text within that. 如果仅是高度的一半,则将有所不同,因为您必须考虑font-sizeline-height只是使块状的东西具有100px的高度(在您的情况下),并将文本居中那。

Basically, line-height adds the value ((desired height) - (font-size)) / 2 to the top and bottom of the text, while adding margins does not account for that. 基本上, line-height在文本的顶部和底部添加值((desired height) - (font-size)) / 2 ,而添加边距并不说明这一点。

Using the line-height property to 100px means that the text is going to be vertically centered in the middle of a line with a 100px height. 使用line-height属性设置为100px意味着文本将垂直放置在高度为100px的行中间。 Meaning it will be placed around 50px. 这意味着它将放置在50px左右。 Setting the margins to 50px will give you a similar result but you might notice its not exactly centered. 将边距设置为50px将为您提供类似的结果,但您可能会注意到其位置不完全居中。

Essentially the line-height helps to set the "leading" and "half-leading". 本质上,行高有助于设置“领先”和“一半领先”。 The 100px accounts for the total height of the element and the text is being "vertically aligned" within that area. 100像素占元素的总高度,并且文本在该区域内“垂直对齐”。 Half is going above and half below. 一半在上面,一半在下面。

This explains better than I. http://www.w3.org/TR/CSS2/visudet.html#line-height 这比我解释得更好。http://www.w3.org/TR/CSS2/visudet.html#line-height

Already been answered, but I thought I might add that the formula is usually is: 已经回答了,但是我想我可以补充一下,公式通常是:

The difference between line-height and font-size is called Leading. 行高和字体大小之间的差异称为行距。 Then divide this Leading by 2 to get half-leading, which is put above and below your text causing it to be vertically centered. 然后将此Leading除以2得到一半的Leads,它位于文本的上方和下方,从而使其垂直居中。

The following set of slides will explain a lot about line-height. 下面的幻灯片将对行高进行很多说明。 You can start at slide 72 for what I explained above, but I would recommend going through the whole set. 您可以从幻灯片72开始,以获得上面我所解释的内容,但是我建议您仔细阅读整个内容。

This doesn't work because line-height is specific to text and not to actual heights of elements like divs. 这行不通,因为line-height特定于文本,而不特定于div等元素的实际高度。 I would suggest using height and not line-height when your subject is not in fact text. 当您的主题实际上不是文本时,我建议您使用高度而不是行高。

当您将元素的行高设置为100%时(在您的示例中,这等于100px),文本将被放置在100px高的行的垂直中间。

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

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