Skip to content

Instantly share code, notes, and snippets.

@devfred
Created May 18, 2012 16:36
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 devfred/2726271 to your computer and use it in GitHub Desktop.
Save devfred/2726271 to your computer and use it in GitHub Desktop.
javascript: Center element within its parent
$.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