Skip to content

Instantly share code, notes, and snippets.

@puneetkay
Created December 13, 2013 10:42
Show Gist options
  • Save puneetkay/7942567 to your computer and use it in GitHub Desktop.
Save puneetkay/7942567 to your computer and use it in GitHub Desktop.
Small jQuery plugin for center of parent or window! (Tweaked to set margin instead of position)
jQuery.fn.center = function(parent) {
if (parent) {
parent = $(this).parent();
} else {
parent = window;
}
$(this).css({
"margin-top": ((($(parent).height() - $(this).outerHeight()) / 2) + $(parent).scrollTop() + "px"),
"margin-left": ((($(parent).width() - $(this).outerWidth()) / 2) + $(parent).scrollLeft() + "px")
});
return $(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment