How to make a picture vertically centered in CSS

Source: Internet
Author: User

Core tip: use a pure CSS to achieve an unknown size of the picture (but width are less than 200px) in a 200px square container horizontally and vertically centered

In the past Taobao ued recruitment has such a topic:

"Use pure CSS to implement an unknown size picture (but width is less than 200px) centered horizontally and vertically in a 200px square container." ”

Of course, the topic is not random, but has its reality, vertical center is Taobao work most often encountered a problem, very representative.

The difficulty of the topic is two points:

1. vertically centered;


2. The picture is a permutation element with some special characteristics.


As for how to solve, here is a trade-off between the relative structure of clean, CSS simple workaround:


. box {


/* Non-IE's mainstream browser recognized vertical Center method * *


Display:table-cell;


Vertical-align:middle;

/* Set Horizontal Center *


Text-align:center;

/* for IE hack * *


*display:block;


*font-size:175px;/* about the height of the 0.873,200*0.873 about 175*/


*font-family:arial;/* prevent hack failure caused by utf-8, such as GBK code

width:200px;


height:200px;


border:1px solid #eee;


}


. box img {


/* Set Picture Vertical Center * *


Vertical-align:middle;


}


"/>





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.