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

不问归期 提交于 2019-11-28 13:35:43

The simple answer is that with line-height the character will be in the vertical middle of the line, so a line with a height of 100px has the text in the middle of, thus 50px.

Trufa

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.

When you give an element a line-height of 100% (in your example, this equates to 100px), the text is placed in the middle vertically of a line that's 100px tall.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!