CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignCSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 13th, 2003, 12:57 PM
liquidspaces liquidspaces is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 5 liquidspaces User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Centering an entire page in CSS?

I have every single component of my web page positioned absolutely with CSS. This is fine for most "standard" resolutions, but on high resolutions it looks really stupid having everything on the left hand side of the page.

Most major websites manage to have their content centered on the page. How can I do this with CSS since I can't just use a <center> as everything is divided into <div class="">?

Reply With Quote
  #2  
Old May 13th, 2003, 01:27 PM
ErikSean ErikSean is offline
Perl Ronin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 152 ErikSean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Here's a not too difficult but kinda ugly solution. Love to see if anyone has a slicker way of doing it...

The basic principal is creating a containing DIV that defaults to the width of the browser, and centerss all content with it. Then, create a "content div" that is the parent of the DIV's that really position your content.

The "2nd Level Div" (the one the contains your content" should be positioned relative, and then no values entered. Here's an example you can cut and paste into a text doc and put in your browser to understand. I just wish when you position stuff, containing DIV's got taller ... *shrugs*

Code:

<HTML>
<HEAD>

	<STYLE TYPE='text/css'>
	
	body	   { margin:0px; }
	#fullPage  { border:2px red solid; }
	#enclosing { align:center; width:400px; height:auto; border:1px black solid; position:relative }
	
	#somecontent { position:relative; top:0px; left:0px }
	#morecontent { background-color:red; top:20px; left:100px; width:76px; height:76px }
	
	</STYLE>
	
</HEAD>


<BODY>
<DIV ID="fullPage" align='CENTER'>
<DIV ID="enclosing" align='LEFT'>




	<!--- Your *actual* page starts here --->
	<DIV ID="somecontent">
	Hi there! How are you today?
	</DIV>


	<DIV ID="morecontent">
	&nbsp;
	</DIV>







</DIV>
</DIV>
</BODY>
</HTML>


You can even "center the page off-center" by assigning value #enclosing for left pixels ... it will then center that # of pixels off to the right. Just give #enclosing a left value and play with it if you don't understand me.

# Erik


PS: Above HTML checked & verified in IE5.1/Mac

Last edited by ErikSean : May 13th, 2003 at 01:48 PM.

Reply With Quote
  #3  
Old May 13th, 2003, 01:39 PM
liquidspaces liquidspaces is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 5 liquidspaces User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks, I'll give it a shot. It seems that this is indeed an ugly situation, but I'll try to work through it. I wasn't even aware that there was a way to use nested <div> tags.

Thanks for your help,
Kevin

Reply With Quote
  #4  
Old May 13th, 2003, 01:57 PM
jharnois's Avatar
jharnois jharnois is offline
mod_dev_shed
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Sep 2002
Location: Atlanta, GA
Posts: 14,299 jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level)jharnois User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 Days 1 h 54 m 17 sec
Reputation Power: 760
Create a conatiner div and set the margins to auto. Sounds like you used set widths?
Code:
<style>
body
  {
  text-align: center /* required by some versions of IE -- but not proper */
  }
#all
  {
  width: 750px; /* whatever your site width is */
  margin-left: auto;
  margin-right: auto;
  }
</style>

<div id="all"><!-- positioning div centers content -->
  <!-- all other content divs -->
</div>

Reply With Quote
  #5  
Old May 13th, 2003, 02:13 PM
liquidspaces liquidspaces is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 5 liquidspaces User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I use set widths about 50% of the time, it just depends on what I'm formatting. Usually just on text and backgrounds.

I'll try what you suggested and will let you know if I have any problems.

Reply With Quote
  #6  
Old May 13th, 2003, 03:48 PM
liquidspaces liquidspaces is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 5 liquidspaces User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ErikSean:
Thanks! After quite a bit of tinkering around, I finally got it to work. Once again thank you, I really appreciate it. Awesome.

jharnois:
Thanks to you as well. It didn't do quite what I wanted it to do, but I saved it to my code snippet journal and am sure that it will come in very helpful another time.

Thanks again,
Kevin

Reply With Quote
  #7  
Old October 29th, 2003, 05:24 AM
bquinn bquinn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 bquinn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 13 sec
Reputation Power: 0
centering a page with CSS

ok so ive tried tinkering with nested divs and margins and whatnot and managed to get the page centered. however then i found it was necessary to use relative positioning instead of absolute...fair enough. but the problem then occurs that after placing an image on the page, the position of the "containing block" moves down to the space below the image even though technically the containing block is still the div that contains both images.
so i found that with negative position values I can correct this and get two images on the same horizontal space but this doesnt seem to correspond to what is dictated in the W3C guidelines which makes me worry that it wont be so cross browser compatible.
Can anyone shed some light on this situation for me?
Thanks,
Brian

Reply With Quote
  #8  
Old October 29th, 2003, 09:03 AM
stinkoman's Avatar
stinkoman stinkoman is offline
what's your moniker?
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Highland Park, NJ
Posts: 201 stinkoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to stinkoman
Quote:
Originally posted by ErikSean
The "2nd Level Div" (the one the contains your content" should be positioned relative


Sadly, I think that's the only way to do it, and there's a problem when you start z-indexing divs. I'm pretty sure to use z-index, you need to use absolute positioning (which is stupid).

here's an article about the whole thing... http://www.mezzoblue.com/tests/centered-css/ pretty good stuff.
__________________
new jersey web design

Reply With Quote
  #9  
Old October 30th, 2003, 07:56 AM
bquinn bquinn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 bquinn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 13 sec
Reputation Power: 0
CSS centering the page

yeah that article covers it pretty clearly. thanks for that.
though while reading it, i couldnt help feeling that tables sound more appealing...
i got the impression upon reading the W3C CSS2 details that "absolute" refers not to the entire page but to the next ouitermost containing block adn that relative positioning was either meant for position changes or perhaps position relative to the cursor. if this is the case then it seems that more browser compatibility issues are on the way.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignCSS Help > Centering an entire page in CSS?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway