Skip to content

Instantly share code, notes, and snippets.

@danro
Created March 12, 2011 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danro/866885 to your computer and use it in GitHub Desktop.
Save danro/866885 to your computer and use it in GitHub Desktop.
jQuery vertical align function
(function ($) {
// vertical align (parent relative)
$.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);
});
};
// vertical align (window relative)
$.fn.vAlignWin = function() {
return this.each(function(i) {
var ah = $(this).height();
var ph = $(window).height();
var mh = (ph - ah) / 2;
if(mh>0) {
$(this).css('margin-top', mh);
} else {
$(this).css('margin-top', 0);
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment