Last active
August 29, 2015 14:01
-
-
Save z-------------/10870360cf4762189ed2 to your computer and use it in GitHub Desktop.
Position an element at the center of the screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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