Javascript doesn't work correctly when css class is used more than once? -
previously, asked how center align image (w/ dynamic width) within div , replied code:
it's working correctly. however, when try using same class image, other image no longer vertically centered:
you see, now, 50x50 black image higher should be. noticed first image gets aligned correctly. if add other images different width , height (using same class) after that, misaligned.
could me find problem i'm not familiar javascript.
you need wrap javascript matching elements, instead of calculating height 1 , applying all:
$('div.container_img img').each(function() { var $img = $(this); var h = $img.height(); $img.css('margin-top', +h / -2 + "px"); });
Comments
Post a Comment