Vertical center of Div Image

Source: Internet
Author: User

It is easy to center Div horizontally and set CSS styles.Text-align: center;You can.

Vertical center also has an attributeVertical-align: middle;This attribute has no effect on normal settings.

Method 1: Use a blank image to center vertically.

 <  Html  >      <  Style  >  # Image  {  Width  : 500px  ;  Height  :  500px  ;  Background  :  # Fff000  ;  Text-align :  Center  ;  Overflow  :  Hidden  ;      }  # Image img  {  Vertical-align :  Middle  ;  }  # Block  {  Width  :  0px  ;  Height :  100%  ;      }          </  Style  >      <  Body  >          <  Div  ID  = "Image"  >              < IMG  SRC  = "Http://www.mm21.cn/dimg6/qqtu/201105/20110507234620615.jpg"  />               <  IMG  SRC  = ""  ID  = "Block"  />          </  Div  >      </  Body  > </  Html  > 

 

I personally thinkVertical-align: MiddleIt is vertical center relative to the height on the left. I will fill the left with a div image to achieve vertical center.

Method 2: Add a table to the div.

 <  Html  >      <  Style  >  #  {  Background :  # Fff000  ;  Width  :  500px  ;  Height  :  500px  ;  Overflow :  Hidden  ;  Text-align  :  Center  ;  }  </  Style  >      <  Body  >         <  Div  ID  = ""  >              <  Table  Width  = "100%"  Height  = "100%"  >                  <  Tr  >                      <  TD Align  = "Center"  >                          <  IMG  SRC  ="Http://static.cnblogs.com/images/logo_small.gif"   />                      </  TD  >                  </  Tr  >              </ Table  >          </  Div  >      </  Body  >  </  Html  > 

In the table, it is easy to set center.

These two methods have no browser incompatibility issues. The first method is recommended.

In addition, let's talk aboutOverflow: hidden;When the image size exceeds the size of the DIV, the excess part is hidden. If this parameter is not set, the image is displayed in full and overwritten on the div.

 

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.