
Sometimes you may want to vertically align a block item, but the CSS-only vertical aligning methods don’t make you feel clean. Personally, I hate having to style items as a table/table cell to get them to vertically align in Internet Explorer.
jQuery to the rescue. Using a jQuery vertical align snippet as my base code, I modified it to be a jQuery plugin that will allow you to use the function in the jQuery chain.
jQuery Vertical Align Plugin Code
Simply copy this code into your website (if you don’t know where, leave a comment):
(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
return this.each(function(i){
var ah = $(this).height();
var ph = $(this).parent().height();
var mh = Math.ceil((ph-ah) / 2);
$(this).css('margin-top', mh);
});
};
})(jQuery);
Now, you can use vAlign as you wish; ie: $('#example p').vAlign();.
Did this make your life easier? Was this information worth a buck?
Donate with PayPal (Much appreciated!)
If it’s not working
If it’s not working, it may be because the script applies a margin to the element, which may not work in all situations. Try changing 'margin-top' to 'padding-top' and see if that helps.
Any questions or comments?

Pingback: Vertical centering
Pingback: The Pain of Vertical Alignments with CSS « Ph@ntoM