Skip to content

Instantly share code, notes, and snippets.

@nikhgupta
Created March 22, 2012 21:26
Show Gist options
  • Save nikhgupta/2164681 to your computer and use it in GitHub Desktop.
Save nikhgupta/2164681 to your computer and use it in GitHub Desktop.
jQuery - center an element
// center an element
$(element).center
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", (($(window).height() - this.outerHeight()) / 2) +
$(window).scrollTop() + "px");
this.css("left", (($(window).width() - this.outerWidth()) / 2) +
$(window).scrollLeft() + "px");
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment