Examples of vertical and horizontal center of images in a fixed height container in css

Source: Internet
Author: User

This "novelty" solution seen on a website (freeappstools) is novel, mainly because it has not been mentioned in various Summary methods in China and is quoted in quotation marks, it is only because many people know the principle that the line-height method is used.

Line-height does not work for img elements:

The code is as follows: Copy code

<Div style = "height: 250px; background: # ccc; line-height: 250px; width: 400px; text-align: center;">

</Div>

Line-height is used for text in the box, and the image is aligned with the baseline of the text:

The code is as follows: Copy code

<Div style = "height: 250px; background: # ccc; line-height: 250px; width: 400px; text-align: center">
A test paragraph
</Div>

 

Set vertical-align: middle of img to center the image vertically:

The code is as follows: Copy code

<Div style = "height: 250px; background: # ccc; line-height: 250px; width: 400px; text-align: center">
A test paragraph
</Div>

 

So someone has done this: (set font-size: 1px, set the font color to the same as the background color, set the left indent-1px, and the picture is perfectly vertical and horizontally centered, note ". ")

 

The code is as follows: Copy code
<Div style = "height: 250px; background: # ccc; line-height: 250px; font-size: 1px; width: 400px; text-align: center; text-indent: -1px; color: # ccc ">
.
</Div>

 

The above method has passed IE7 +, FF, Chrome testing ~~

Or, if you do not need to be compatible with earlier IE versions, you can directly use the: after pseudo class to process this ".",
Div: after {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.