Center an element on the screen


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



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.