<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Example 7: Vertical alignment of content with JavaScript & CSS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
#content
{
position: relative; /* Needed for Safari */
margin: auto; /* Takes care of horizontal alignment in standards compliant browsers */
text-align: left;
width: 200px;
height: 200px;
background-color: #fc0;
}
h1, p
{
margin: 0;
padding: 1em;
}
h1
{
font-size: 12px;
line-height: 1.5em;
}
-->
</style>
<script type="text/javascript">
<!--
function getWindowHeight() {
var windowHeight = 0;
if (typeof (window.innerHeight) == 'number') {
windowHeight = window.innerHeight;
}
else {
if (document.documentElement && document.documentElement.clientHeight) {
windowHeight = document.documentElement.clientHeight;
}
else {
if (document.body && document.body.clientHeight) {
windowHeight = document.body.clientHeight;
}
}
}
return windowHeight;
}
function setContent() {
if (document.getElementById) {
var windowHeight = getWindowHeight();
if (windowHeight > 0) {
var contentElement = document.getElementById('content');
var contentHeight = contentElement.offsetHeight;
if (windowHeight - contentHeight > 0) {
contentElement.style.position = 'relative';
contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2)) + 'px';
}
else {
contentElement.style.position = 'static';
}
}
}
}
window.onload = function() {
setContent();
}
window.onresize = function() {
setContent();
}
//-->
</script>
</head>
<body>
<div style="position: relative; top: 121.5px;" id="content">
<h1>
Content</h1>
<p>
This content should be centered in your browser window. CSS is used for horizontal
alignment, while scripting is used for vertical alignment.</p>
</div>
</body>
</html>
reference :
http://www.alistapart.com/d/footers/footer_variation1.html
function PageViewSize() {
// initialize
this.width = 0;
this.height = 0;
// determine window height
function getWindowHeight() {
var windowHeight = 0;
if (typeof (window.innerHeight) == "number") {
windowHeight = window.innerHeight;
} else {
if (document.documentElement && document.documentElement.clientHeight) {
windowHeight = document.documentElement.clientHeight;
} else {
if (document.body && document.body.clientHeight) {
windowHeight = document.body.clientHeight;
}
}
}
return windowHeight;
}
// determine window width
function getWindowWidth() {
var windowWidth = 0;
if (typeof (window.innerWidth) == "number") {
windowWidth = window.innerWidth;
} else {
if (document.documentElement && document.documentElement.clientWidth) {
windowWidth = document.documentElement.clientWidth;
} else {
if (document.body && document.body.clientWidth) {
windowWidth = document.body.clientWidth;
}
}
}
return windowWidth;
}
// assign current window size
this.width = getWindowWidth();
this.height = getWindowHeight();
// method to get 'y' location of panel
this.getTop = function(contentHeight) {
if (this.height > 0 && this.height - contentHeight > 0) {
return ((this.height - contentHeight) / 2);
} else {
return 0;
}
}
// method to get 'x' location of panel
this.getLeft = function(contentWidth) {
if (this.width > 0 && this.width - contentWidth > 0) {
return ((this.width - contentWidth) / 2);
} else {
return 0;
}
}
}