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)