Skip to content

Instantly share code, notes, and snippets.

@adamculpepper
Last active August 29, 2015 14:07
Show Gist options
  • Save adamculpepper/f74e3dda759e265a7167 to your computer and use it in GitHub Desktop.
Save adamculpepper/f74e3dda759e265a7167 to your computer and use it in GitHub Desktop.
Center an element to another element
// 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
});
}
@adamculpepper
Copy link
Author

Note: Requires jQuery
Demo: http://jsfiddle.net/adamculpepper/9bf2r9uj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment