javascript to center a div with prototype.js


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



Copy this code and paste it in your HTML
  1. var elt = $('ID_DIV_TO_CENTER');
  2.  
  3. // retrieve required dimensions
  4. var eltDims = elt.getDimensions();
  5. var browserDims = document.body.getDimensions();
  6.  
  7. // calculate the center of the page using the browser and element dimensions
  8. var y = (browserDims.height - eltDims.height) / 2;
  9. var x = (browserDims.width - eltDims.width) / 2;
  10.  
  11. // set the style of the element so it is centered
  12. var styles = { position : 'absolute',
  13. top : y + 'px',
  14. left : x + 'px' };
  15.  
  16. elt.setStyle(styles);
  17. elt.scrollTo();

URL: http://www.barattalo.it/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.