Print Page | Close Window

HELP!: find window width? Center a <div>?

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Web Design Discussion
Forum Description: Discussion on web design and development subjects.
URL: https://forums.webwiz.net/forum_posts.asp?TID=4088
Printed Date: 28 March 2026 at 3:11pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: HELP!: find window width? Center a <div>?
Posted By: Kingherc
Subject: HELP!: find window width? Center a <div>?
Date Posted: 05 July 2003 at 5:07pm

Well here's my problem (a JavaScript problem i think):

I have a <div> on my page. I want that div to be shown over the document. But I also want the div to be in the exact center of the page or it won't look right.

I have tried many things, and played with the css styles of the position (top & left attributes). For a minute i got stuck on the ''left'' css attribute. The brightest solution was to enter 50% in the attribute but unfortunately that applies only to the edge, so the width exceeds the center and it doesn't look right again! I have the div's width but I don't know (well i don't know if it CAN happen) how to subtract it from a percantage like 50%-width would be great!

The next idea was to divide the window's width by two and then subtract the half of the div's width. But again, the window's width can only be determined in Netscape. In Internet Explorer I can't seem to find a way to find the window's width through JavaScript. Can anyone plz tell me how to find it??

(and my last solution was to put it inside the <center></center>, but that works only when the <div> is part of the document layer, which doesn't make it float over the document and, so, still doesn't seem right!)

Anyway, can anyone show me how to find the window's width in Internet Explorer or better yet how to center a ''floating'' div?? Thx 4 all responsed!!




Replies:
Posted By: ljamal
Date Posted: 05 July 2003 at 7:28pm
why not use <div align=center> to center the div?

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: Kingherc
Date Posted: 05 July 2003 at 11:42pm
Well I think that works only for the text inside the <div>.


Posted By: Bluefrog
Date Posted: 09 July 2003 at 9:33am
Check out some DHTML sites. There are some scripts that have drop in DHTML DIVs that can be configured to do the same thing. i.e. google for "DHTML DIV drop in" or something like that.


Posted By: Flamewave
Date Posted: 14 July 2003 at 2:59pm

http://www.javascripter.net/faq/browserw.htm - http://www.javascripter.net/faq/browserw.htm

This will tell you how to get the window size and width using javascript. From there you can maybe use an absolute position to put the div directly in the center of the page. Although the easiest way of doing this would be to make a table and just use the standard align and size properties of the table to get it adjusted where you want it, and put the div inside of the table then.



-------------
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.


Posted By: Bluefrog
Date Posted: 14 July 2003 at 10:34pm

I saw this script used in one place today and remembered this thread:

<script language="Javascript"><!--

if (self.screen) {    
        width = screen.width
        height = screen.height
}

// for NN3 w/Java
else if (self.java) {  
       var javakit = java.awt.Toolkit.getDefaultToolkit();
       var scrsize = javakit.getScreenSize();      
       width = scrsize.width;
       height = scrsize.height;
}
else {

// N2, E3, N3 w/o Java (Opera and WebTV)
width = height = '?'
}

document.write("Your Screen Resolution is set to: "+ width +" x "+ height)
     
//-->
</script>

 




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net