Centring a div in NS
And then in HTML I open the div then call the centreDiv function, thus:
function centreDiv(myDiv, divHeight, divWidth)
var windowWidth = window.innerWidth
var windowHeight = window.innerHeight
else if (document.all)
var windowWidth = document.body.clientWidth
var windowHeight = document.body.clientHeight
var windowWidth = 0
var windowHeight = 0
if (windowWidth > 0 && windowHeight > 0)
getElement(myDiv).top = parseInt((windowHeight / 2) - (divHeight / 2));
getElement(myDiv).left = parseInt((windowWidth / 2) - (divWidth / 2));
The div is set to a width and height of 400 and 749 respectively in CSS. The div looks fine in both IE and NS however it is in the top left of the screen in NS 6 and centred in IE and NS 4.7.
<div class="backimage" id="mainPannel">
centreDiv("mainPannel", 400, 749);
Has anyone got any ideas?
Thanks in advance.
Here's a post on another forum that addresses this exact issue...
Thanks for your help.
I would love to use CSS but I can't find a way of doing it. I have a div which is a set width and height, and it needs to stay that way. How do you centre that in the middle of the screen (both horizontal and vertical) on all screen resolutions? Oh and it is an XHTML page so I don't want to use tables.
Well, the problem with you code is first you aren't accessing the style object first, before modifying the top and left properties. Also, with XHTML, document.body.clientHeight will return 0, so you will never make that calculation anyway. ALL that aside...here's a tester page I did...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
border: 1px dashed #999;
<div id="content">Yeeehaw! We've got a centered one!</div>