views:

203

answers:

7

I want to "centerize" the text and contents of my webpage. Now I don't want to align the text to center, I still want a left alignment but I want significant margins on the left and right so that everything looks relatively center-ish. Can you show me the HTML/CSS to achieve this? THanks.

+2  A: 
#wrapper {
    width: 800px;
    margin: 0 auto; 
 }

<div id="wrapper">
     <p>This will appear in a centered container</p>
</div>
meep
In some browsers, like IE for example, this will not work. You will have to add another div wrapping #wrapper that sets text-align to center. In #wrapper you should then set text-align to left again.
Kevin D.
I have never experienced that problem? I have used that bit of code on every webpage I have made. With XHTML doctype. If you see: http://dev.korebogen.dk - Its centered in all browsers. And the CSS is only #wrapper { width: 960px; margin: 0 auto; }
meep
Alright - maybe it will not center in IE5 - but hell, whos using that anyway? :-)
meep
+1  A: 

I believe this might help you.

George Stocker
A: 

CSS:

#container {
    max-width: 800px;
    margin: 0 auto;
}

And in the HTML, wrap everything in:

<div id='container'>
...
</div>

(Note that this answer differs from meep's in that I'm using max-width to give a fluid layout below 800 pixels, whereas he's using width to give a fixed layout.)

RichieHindle
...Except most versions of IE don't know what 'max-width' is.
Cory Larson
IE7 and above do. For IE6, here's an excellent article on how to achieve the same thing: http://javascript.about.com/library/blwidth.htm
RichieHindle
+2  A: 

Create 3 columns on your page. All your text goes in the center column and can be left alligned.

Have a look here for examples http://matthewjamestaylor.com/blog/perfect-3-column.htm

Glen
A: 

try

#div {
  margin:0 auto
};
dasha salo
+8  A: 

body {
   text-align: center; /* Center in IE */
}

#content {
   text-align: left; /* reset text-align for IE */
   margin: 0 auto; /* Center in other browsers */
   width: 800px;
}

html {
   overflow: -moz-scrollbars-vertical; /* Force vertical scrollbar in FF */
}

</head>
<body>
<div id="content">

   content here

</div>
</body>
</html>

*UPDATE: I added some CSS that forces a vertical scrollbar in FF as per some comments below.

Cory Larson
It's better to put the text-align: center; on another containing div in case you're using a WYSIWYG editor that will center all your text in the editor.
Steve Perks
+1 because I agree -- I was just giving the simplest example I could for the asker, but in reality I would do exactly as you suggest.
Cory Larson
This works well, but there is a slight problem. I've applied it across all my pages and it seems that for some pages they move slightly to the left/right. This is distracting when my menubar is moving from page to page.
alamodey
It seems like the pages with contents/text that can fill the width of the page will have a center that is more "left" than other pages that are almost empty.
alamodey
Does it seem like about 30 pixels shift? Could be because of the scrollbar appearing and disappearing in FF when the content stretches beyond the page.
Cory Larson
Yes, that's what it is! Is there a way of correcting that? In other words set my page to start at a certain number of pixels from the left of the screen (regardless of whether or not there is a scrollbar).
alamodey
I updated my answer. Note that IE always shows the scrollbar's trackbar so there's no shift, so this fix is ONLY for FF, and I only tested it in 3.0.
Cory Larson
A: 

Have a container div within which you put all your content:

<html>
<head>
    <title>a sample</title>
</head>
<body>
    <div id="container">
         <h1>this is it</h1>
         <p>all content goes here</p>
    </div>
</body>

Then add some css specifying the width and margins of your container div:

#container {
    width: 750px;
    margin: 0 auto;
}
Tomas Lycken