CSS learning notes 06 simple understanding of line-height, csslineheight

Source: Internet
Author: User

CSS learning notes 06 simple understanding of line-height, csslineheight

When creating a page, you will often encounter text images that need to be centered. At this time, you only need to set the line-height attribute of the text to the height of the element that wraps the text to make the text center display. Let's take a look at this phenomenon.

1 <! DOCTYPE html> 2 

By default, the text is placed in the upper left corner of the div, and the line-height attribute of the text is now set.

At this time, we can see that the text is magically centered. Why? To know the reason, you need to understand what the line-height attribute is and what it represents. The line-height attribute is used to set the row spacing, that is, the distance between the row and the row, it is generally referred to as the line height. The more official explanation is the distance between the baselines of the text line. Here is a term that needs to be explained. I think you should have guessed it. That is, the baseline. So what is a baseline? Take a look at the image below

I'm very impressed, that is, the four-line three cells used for learning Pinyin in elementary school. This template is very similar to the template that will introduce the line-height principle.

Based on the above figure, it is easy to obtain the following equivalent relationship

Line Height = distance between two lines of text = text top line to text baseline + TEXT baseline to text bottom line + doubled line spacing = text top line + TEXT bottom line + double Line spacing (0.5 times above + 0.5 times below)

Since the text lines have 0.5 times the line spacing between the top and bottom, our text is naturally placed in the middle, this also explains why text is centered when the Row Height of the text is equal to the height of the element that wraps the text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.