javascript - center images vertically in div -
each image has div parent, these parents' width , height in proportion, question is, can't center each image inside parent horizontally. seems takes first element's height , applies rest of elements. ![it seems takes first elements height , apply of rest ][1]
function centerimages(image) { var parent_height = $('.thumb img').parent().height(); var image_height = $('.thumb img').height(); var top_margin = (parent_height - image_height) / 2; $('.thumb img').css( 'margin-top' , top_margin); } $(".thumb img").each(function() { centerimages(this); });
you pointing same image on again. change to:
function centerimages(image) { var parent_height = $(image).parent().height(); var image_height = $(image).height(); var top_margin = (parent_height - image_height) / 2; $(image).css( 'margin-top' , top_margin); } $(".thumb img").each(function() { centerimages(this); });
plus add 'px' suggested.
Comments
Post a Comment