Posted By

ginoplusio on 12/16/08


Tagged

div javascript prototypejs center vertically


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

MMDeveloper


javascript to center a div with prototype.js


 / Published in: JavaScript
 

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

  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();

Report this snippet  

You need to login to post a comment.