Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

specifying the left property in CSS using javascript

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
I need a way of specifying the left property of a div tag using javascript. I want the tag centered(along with it's contents) on the screen regardless of the resolution. The page should occupy the entire screen when the resolution is at 640X480. right now i have...

<div id=&quot;Layer8&quot; align=&quot;center&quot; style=&quot;position:absolute; left:0px; top:70px; width:640; height:30px; z-index:8; background-color: white; layer-background-color: white; border: 1px none #000000&quot;>

in the left property i need a function which will perform the following calculation (screen.width - 640) / 2 and use the result to specify how far left the div tag should be placed.

any help is greatly appreciated!!!
 
Something like this? Take what you need, I'm sure you need to cut stuff out.



<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- //This is for centering the layer
var nav = navigator.userAgent;
var arr = nav.split(&quot; &quot;);

alert(arr.length);

//Catch Netscape 6+ (tested on Netscape 6.01)
if (document.getElementById && !document.all)
{
winW = window.innerWidth;
winH = window.innerHeight;
}
//Catch Opera browser (tested on 5.12)
else if (document.getElementById && arr[6] == &quot;Opera&quot;)
{
winW = window.innerWidth;
winH = window.innerHeight;
}
//Catch Netscape Navigator (tested on 4.77)
else if (document.layers)
{
winW = window.innerWidth-16;
winH = window.innerHeight;
}
//Catch Microsoft Internet Explorer (tested on 5.5)
else if (document.all)
{
winW = document.body.offsetWidth;
winH = document.body.offsetHeight;
}
document.writeln('<STYLE TYPE=&quot;text/css&quot;>');
document.writeln('<!--');
document.writeln('#layer1 {POSITION: absolute; VISIBILITY: hidden; left:'+(winW/2-350/2)+'; top:'+(winH/2-275/2)+';}');
document.writeln('-->');
document.writeln('</STYLE>');
//-->
</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top