Vertical center with CSS

Source: Internet
Author: User
Tags truncated

This method inserts a div outside the content element. Set the div height: 50%; margin-bottom:-contentheight ;.
Content is cleared and displayed in the middle.

<Div id = "floater">
<Div id = "content">
Content here </div>
</Div>

# Floater {float: left; height: 50%; margin-bottom:-120px ;}
# Content {clear: both; height: 240px; position: relative ;}

Advantages:

Applicable to all browsers
Content is not truncated when there is not enough space (for example, window narrowing), and the scroll bar appears

Disadvantages:

The only thing I can think of is the need for additional null elements (not that bad, but another topic)

Method 4

This method uses a position: absolute, div with fixed width and height. This div is set to top: 0; bottom: 0 ;. However, because it has a fixed height, it cannot be separated from the upper and lower distances to 0, so margin: auto; will Center it. It is easy to use margin: auto; to vertically center block-level elements.

<Div id = "content">
Content here </div>

# Content {
Position: absolute;
Top: 0;
Bottom: 0;
Left: 0;
Right: 0;
Margin: auto;
Height: 240px;
Width: 70%;
}

Advantage: Simple

Disadvantages:

IE (IE8 beta) is invalid
When there is not enough space, the content is truncated, but no scroll bar appears.

 

Related Article

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.