Skip to content

Instantly share code, notes, and snippets.

@dustintheweb
Last active December 16, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustintheweb/5445872 to your computer and use it in GitHub Desktop.
Save dustintheweb/5445872 to your computer and use it in GitHub Desktop.
jQuery - Dynamically center elements
// >> jQuery - Dynamically center elements >>>>>>>>>>>>>>>>
var el_1 = $('.element-1'),
el_2 = $('.element-2'),
el_3 = $('.element-3'),
el_4 = $('.element-4');
// -- X&Y ---------------
function XYmid(){
el_1.css({'marginLeft' : el_1.outerWidth()/-2, 'marginTop' : el_1.outerHeight()/-2});
el_2.css({'marginLeft' : el_2.outerWidth()/-2, 'marginTop' : el_2.outerHeight()/-2});
}
// -- X ---------------
function Xmid(){
el_3.css({'marginLeft' : el_3.outerWidth()/-2});
}
// -- Y ---------------
function Ymid(){
el_4.css({'marginTop' : el_4.outerHeight()/-2});
}
XYmid();
Xmid();
Ymid();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment