Skip to content

Instantly share code, notes, and snippets.

@utsengar
Created July 29, 2013 04:58
Show Gist options
  • Save utsengar/6102220 to your computer and use it in GitHub Desktop.
Save utsengar/6102220 to your computer and use it in GitHub Desktop.
Center any div on page (think google.com's search box)
// $(element).center();
jQuery.fn.center = function ()
{
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment