In CSS resolve Vertical-align:middle Vertical Center Invalid method

Source: Internet
Author: User

Div Horizontal Center is easy, set CSS style text-align:center; It's OK.

Vertical Center also has a property vertical-align:middle; The normal setting of this property is not effective.

The first method: the vertical center effect can be achieved by a blank picture.

The code is as follows Copy Code

 <title> picture centered </title>
 <style>
/* Definition Vertical Center */
#test * {
 margin: 0px;
 padding:0px;
 vertical-align:middle;
}

/* Define div size and horizontally center */
#test {
 background: #fff000;
 width:500px;
 height: 500px;
 text-align:center;
}

/* Auxiliary picture height fills Div, width is 0, so I don't see this picture */
#img {
 width:0px
 height:100%;
}
</style>
 <body>
  <div id= "test"
<!--auxiliary pictures;
   ; img alt= "" src= "" Id= "img"
  <!--the picture to be centered
       </div>
 </body>

 

Personally feel that the vertical-align:middle is relative to the left of the height of the vertical center, I give the left a fill div picture, you can achieve vertical center.

The second way: add a table to the Div.

The code is as follows Copy Code
<style>
#a {
Background: #fff000;
width:500px;
height:500px;
Overflow:hidden;
Text-align:center;
}
</style>
<body>
<div id= "a" >
<table width= "100%" height= "100%" >
<tr>
&LT;TD align= "center" >

</td>
</tr>
</table>
</div>
</body>

It's easy to set the center in the table.

After testing, neither of these approaches has a problem with browser incompatibility. The first method is recommended.

In addition, say Overflow:hidden; This property, when the picture size exceeds the size of the Div, will shadow the hidden part. If not set, the picture will be all displayed, overlay on the Div.

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.