简体   繁体   中英

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

This specially puzzles me since, when I center a div :

I would use: 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! 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)

Thanks for all the helpful answers

BTW: Sorry for the horrid artwork.

I think line-height is just what is sounds like. 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.

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.

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. Meaning it will be placed around 50px. Setting the margins to 50px will give you a similar result but you might notice its not exactly centered.

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. Half is going above and half below.

This explains better than I. 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.

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.

This doesn't work because line-height is specific to text and not to actual heights of elements like divs. I would suggest using height and not line-height when your subject is not in fact text.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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