How to center an image?

Source: Internet
Author: User

I find three ways to center an image in a div:

<Style type = "text/CSS">. container1 {border: solid 1px #666; width: 100px; Height: 100px ;}. container1 IMG {margin: 42px 0px 0px 42px;/* (100-16)/2 = 42 */}. container3 {border: solid 1px #666; width: 100px; Height: 100px; position: relative ;}. container3 IMG {position: absolute; top: 42px; left: 42px;/* (100-16)/2 = 42 */}. container4 {border: solid 1px #666; width: 100px; Height: 100px; position: relative ;}. container4 IMG {position: absolute; top: 50%; margin-top:-8px;/* 16/2 = 8 */left: 50%; margin-left:-8px ;} </style> <Div class = "container1">  </div> <br /> <Div class = "container3">  </div> <br/> <Div class = "container4">  </div>

In my opinion, the third method is graceful and accurate, which I borrow from My last example.

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.