views:

162

answers:

3

HTML:

<ul>
  <li><img src="image1.png" /></li>
  <li><img src="image2.png" /></li>
  <li><img src="image3.png" /></li>
  <li><img src="image4.png" /></li>
  <li><img src="image5.png" /></li>
  <li><img src="image6.png" /></li>
</ul>

... the images are all different sizes, I'd like to center them vertically.

jQuery:

$('ul li').css('paddingTop', height($("ul li").height() - ("li img") / (2)));
# padding-top = height of li - height of image / 2

.. but this isn't working.

+5  A: 

A Better Way?

If you're using jQuery, why not use one of the centering plugins?

// make sure li in this case is position:relative;
$("ul li img").center();

Present Problems

The following line has many problems:

height($("ul li").height() - ("li img") / (2))

height() is not a function, unless you've declared it elsewhere. If so, what is it suppose to do exactly? Note, I'm not referring to $.height(), which is a valid method in the jQuery Framework. Additionally, ("li img") is not a numerical value, so dividing it by 2 makes no sense.

Perhaps the following may be more helpful:

$("ul li img").each(function(){
  var pHeight = $(this).parent().height();
  var iHeight = $(this).height();
  var diff    = Math.round(pHeight - iHeight);
  $(this).parent().css("paddingTop", diff);
});
Jonathan Sampson
tried that plugin, not working :(
Nimbuz
Actually the height, width, outerHeight, and outerWidth functions do exist.
czarchaic
padding-top = height of li - height of image / 2
Nimbuz
Make sure your li's have "position:relative". The plugin works.
Jonathan Sampson
czarchaic, I said `height()` is a jQuery function, but not a native Javascript function.
Jonathan Sampson
Jonathan, oops, reread the code and saw the other height function, my bad.
czarchaic
Ah yes, almost works with position:relative on li, but I still need to define width/height for all the images for it to work.
Nimbuz
You could try something like this before calling the center plugin: `$("img").attr({width:$(this).width(),height:$(this).height()});`
Jonathan Sampson
@Jonathan, wouldn't that have to be in a `$("img").each` loop for `this` to point to the `img`?
Doug Neiner
Doug, Yes. Oh! Oops.
Jonathan Sampson
`$("img").each(function(){ $(this).attr({width:$(this).width(),height:$(this).height()}); });`
Jonathan Sampson
+1  A: 

I think @Jonathan's answer is what you should follow ( centering plugin ) but here is your code cleaned up quite a bit:

$('ul li').each(function(){
   var $li = $(this), $img = $li.find('img');
   $img.css('padding-top', ($li.height() / 2) - ($img.height() / 2));
});

Of course, this will only work when the li has a fixed height in the CSS.

Doug Neiner
Dollar signs...you must be a PHP programmer ;)
Jonathan Sampson
LOL, no, I always use dollar signs when storing a jQuery result set. So `var domElement = $("h1")[0];` and `var $resultSet = $("h1");`
Doug Neiner
Ah; in all my time using jQuery I've never seen that. Interesting.
Jonathan Sampson
Oh, it works either way just fine. Its just a nice way of knowing what is a result set and what is a plain DOM element or normal JS variable.
Doug Neiner
A: 

Vertically center

$('ul li img').each(function(){
  var height=$(this).outerHeight(),
  li=$(this).closest('li'),
  li_height=li.outerHeight();
  li.height(li_height+'px');
  $(this).css({marginTop: (li_height-height)/2+'px'});
});
czarchaic
The `+'px'` is not necessary. When you pass in a number, jQuery will automatically assume it is in pixels.
Doug Neiner