Skip to content

Instantly share code, notes, and snippets.

@z-------------
Last active August 29, 2015 14:01
Show Gist options
  • Save z-------------/10870360cf4762189ed2 to your computer and use it in GitHub Desktop.
Save z-------------/10870360cf4762189ed2 to your computer and use it in GitHub Desktop.
Position an element at the center of the screen
HTMLElement.prototype.center = function(){
this.style.top = "50%";
this.style.left = "50%";
this.style.marginTop = -this.offsetHeight / 2 + "px";
this.style.marginLeft = -this.offsetWidth / 2 + "px";
}
// document.querySelector("#myDiv").center();
// assumes position absolute or fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment