Vertical center of text with fixed height and no fixed number of lines

Source: Internet
Author: User

As we all know, the horizontal direction of the center is very good to do, Text-align:center can do; center vertically if the number of lines and the height of the text is fixed also good implementation, but if the number of rows is not fixed?

Recently, a requirement was received: Height fixed, the number of lines of text may be one line, two lines, but no matter how many lines, the text should be vertically centered. For example:

For the H5 page, the CSS3 translate can be done, the principle is the outer div relative positioning, the element inside the absolute positioning, distance from the parent container top value is 50%, and then through the Translate property to move it up 50% relative to itself.

Its CSS is:

For the PC, it's not so good to fool: the low-version browser does not support the CSS3 attribute, so what should we do? One feature of the table is that the content can be vertically centered, no matter how much text is inside. So we can use a table to achieve vertical centering of text, so we can use CSS to simulate the vertical center of the table. And display:table can do: not too low version of IE browser does not support display:table, so in order to be compatible with IE browser, also need to use CSS hack to implement: its CSS is:

In this way, both on the PC, or in the H5 are able to achieve a high level of fixed, the number of lines is not fixed vertically centered.

Vertical center of text with fixed height and no fixed number of lines

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.