tags:

views:

475

answers:

3

Hello,

I am attempting to center my entire page using only CSS and it is proving more complicated than i first expected. Currently my code works in IE but not in Firefox which makes a change. The page can be seen here. Below is the code portion involved:

#wrap {
 width: 960px;
 margin: 0 auto;
 padding: 6px;
 background: #FFFFFF;
 }

The structure of my HTML is:

<body>
<div id="wrap">
    Gubbins in here.
</div>
</body>

It seems that in Firefox everything following the wrap div is be created outside of it. This is problem is resolved if i add a 'float: left' to the wrap div but then obviously everything floats left rather than center.

Any help would be greatly appreciated.

+5  A: 

Change your markup to

<body>
<div id="wrap">
    Gubbins in here.
</div>
</body>

EDIT: Looking at the link, you've already done that. You'll want to either add overflow:auto; to #wrap or add a clearing div at the end just before the closing tag on the wrap div.

Also, on your example page, the wrap div is missing its closing tag.

John Sheehan
Sorry my mistake my markup is already how you have stated.
Ronnie
Actually looking again, on your example you're closing your div on the line that says </div> <!-- / navigation --> your navigation UL is already closed, so this div is closing the wrap. move that closing tag to the end
John Sheehan
A: 

Use this CSS:

body { text-align:center;}
#wrap {text-align:left; margin: 0 auto; width:960px;}

Then, let's examine this statement from your question:

everything following the wrap div is be created outside of it

That's kind of the way it works. Don't put anything outside of your wrap div. Think of it as a surrogate body.

Joel Coehoorn
I thought text-align:center; does not work for anything other than text in FF?
jmein
margin: 0 auto; will work great for him once his markup errors are fixed
John Sheehan
It's for IE, not firefox, to fix an IE css rendering glitch when trying to center content.
Joel Coehoorn
but he said that his code works in IE but not in FF
jmein
I have corrected the markup errors that have been pointed out. Yet i still had the problem. However after adding overflow: auto to wrap, everything is fine. Thank you.
Ronnie
Ah, but which version of IE?
Joel Coehoorn
IE6+ handles margin: 0 auto; without issue.
John Sheehan
that is good to know. never done it like that before
jmein
It now works in Firefox 3 and IE 7.
Ronnie
A: 

If you know the width of your page - and it's fixed, you can use the following methodology.

  • Contain your page content with a div (which will act as a wrapper)
  • Give this 'wrapper' div a width of 'W'
  • Position the wrapper div using 'left: 50%;'

now, utilising the fact that it's possible to have a negative margin...

  • Pull back the positioning of the wrapper div using 'margin-left: -(W/2);'
codeinthehole