Vertical and horizontal center of an unknown size image in a known container _ CSS/HTML

Source: Internet
Author: User
In addition to special instructions on vertical and horizontal center of images of unknown size in a known container, this site uses the creation of shared authorization signatures and non-commercial purposes. Please respect the labor results.

In the CSS layout, vertical center of images of unknown sizes has always been a headache. The standard browser only needs to set the line-height of the container and the vertical-align: middle; of the image, but it is useless to IE. I saw a solution to IE on a foreign website a few months ago. I don't feel very ideal and I didn't take it seriously. Recently, I have seen some friends asking similar questions, turning out the code and changing the code for IE.

CSS

. Box {
Height: 140px;
Width: 200px;
Border: solid 1px #666;
Text-align: center;/* horizontal center */
Line-height: 140px;
Font-size: 126px;/* center IE vertically */
}
. Box [class] {
Font-size: 12px;/* this value is required by the standard browser */
}
Img {
Vertical-align: middle;/* vertical center of standard browser images */
}

The font-size of the original foreigner code is the same as the height. After the test, the height of the container is higher than that of the standard browser, and the font size is reduced by 10 percent of the height of the container multiple times, in this way, it looks similar. The reason is unclear.

The test is successful in IE5.5, IE6.0, FF1.5, and Opera9.0, and is invalid for IE5.0 and IE7.0.

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.