Css Image vertical center (compatible with mainstream browsers, including IE6/IE7)

Source: Internet
Author: User
Tags advantage

1. Use table-cell to center, font-size for compatibility with IE6 +/FF/Chrome
Advantage: no additional tags are added and compatible with mainstream browsers.

The code is as follows: Copy code
<! -- Html structure -->
<Div class = "box-1">
<A class = "img-wrap-1" href = "#">

</A>
</Div>
/* Style */
. Box-1 {display: table ;}
. The img-wrap-1 {display: table-cell; width: 200px; height: 200px; border: 1px solid # ccc; text-align: center; * font-size: 178px; zoom: 1; vertical-align: middle ;}
/* The ratio of height to font-size is approximately: 1: 0.893; zoom: 1 is used to trigger hasLayout of IE */

2. Use line-height to implement center compatibility with IE7 +/FF/Chrome
Advantage: no hack is required and compatible with mainstream Internet Explorer 7 and Internet Explorer 7
Disadvantage: This method is not compatible with IE6. Of course, you can use the font-size in method 1 to be compatible with IE6.

The code is as follows: Copy code
<! -- Html structure -->
<Div class = "box-2">
<A class = "img-wrap-2" href = "#">

</A>
</Div>
/* Style */
. Img-wrap-2 {display: inline-block; width: 200px; height: 200px; line-height: 200px; border: 1px solid # ccc; text-align: center ;}
. Img-wrap-2 img {vertical-align: middle ;}

3. Use the button tag to implement Center-compatible IE6 +/FF/Chrome
Advantage: no hack is required and compatible with mainstream Internet Explorer 6 and Internet Explorer 6
Disadvantage: extra tag buttons are added.


 

The code is as follows: Copy code
<! -- Html structure -->
<Div class = "box-3">
<A class = "img-wrap-3" href = "http://www.111cn.net/" target = "_ blank">
<Button disabled = "disabled"> </button>
</A>
</Div>
/* Style */
. Box-3 {display: inline-block; width: 200px; height: 200px; border: 1px solid # ccc; text-align: center ;}
. Img-wrap-3 button {width: 100%; height: 100%; border: 0; background: none ;}
. Img-wrap-3 img {cursor: pointer ;}

4. Use 1px small images to achieve vertical center compatibility with IE6 +/FF/Chrome
Advantage: no hack is required and compatible with mainstream Internet Explorer 6 and Internet Explorer 6
Disadvantage: added the redundant label img.


 

The code is as follows: Copy code
<! -- Html structure -->
<Div class = "box-4">

<A class = "img-wrap-4" href = "#" target = "_ blank">

</A>
</Div>
/* Style */
. Box-4 {display: inline-block; width: 200px; height: 200px; border: 1px solid # ccc; text-align: center ;}
. Blank {width: 0px; height: 100%; vertical-align: middle ;}

Of course, the vertical center method has more than one of the above methods, but currently it is more common, and the compatibility is better, if you find a better method to supplement later

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.