5 Vertical center css and 5 center css

Source: Internet
Author: User

5 Vertical center css and 5 center css
Abstract:

We often encounter vertical content center requirements when creating pages. Today we share five vertical center methods, each of which has its own advantages and disadvantages. You can choose your preferred method. The following code has been personally tested.

Line-height:
<style>        .content {            height:240px;            line-height: 240px;        }    </style><div class="content">        vertical-align:middle;    </div>

 

: Before:
<style>        .content {            height: 240px;        }        .child:before {            content: ' ';            display: block;            height: 120px;        }    </style><div class="content">        <div class="child">
      vertical-align:middle;

</div></div>

 

Padding-top:
<style>        .content {            height:240px;        }        .child {            padding-top: 120px;        }    </style><div class="content">        <div class="child">            vertical-align:middle;        </div>      </div>

 

Position: absolute:
<style>        .content {            position:absolute;            height:240px;        }        .child {            position: relative;            top:50%;        }    </style><div class="content">        <div class="child">            vertical-align:middle;        </div>      </div>

 

Display: table-cell;
<style>        #content {            display:table;        }        #child {            display:table-cell;            vertical-align:middle;            height: 300px;        }    </style><div id="content">          <div id="child">                  vertical-align:middle;        </div>      </div>

 

Summary:

The above code is personally tested in chrome, and may cause some problems in ie. Please pay attention when using it.

 

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.