"Tony Vasquez" <co**********@earthlink.net> writes:
Actually, I don't want the screens available width, I'm sorry, I meant the
browsers available width. What I am trying to do is center a DIV vertically
as well as horozontally. Can you help further?
The FAQ has an entry on finding the browser size:
<URL:http://jibbering.com/faq/#FAQ4_9>
I wouldn't use Javscript to center an element, though. CSS is there for
that.
You should also consider what you mean by "centering vertically". If
the document is longer than the viewport, what should the element be
centered wrt.? However, the usual use for vertical centering is to
position a div containing the entire content of the page (which I have
*never* understood. Why not use all the available space instead of
showing ones page as a stamp-sized box in the middle of an empty page).
There are several ways to center a div wrt. the page.
The most correct, according to standards, is with the following CSS:
<style type="text/css">
body,html {height:100%; /* makes document as high as the viewport */ }
div.centered {
margin:auto;
width:640px;
height:480px;
}
</style>
However, older borwsers, especially older IE's, have proplems with
auto margins. The trick that is often used in IE is more dangerous.
It positions the element with the top at the center and the uses
a negative margin to pull it up.
<!--[if IE]>
<style type="text/css">
body,html {height:100%;}
div.centered {
margin:0px;
position:absolute;
width:640px; /* positions element at 320px left of the center */
left:50%; /* */
margin-left:-320px; /* */
height:480px;
top:50%;
margin-top:-240px;
}
</style>
<[end if]-->
This method is more dangerous, because a small screen can make the
negative margin position the element above the top of the page (or to
the lef tof the left side). You cannot scroll there, so the content is
lost.
I recommend against vertical centering in these cases.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'