CSS for vertical centering

Source: Internet
Author: User

There are a number of ways to center vertically, and here's a quick and easy way to record what I've found.

A parent div and a sub-div need to be specified, and the sub-div will be vertically centered in the parent Div, then the content you want to center vertically is placed in the sub-div.

Chrome, Firefox, and IE are all available. IE only tested the edge and IE11.

1 <HTML>2 3 <Head>4     <style>5 . Vcenter-outter{6 Display:Table;7 background:Green;8 width:100%;9 Min-height:300px;Ten         } One  A . Vcenter-inner{ - Display:Table-cell; - text-align:Center; the vertical-align:Middle; -         } -  - . Vcenter-inner span{ + background:Red; - width:33%; + Height:Auto; A         } at     </style> - </Head> -  - <Body> -     <Divclass= "Vcenter-outter"> -         <Divclass= "Vcenter-inner"> in             <span>Center vertically</span> -         </Div> to     </Div> + </Body> -  the </HTML>

The advantage of this approach is that if you have several different types of elements that you want to center vertically on the same line, you can use a parent div to cover multiple sub-div.

1 <HTML>2 3 <Head>4     <style>5 . Vcenter-outter{6 Display:Table;7 background:Green;8 width:100%;9 Min-height:300px;Ten         } One  A . Vcenter-inner{ - Display:Table-cell; - text-align:Center; the vertical-align:Middle; -         } -  - . Vcenter-inner span{ + background:Red; - width:33%; + Height:Auto; A         } at     </style> - </Head> -  - <Body> -     <Divclass= "Vcenter-outter"> -         <Divclass= "Vcenter-inner"> in             <span>Vertically centered span</span> -         </Div> to         <Divclass= "Vcenter-inner"> +             <ul> -                 <Li>Vertically centered UL</Li> the                 <Li>Vertically centered UL</Li> *                 <Li>Vertically centered UL</Li> $                 <Li>Vertically centered UL</Li>Panax Notoginseng             </ul> -         </Div> the         <Divclass= "Vcenter-inner"> +             <img>Vertically centered img</img> A         </Div> the     </Div> + </Body> -  $ </HTML>

Reference article: http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201505121820.html

CSS for vertical centering

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.