Centering a DIV

This is a discussion on "Centering a DIV" within the Web Page Design section. This forum, and the thread "Centering a DIV are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 9th, 2006, 02:24
New Member
Join Date: Sep 2006
Location: denver
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Centering a DIV

I am building an interface where I have a DIV that gets made visible when you roll over a certain area. The div contains some nav elements, which are jscript rollovers themselves. I've got the code working to hide and show the DIV and to do the rollovers (I've only done the one in the upper left). Here is what it looks like so far:

http://www.artntech.com/sample/index.html

The problem is, I really want the design to be centered, and since I am using absolute positioning for the DIV, this creates a problem. Here is what it looks like when I center the underlying table:

http://www.artntech.com/sample/index2.html

I'm pretty new to all his, and am stumped as to how to get that DIV element to center and align with the underlying table (since it is centered, I can't really predict exactly where it will be in the window).

I've seen articles saying this kind of thing can be one with CSS and have tried several of the samples I've seen with no luck. Anyone know of a good way to do this? Any help greatly appreciated.

Thanks

Johnmr

*
Reply With Quote

  #2 (permalink)  
Old Sep 9th, 2006, 08:02
Junior Member
Join Date: May 2006
Location: in a house
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering a DIV

1st add this to your body elemant in your style sheet:
body {text-align: center;}

2nd surround everything inside the body tag with <div class="global"> tag
then add the following to your CSS. that should do the trick.

div.global {
position: relative;
margin-left: auto;
margin-right: auto;
width: "what ever the width of the content is" px;
}
Reply With Quote
  #3 (permalink)  
Old Sep 9th, 2006, 12:03
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,619
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Centering a DIV

just add this to your div's css:
margin:0 auto;
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
  #4 (permalink)  
Old Sep 9th, 2006, 14:23
New Member
Join Date: Sep 2006
Location: denver
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering a DIV

thanks very much - I'll try this out and let you know what happens
Reply With Quote
  #5 (permalink)  
Old Sep 9th, 2006, 18:05
moojoo's Avatar
Moderator
Join Date: Aug 2005
Location: Texas
Age: 31
Posts: 1,737
Blog Entries: 1
Thanks: 0
Thanked 16 Times in 16 Posts
Send a message via AIM to moojoo Send a message via MSN to moojoo Send a message via Yahoo to moojoo
Re: Centering a DIV

I don't see the need for absolute positioning on this but eh. What do I know. Since all 4 areas are contained within a containing div wouldn't relative positioning work? I may be wrong.
__________________
The internet is just a fad.
http://www.mevans76.com
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Reply With Quote
  #6 (permalink)  
Old Sep 11th, 2006, 11:18
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,619
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Centering a DIV

it's soooooooooo simple:
margin:0 auto;

Code: Select all
<style>
#Container{
 width: 780px;
 margin:0 auto;
}
#Row1{ font-size:200%; padding:50px 0px; color:white; background:green; }
#Row2{ font-size:200%; padding:50px 0px; color:white; background:red; }
#Row3{ font-size:200%; padding:50px 0px; color:white; background:blue; }
</style>

<div id="Container">
 <div id="Row1">
  This is row 1
 </div>
 <div id="Row2">
  This is row 2
 </div>
 <div id="Row3">
  This is row 3
 </div>
</div>
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

Last edited by spinal007; Sep 11th, 2006 at 11:21.
Reply With Quote
  #7 (permalink)  
Old Sep 11th, 2006, 12:54
moojoo's Avatar
Moderator
Join Date: Aug 2005
Location: Texas
Age: 31
Posts: 1,737
Blog Entries: 1
Thanks: 0
Thanked 16 Times in 16 Posts
Send a message via AIM to moojoo Send a message via MSN to moojoo Send a message via Yahoo to moojoo
Re: Centering a DIV

Even text-align: center; on the body would work. But I think there are issues preventing that, I need to look and see how he actually centered it.
__________________
The internet is just a fad.
http://www.mevans76.com
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Reply With Quote
  #8 (permalink)  
Old Sep 11th, 2006, 13:09
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,619
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Centering a DIV

the problem with text-align:center is that you have to set text-align:left/right for all underlying elements...
also, text-align:center is for positioning/aligning text, not elements.

PS.: Moojoo: what's a noob?
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
  #9 (permalink)  
Old Sep 11th, 2006, 14:23
moojoo's Avatar
Moderator
Join Date: Aug 2005
Location: Texas
Age: 31
Posts: 1,737
Blog Entries: 1
Thanks: 0
Thanked 16 Times in 16 Posts
Send a message via AIM to moojoo Send a message via MSN to moojoo Send a message via Yahoo to moojoo
Re: Centering a DIV

Actually you only have to set text-align:left on the container div. Everything in there will follow suit. You need to do that for IE 5.x anyway since IE 5 doesn't know margin: auto;
__________________
The internet is just a fad.
http://www.mevans76.com
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Reply With Quote
  #10 (permalink)  
Old Sep 12th, 2006, 09:18
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,619
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Centering a DIV

Good point...
I'll just hope IE 5 is abolished soon enough... lol
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
  #11 (permalink)  
Old Sep 12th, 2006, 13:02
moojoo's Avatar
Moderator
Join Date: Aug 2005
Location: Texas
Age: 31
Posts: 1,737
Blog Entries: 1
Thanks: 0
Thanked 16 Times in 16 Posts
Send a message via AIM to moojoo Send a message via MSN to moojoo Send a message via Yahoo to moojoo
Re: Centering a DIV

Wishful thinking heh. It will be a long time before everyone gets on Vista, and XP. although I hear IE 7 will be a pushed update to XP so.. lets hope. Too many people still on Win 98/Me/2k.. XP has been around for 6 years now? And I would guess that only 60 - 70% have upgraded to XP. And that may be a generous set of numbers.
__________________
The internet is just a fad.
http://www.mevans76.com
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Reply With Quote
  #12 (permalink)  
Old Sep 13th, 2006, 02:55
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Centering a DIV

Here's to hoping Vista is as good as Tiger. Not that I'll ever switch back to Windows...
Reply With Quote
  #13 (permalink)  
Old Sep 13th, 2006, 13:07
moojoo's Avatar
Moderator
Join Date: Aug 2005
Location: Texas
Age: 31
Posts: 1,737
Blog Entries: 1
Thanks: 0
Thanked 16 Times in 16 Posts
Send a message via AIM to moojoo Send a message via MSN to moojoo Send a message via Yahoo to moojoo
Re: Centering a DIV

Vista will be close, half of its appearance and functions are a straight up rip of OS X. Right down to the rounded input fields. And their window management is a direct rip of Looking Glass by SUN. Muhahaha. No worries though, Vista may be as good as Tiger or close but leopard will be released first and no way will Vista even compare to Leopard. I however will happily purchase Vista and run it on my Macbook Pro.
__________________
The internet is just a fad.
http://www.mevans76.com
Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Reply With Quote
Reply

Tags
centering, div

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Vertical centering - please help! mpnuttall Web Page Design 6 Apr 16th, 2008 11:38
Centering a Page In CSS britishchampion Web Page Design 5 Jan 3rd, 2008 16:20
Centering website afowler Web Page Design 8 Oct 24th, 2007 18:59
centering help please RZX Developer Web Page Design 7 Aug 2nd, 2007 11:58
centering vertically benbacardi Web Page Design 17 May 27th, 2004 13:51


All times are GMT. The time now is 19:19.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43