Home  >  Article  >  Web Front-end  >  css5 ways to achieve vertical centering_html/css_WEB-ITnose

css5 ways to achieve vertical centering_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:51:03867browse

Summary:

When we create pages, we often encounter the need for vertical centering of content. Today we share 5 methods of vertical centering. Each method has its own advantages and disadvantages. You can choose what you like. way. The following codes have been personally tested by me.

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:

I tested the above code under Chrome. Some of it may have problems under IE. Please pay attention when using it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn