How to center a page without use of a table

This is a discussion on "How to center a page without use of a table" within the Web Page Design section. This forum, and the thread "How to center a page without use of a table 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 Jan 21st, 2007, 07:04
pdk pdk is offline
New Member
Join Date: Jan 2007
Location: ontairo
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
How to center a page without use of a table

hi, I'm sort of an off-and-on designer who does small pages mainly for friends and family, but I'm sort of learning CSS. I've got something started at

http://pdk.lolmaha.com/design

but I can't for the life of me figure out how to center it without the use of a table OR a container div as I've done in this.

if someone could tell me what i'm doing, it would be greatly appreciated
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 Jan 21st, 2007, 12:15
Elite Veteran
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

Links not working so can't see your code!
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 Jan 21st, 2007, 14:05
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

Your combination of css styling code and the use of a container div is just fine.

Get into the habit of placing your css in a seperate file and <link... it into your pages.
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 Jan 21st, 2007, 19:03
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: How to center a page without use of a table

Something has to be fixed width, but you can add margin: 0 auto; to an element.
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 Jan 22nd, 2007, 13:18
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

How to center your site horizontally

Last edited by karinne; Jan 22nd, 2007 at 13:32.
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 Jan 22nd, 2007, 15:18
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
Re: How to center a page without use of a table

also text-align:center; the body and text-align:left; your container. IE7 doesn't seem to like margin:0 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
  #7  
Old Jan 22nd, 2007, 17:05
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

Quote:
Originally Posted by moojoo View Post
also text-align:center; the body and text-align:left; your container. IE7 doesn't seem to like margin:0 auto;
Hmm ... that's news to me ... I'm pretty sure only IE5 and IE5.5 possibly are the only ones that need the text-align:center; ... if you look at my portfolio site in IE7, it centers correctly and I don't have the text-align:center; anywhere in there.
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 Jan 22nd, 2007, 18:03
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: How to center a page without use of a table

I heard that too, perhaps it was on one of the beta versions?
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 Jan 22nd, 2007, 19:02
New Member
Join Date: Jul 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

quick reply to you dude

use margin-left:auto;
and margin-right:auto to centre your divs
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 Jan 22nd, 2007, 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: How to center a page without use of a table

margin:0 auto is less code and works the same

Quote:
Originally Posted by barry View Post
quick reply to you dude

use margin-left:auto;
and margin-right:auto to centre your divs
__________________

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
  #11  
Old Jan 22nd, 2007, 19:06
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
Re: How to center a page without use of a table

I test in IE 7 under Parallels and on IE 7 at work both XP Pro. Neither one supports it for me, perhaps I just need to update it. Will run sw update at home and see.

Quote:
Originally Posted by Ryan Fait View Post
I heard that too, perhaps it was on one of the beta versions?
__________________

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 Jan 22nd, 2007, 22:37
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: How to center a page without use of a table

Yep, that's good to know!
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 Jan 23rd, 2007, 00:26
Elite Veteran
Join Date: Jan 2007
Location: You know where
Age: 31
Posts: 4,617
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

The code I posted in my first reply, you'll notice I don't have the text-align: center in the body { }. Only margin: 0 auto; and a width in the #wrap.

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-CA" xml:lang="EN-CA">

<head>
    <title>Some title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
    <style type="text/css">
    body {
        color: #fff;
    }
    
    #wrap {
        margin: 0 auto;
        width: 780px;
        background-color: #900;
    }
    </style>
</head>

<body>

<div id="wrap">
    this box is centered
</div>

</body>
</html>
This works in FF2, Opera 9 and IE6 and IE7.

You can test it here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Jan 23rd, 2007, 15:30
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
Re: How to center a page without use of a table

Yes centering only works with a width value specified. Good point =P.
__________________

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
  #15  
Old Jan 30th, 2007, 20:56
pdk pdk is offline
New Member
Join Date: Jan 2007
Location: ontairo
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to center a page without use of a table

Okay, so I've solved that problem. How do I make column1 and column2 the same length no matter what?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Jan 30th, 2007, 21:01
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: How to center a page without use of a table

Unforunately you can't.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #17  
Old Jan 30th, 2007, 21: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: How to center a page without use of a table

A bit of stuff I noticed:

Change this code:

Code: Select all

<P>HEADERHEADERHEADER
<P>HEADERHEADERHEADER
</div>            
<div id='column1'>
<li><a href='POKER'>OMAHA</a></li>
<li><a href='POKER'>STUD</a></li>
<li><a href='POKER'>LEE JONES</a></li>
<li><a href='POKER'>TEXAS FOLD THEM</a></li>
<li><a href='POKER'>POKER</a></li>
</div>
<div id='column2'>
this for news 
</div>
<div id='footer'>
this the footer
</div>
to:

Code: Select all

<h1>HEADERHEADERHEADER</h1>
<h1>HEADERHEADERHEADER</h1>
</div>            
<div id="column1">
<ul>
<li><a href="POKER">OMAHA</a></li>
<li><a href="POKER">STUD</a></li>
<li><a href="POKER">LEE JONES</a></li>
<li><a href="POKER">TEXAS FOLD THEM</a></li>
<li><a href="POKER">POKER</a></li>
</ul>
</div>
<!-- end column1 -->
<div id="column2">
<p>this for news</p>
</div>
<!-- end column2 -->
<div id="footer">
<p>this the footer</p>
</div>
<!-- end footer -->
__________________

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
  #18  
Old Jan 30th, 2007, 21:06
Elite Veteran
Join Date: Aug 2005
Location: That Place
Posts: 2,044
Blog Entries: 1
Thanks: 0
Thanked 37 Times in 37 Posts
Re: How to center a page without use of a table

Quote:
Originally Posted by Ryan Fait View Post
Unforunately you can't.
Well you can in ways..

1. Simulate the columns by using a background image repeating on the y axis of your container so a border whatever appears the entire height of the containing div or even on the body. Called "Faux Columns".

2. Specify them both the same height and use the over flow auto property

3. Give both the same amount of content

4. (This one is not recommended) use javascript to control them.
__________________

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