Last active
August 29, 2015 14:07
-
-
Save adamculpepper/f74e3dda759e265a7167 to your computer and use it in GitHub Desktop.
Center an element to another element
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
// demo: http://jsfiddle.net/adamculpepper/9bf2r9uj | |
centerElement($(".center-it.me"), $(".center-it.to")); | |
function centerElement(me, to) { | |
var toWidth = to.outerWidth(); | |
var meWidth = me.outerWidth(); | |
var meCenter = Math.abs(meWidth / 2) - (toWidth / 2); | |
me.css({ | |
'position': 'absolute', | |
'left': meCenter | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Requires jQuery
Demo: http://jsfiddle.net/adamculpepper/9bf2r9uj