Skip to content

Instantly share code, notes, and snippets.

@jswhisperer
Created July 8, 2013 19:09
Show Gist options
  • Select an option

  • Save jswhisperer/5951583 to your computer and use it in GitHub Desktop.

Select an option

Save jswhisperer/5951583 to your computer and use it in GitHub Desktop.
Center anything with jquery
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;
}
//Use the above function as:
$(element).center();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment