Comparison of several methods of vertical centering (pure CSS)

Source: Internet
Author: User

Method One
<div class= "table" style= "height:200px;" >   <divclass= "Table-cell" >        content   </div></div>.table {display:table;}. Table-cell {Display:table-cell; vertical-align:middle;}

Advantages : Content can dynamically change the height, if you do not need to support IE7 browser, this should be the preferred solution, is also recommended by the Web.

cons : IE7 and IE8 beta are not supported.

Method Two

<div class= "Content" >Content</div>

#content {
Position:absolute;
top:50%;
height:240px;
margin-top:-120px; /* Negative half of the height */
Overflow:auto;
}

Pros : Applies to all browsers.

cons : The content area is highly fixed, and if you don't use Overflow:hidden or Overflow:auto, the content display will go out of the content area and it won't look centered.


When you use Overflow:hidden, the content disappears and the scroll bar appears when you use Overflow:auto.

Method three-deprecated

This method utilizes the calculation method of Margin:auto, sets a fixed height for the content area div#content, and sets the position:absolute;top:0; bottom:0;. Because it has a fixed height, in fact, can not make the upper and lower spacing of 0, so margin:auto; will make it vertically centered.

<divid= "Content" >content </div>

#content {
Position:absolute;
top:0;
bottom:0;
Margin:auto;
height:240px;
}

Advantages : Obviously not as the first method, generally not.

cons : IE7 and IE8 beta are not valid, because the content area is highly fixed, and the second method has the same problem.

Method Four

This method can only be placed in a single line of text. Simply setting the line-height to the height value of the content container allows the text to be centered.

<divid= "Content" >content </div>

#content {height:100px; line-height:100px;}

Pros : Applies to all browsers and is useful for small elements where text does not wrap, such as centering the button text or a single line of text.

disadvantage : Only valid for text (block-level elements are not valid); Because the line-height is too high, the line spacing will be very large;

Comparison of several methods of vertical centering (pure CSS)

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.