code_junkie

CSS: Best way to center a DIV on a page vertically and horizontally?

Nov 12th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. var centerIt = function (el /* (jQuery element) Element to center */) {
  2. if (!el) {
  3. return;
  4. }
  5. var moveIt = function () {
  6. var winWidth = $(window).width();
  7. var winHeight = $(window).height();
  8. el.css("position","absolute").css("left", ((winWidth / 2) - (el.width() / 2)) + "px").css("top", ((winHeight / 2) - (el.height() / 2)) + "px");
  9. };
  10. $(window).resize(moveIt);
  11. moveIt();
  12. };
Add Comment
Please, Sign In to add comment