Centering images in a div vertically

后端 未结 3 468
太阳男子
太阳男子 2021-01-28 12:32

I have this code for centering vertically the images in a bunch of divs.

function centerImages(parent, image) {
    var parent_height = $(image).parent().height(         


        
3条回答
  •  Happy的楠姐
    2021-01-28 13:06

    if your div have all the same height and contain only the image, that's a pure CSS solution:
    http://jsfiddle.net/Tpy2w/

    Relevant CSS

    div {
      width: 300px;
      height : 300px; 
      line-height: 300px; 
      text-align: center;    
    }
    
    div img {
      vertical-align: middle;
    }
    

    Just set an height and the same line-height for the div. Then apply vertical-align: middle on the image

提交回复
热议问题