Vertical center of layers using CSS

Source: Internet
Author: User
A method mentioned in "A review of WEB compatibility problems that have been confusing" is that "the method of absolute percentage locating and negative value of external patches is used, the negative value is half of its own width and height. "The vertical center of the layer is achieved.

1 div. innerbox {
2 position: absolute;
3 top: 50%;
4 left: 50%;
5 margin:-100px 0 0-100px;
6 width: 200px;
7 height: 200px;
8 border: 1px solid red
9}

This CSS implements the vertical center of layers in the browser. To vertically center innerBox with respect to a parent layer, you only need to set the CSS of the parent layer:

1 div. outbox {
2 width: 400px;
3 height: 400px;
4 border: 1px solid # f00;
5 position: relative
6}

However, the principle that "the negative value is half of its own width and height" restricts its practical application. The constraint between the margin and width and height of innerBox makes the height changes caused by the dynamic changes of innerBox content cannot be reflected to the margin. Unless JS is used for dynamic control, leave aside JS, the vertical center implemented by this principle can only be used when the height and width attributes of innerBox are fixed values. If you want to achieve dynamic content changes in innerBox and keep the vertical center, use the following method:

1. outBox2 {display: table; height: 400px; width: 600px; # position: relative; overflow: hidden; background: # FFCCCC; border: 1px solid # d00 ;}
2. midBox2 {# position: absolute; # top: 50%; display: table-cell; vertical-align: middle ;}
3. innBox2 {# position: relative; # top:-50%; width: 300px; margin: 0 auto}

The corresponding Html structure is as follows:

1 <div class = "outBox2">
2 <div class = "midBox2">
3 <div class = "innBox2" style = "border: 1px solid # c00">
4 vertical center <br>
5 incredible vertical center <br>
6. My blog is centered
7. Incredible vertical center <br>
8. My blog is centered
9 </div>
10 </div>

As for the current usage, both methods are good and have their own purposes. Both methods are compatible with IE6, IE7, IE8 Beta, Firefox3, and Opera9.

I am so tired. Although it is a dry bean curd article, I have to worry about the editor layout and test the code correctness. Well, writing Technical Articles is really physical.

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.