Use CSS to vertically center unknown height content

Source: Internet
Author: User

Solution 1:

 <!  Doctype html  >  <  Html  >  <  Head  >      <  Meta  Charset  = "UTF-8"  />      <  Title  > Demo </  Title  >      <  Style  Type  = "Text/CSS"  >  # Outer  {  Width  :  500px  ; Height  :  200px  ;  Margin  :  50px auto  ;  Border  :  1px solid # ccc ;  Display  :  Table  ;  Text-align  :  Center  ;  # Position  : Relative  ;          }  # Middle  {  Display  :  Table-Cell  ;  Vertical-align  : Middle  ;  # Position  :  Absolute  ;  # Top  :  50%  ;  # Left :  50%  ;          }  # Inner  {  # Position  :  Relative  ;  # Top :  -50%  ;  # Left  :  -50%  ;          }      </  Style  >  </  Head  > <  Body  >      <  Div  ID  = "Outer"  >          <  Div  ID  = "Middle"  >              <  IMG  ID  = "Inner"  SRC = "Http://static.cnblogs.com/images/logo_small.gif"  />          </  Div  >      </  Div  >  </  Body  >  </  Html  > 

Solution 2:

 <!  Doctype html >  <  Html  >  <  Head  >      <  Meta  Charset  = "UTF-8"  />      <  Title  > Demo </  Title >      <  Style  Type  = "Text/CSS"  >  # Outer  {  Width  :  500px  ;  Height :  200px  ;  Margin  :  50px auto  ;  Border  :  1px solid # ccc  ; Position  :  Relative  ;          }  # Inner  {  Position  :  Relative  ; Left  :  50%  ;  Top  :  50%  ;  Margin-left  :  -71px ;  Margin-top  :  -27px  ;          }      </  Style  >  </  Head  >  <  Body  >     <  Div  ID  = "Outer"  >              <  IMG  ID  = "Inner"  SRC  = "Http://static.cnblogs.com/images/logo_small.gif"  />      </  Div  >  </  Body >  </  Html  > 

Solution 1 the main principle is to use table and table-Cell Layout in a standard browser, and then use vertical-align: middle to center elements, but ie67 does not support table layout, therefore, CSS hake is used, which is an attribute starting.

Solution 2 uses negative margin, but the disadvantage is to know the width and height of the center content.

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.