vertical align with javascript
source: http://snipplr.com/view/9321/vertically-align-within-browser-window/
1 <div id="verticalContainer"> 2 This text will always be vertically centered inside the browser window 3 </div> 4 5 <script type="text/javascript"> 6 function getWindowHeight() { 7 var windowHeight = 0; 8 if (typeof(window.innerHeight) == 'number') { 9 windowHeight = window.innerHeight; 10 } 11 else { 12 if (document.documentElement && document.documentElement.clientHeight) { 13 windowHeight = document.documentElement.clientHeight; 14 } 15 else { 16 if (document.body && document.body.clientHeight) { 17 windowHeight = document.body.clientHeight; 18 } 19 } 20 } 21 return windowHeight; 22 } 23 24 function setContent() { 25 if (document.getElementById) { 26 var windowHeight = getWindowHeight(); 27 if (windowHeight > 0) { 28 var contentElement = document.getElementById('verticalContainer'); // Be sure to specify the correct ID 29 var contentHeight = contentElement.offsetHeight; 30 if (windowHeight - contentHeight > 0) { 31 contentElement.style.position = 'relative'; 32 contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px'; 33 } 34 else { 35 contentElement.style.position = 'static'; 36 } 37 } 38 } 39 } 40 41 window.onresize = function() { 42 setContent(); 43 } 44 45 window.onload = function() { 46 setContent(); 47 } 48 </script>