CSS text is centered vertically

Source: Internet
Author: User

Original Demo

Html

  <Divclass= "box">    <Divclass= "text">I am a single-line Test text</Div>  </Div>  <Divclass= "box">    <Divclass= "text">I am multiline text I am multiline text is multiline text I am multiline text I am multiline text I am multiline text I am multiline text I am multiline text I am multiline text I am multi-line text I am > multiline word</Div>  </Div>

Css

  . box{border:1px solid #f00; Background-color: #eee; width:500px; height:100px;}  . text{}

One, vertical center of single-line text

Solution: Line-height Method

Css

   . Box{ border:1px solid #f00; background-color:#eee; width :500px; height:100px; line-height:100px;}

Note: You can see that this method will make one-line text centered, and multiline text because it is set with the container and other large line-height, resulting in text overflow. Therefore, this method is only suitable for the center of single-line text.

Two, the vertical center of multiline text

Solution: Vertical-align Method

Css

  . Box{ border:1px solid #f00; background-color:#eee; width :500px; height:100px; Vertical-align:middle;display:table-cell;}   . Text{}

Note: You can see that this method can make single-line text, multiline text is centered. However, because Table-cell is an inline type, it causes the original block-level element to move to the same row for each div line. If you need to split two rows, you need to add additional containers outside to control the location.

Related reading:

The horizontal vertical center of a picture with no fixed size and multiline text

Some problems of line-height, Vertical-align and Display:inline-block

CSS text is centered vertically

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.