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:

http://jsfiddle.net/wdzx4/6/

it's working correctly. however, when try using same class image, other image no longer vertically centered:

http://jsfiddle.net/b4bbd/

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

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -