October 26, 2009

Center Anything

Small snippet of jQuery to center anything within a relatively positioned element.

$(function() {

	$(element).each(function() {

		var h = $(this).height();
		var w = $(this).width();

		var h = 0 - (h / 2);

		$(this).css({
			'height' : h,
			'width' : w,
			'margin-top' : h
		});

	});

});

Leave a Response