How CSS implements vertically centered images up and down

Source: Internet
Author: User

How CSS implements vertically centered images:
Recommended : As much as possible handwriting code, can effectively improve the learning efficiency and depth.
There are many times when you need to center the image vertically, but there is no immediate property to do this. Of course, the way to achieve the vertical center of the image is multiple, the following is a brief introduction of two of them.
Method One:
Use the margin method to center the picture vertically in the div. The Margin-top value is calculated as: div height/2-picture height/2.
The code example is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><styletype= "Text/css">Div{Height:400px;width:400px;Border:1px solid Red;}div img{Margin-top:127px;}</style></Head><Body><Div><imgsrc= "Mytest/demo/border.jpg"alt= "Ant Tribe"></Div></Body></HTML>

Way two:
The code example is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><styletype= "Text/css">Div{Height:400px;width:400px;Border:1px solid Red;vertical-align:Middle;Display:Table-cell;}</style></Head><Body><Div><imgsrc= "Mytest/demo/border.jpg"alt= "Ant Tribe" /></Div></Body></HTML>

The above code allows the image to be vertically centered, but the IE7 browser does not support this approach.
The implementation method adds the following code to the DIV:

vertical-align:middle;display:table-cell;line-height:400px;

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=4614

For more information, refer to: http://www.softwhy.com/divcss/

How CSS implements vertically centered images up and down

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.