Webforumz's RSS FeedRSS Webforumz RegistrationRegister Contact Webforumz StaffContact

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.


 Subscribe in a reader

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

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Sep 9th, 2006, 03: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

*
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2  
Old Sep 9th, 2006, 09: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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 9th, 2006, 13:03
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,685
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Re: Centering a DIV

just add this to your div's css:
margin:0 auto;
__________________
Diego - Web-Developer & London SEO Expert
jQuery Plugins: Multiple File Upload, Star Rating, FCKEditor | NEW: XML to JSON
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 9th, 2006, 15: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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 9th, 2006, 19:05
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
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.
__________________

Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Sep 11th, 2006, 12:18
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,685
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
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>
__________________
Diego - Web-Developer & London SEO Expert
jQuery Plugins: Multiple File Upload, Star Rating, FCKEditor | NEW: XML to JSON
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

Last edited by spinal007; Sep 11th, 2006 at 12:21.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Sep 11th, 2006, 13:54
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
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.
__________________

Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Sep 11th, 2006, 14:09
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,685
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
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?
__________________
Diego - Web-Developer & London SEO Expert
jQuery Plugins: Multiple File Upload, Star Rating, FCKEditor | NEW: XML to JSON
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Sep 11th, 2006, 15:23
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
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;
__________________

Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Sep 12th, 2006, 10:18
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,685
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Re: Centering a DIV

Good point...
I'll just hope IE 5 is abolished soon enough... lol
__________________
Diego - Web-Developer & London SEO Expert
jQuery Plugins: Multiple File Upload, Star Rating, FCKEditor | NEW: XML to JSON
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Sep 12th, 2006, 14:02
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
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.
__________________

Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Sep 13th, 2006, 03:55
Ryan Fait's Avatar
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,787
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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Sep 13th, 2006, 14:07
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
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.
__________________

Last Blog Entry: Apps every Mac based web dev should consider (Jul 10th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
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 12:38
Centering a Page In CSS britishchampion Web Page Design 5 Jan 3rd, 2008 17:20
Centering website afowler Web Page Design 8 Oct 24th, 2007 19:59
centering help please RZX Developer Web Page Design 7 Aug 2nd, 2007 12:58
centering vertically benbacardi Web Page Design 17 May 27th, 2004 14:51