Forums
This topic is locked
centering page with layers?
Replies
Replied 06 Aug 2005 09:16:49
06 Aug 2005 09:16:49 gil gallo replied:
center and div align center does not work outside of content using other div tags
Replied 08 Aug 2005 18:05:28
08 Aug 2005 18:05:28 myke black replied:
it is possible, although you have to do a bit of extra graft to get it working. Basically you need to get the width and height of the current browser window, then get the width and height of your div layer, then you reposition the top and left css attribute of the layer so that it centers ok. When I do this, I also set it so that the layer is initially hidden, and made visible after the page load because the page jumps to the centre when the page is loaded up. Heres some code for you:
<pre id=code><font face=courier size=2 id=code>
<html>
<head>
<style>
body {left:0px;top:0px;}
#mainDiv {left:0px;top:0px;width:760;height:624;background-color<img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle>ink;position:absolute;visibility:hidden}
</style>
</head>
<body onResize = setMiddle()>
<div id="mainDiv">
Here is the main div layer, put your content in here.
</div>
<script>
function setMiddle(){
var sWidth = actualPageWidth();
var sHeight = actualPageHeight();
var mWidth = parseInt(document.getElementById("mainDiv".style.width)
var mHeight = parseInt(document.getElementById("mainDiv".style.height)
lMargin = (sWidth - mWidth)/2;
if (lMargin < 0) lMargin="0";
tMargin = (sHeight - mHeight)/2;
if (tMargin < 0) tMargin="0";
document.getElementById("mainDiv".style.left = lMargin + "px";
document.getElementById("mainDiv".style.top = tMargin + "px";
document.getElementById("mainDiv".style.visibility = "visible"
return false;
}
function actualPageHeight() {
if (window.innerHeight != null)
return window.innerHeight;
if (document.body.clientHeight != null)
return document.body.clientHeight;
return(null);
}
function actualPageWidth() {
if (window.innerWidth != null)
return window.innerWidth;
if (document.body.clientWidth != null)
return document.body.clientWidth;
return(null);
}
setMiddle()
window.onResize = setMiddle()
</script>
</body>
</html>
</font id=code></pre id=code>
Alternatively, you can use tables, which is a lot easier.
<pre id=code><font face=courier size=2 id=code>
<html>
<head>
<style>
body {left:0px;top:0px;}
#mainDiv {left:0px;top:0px;width:760;height:624;background-color<img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle>ink;position:absolute;visibility:hidden}
</style>
</head>
<body onResize = setMiddle()>
<div id="mainDiv">
Here is the main div layer, put your content in here.
</div>
<script>
function setMiddle(){
var sWidth = actualPageWidth();
var sHeight = actualPageHeight();
var mWidth = parseInt(document.getElementById("mainDiv".style.width)
var mHeight = parseInt(document.getElementById("mainDiv".style.height)
lMargin = (sWidth - mWidth)/2;
if (lMargin < 0) lMargin="0";
tMargin = (sHeight - mHeight)/2;
if (tMargin < 0) tMargin="0";
document.getElementById("mainDiv".style.left = lMargin + "px";
document.getElementById("mainDiv".style.top = tMargin + "px";
document.getElementById("mainDiv".style.visibility = "visible"
return false;
}
function actualPageHeight() {
if (window.innerHeight != null)
return window.innerHeight;
if (document.body.clientHeight != null)
return document.body.clientHeight;
return(null);
}
function actualPageWidth() {
if (window.innerWidth != null)
return window.innerWidth;
if (document.body.clientWidth != null)
return document.body.clientWidth;
return(null);
}
setMiddle()
window.onResize = setMiddle()
</script>
</body>
</html>
</font id=code></pre id=code>
Alternatively, you can use tables, which is a lot easier.
Replied 08 Aug 2005 21:15:04
08 Aug 2005 21:15:04 Shane Gardner replied:
Replied 09 Aug 2005 00:34:31
09 Aug 2005 00:34:31 gil gallo replied:
wow, you guys are good. i work crazy hours, and I'm still not as proficient as you guys. How do I learn to be a master like you guys?
Replied 10 Aug 2005 19:09:33
10 Aug 2005 19:09:33 myke black replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote> wow, you guys are good. i work crazy hours, and I'm still not as proficient as you guys. How do I learn to be a master like you guys?<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
blood, sweat, tears and plenty of caffeinated drinks.
blood, sweat, tears and plenty of caffeinated drinks.