Center something in jQuery


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. jQuery.fn.center = function () {
  2. this.css("position","absolute");
  3. this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
  4. this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
  5. return this;
  6. }
  7.  
  8. //Use the above function as:
  9. $(element).center();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.