How to center images in a responsive grid with CSS only and supporting retina displays?

时光怂恿深爱的人放手 提交于 2019-12-04 19:52:30

Use a combination of techniques to do this:

Long question, but if its only about the centering of the images of different sizes you could do it like this:

You need Divs which you will float, those will have the same heights and wdths (you can adjust that however for the different queries).

After that you have another div in each of those divs for vertical and horizontal center:

  <div class="outer">
    <div class="table">
    <div class="image"></div>
     </div>
    </div>

Basically, you float the outer div, apply display table-cell to the .table div, then apply vertical align middle, for vertical centering.

IMPORTANT

A floated div with display:table cell will ignore vertical align! Therfore the additional div!

http://jsbin.com/ajunol/2/edit

.outer {
  height:100px;
    width:100px;
  border:solid;
  float:left;
  margin:10px;
}
.table {
  height:100px;
  width:100px;
  border:solid;
    display:table-cell;
  vertical-align:middle;
}
.image {
  height:50px;
  width:50px;
  background-color:red;
  margin:0 auto;
  max-width:100%;
}

Height, width and background color, obv you can disregard, but you should have max-width 100% so the image doesnt exceed the containing box.

In my example the images are represented as divs, but obviously you will have image tags there and not divs. To work with this, replace the divs with class image with images.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!