Just like www.stackoverflow.com for example, only uses the centre of the page while retaining the header at the top. See my layout so far - it uses the whole screen whether its resized or maximised. So basically I want to retain the header at all window sizes, but use only the centre of the page for the other 3 divs.
-
1set width to container and margin: {0 auto} – Evgeniy Apr 03 '14 at 13:09
-
You need to wrap another div with class lets say wrapper around elements like header, left, right, footer and then in css put it as .wrapper{width:850px; /*desired width*/ margin:0 auto;} – Pravin W Apr 03 '14 at 13:10
-
my advice is to use bootstrap. – Govan Apr 03 '14 at 13:11
-
look up dynamic liquid layout http://www.dynamicdrive.com/style/layouts/category/C13/ – TheBetaProgrammer Apr 03 '14 at 13:12
-
possible duplicate of [How do you easily horizontally center ausing CSS?](http://stackoverflow.com/questions/618097/how-do-you-easily-horizontally-center-a-div-using-css)– T J Apr 03 '14 at 13:13
5 Answers
First thing you need is some wrapping element that "contains" the entire website. For example...
HTML
<body>
<div class="wrapper">
<!-- REST OF SITE HERE (OR ANYTHING YOU WANT CENTERED -->
</div><!-- END WRAPPER -->
</body>
Then some simple CSS... The wrapper element MUST have some width that you define, and a specific margin code. If the site is responsive, you want a dynamic width, with an additional max-width. In the code below, it is the margin: 0 auto;
that actually does the centering. It's important to note the width however. If you have 100% width, without a max-width also declared, then the "centering" isn't noticeable as the div takes up 100% of the width of whatever element contains it.
CSS
.wrapper {
width: 100%;
max-width: 960px;
margin: 0 auto;
}
If you don't want the entire website to be centered, simply change your HTML a little bit, and maybe your class name so that it makes more sense in the context that it is being used... For example, if you want just the content area to be centered...
HTML
<body>
<header></header>
<div class="content-wrapper">
<!-- ALL OF YOUR CONTENT HTML HERE -->
</div><!-- END CONTENT WRAPPER -->
<footer></footer>
</body>
CSS
.content-wrapper {
width: 100%;
max-width: 960px;
margin: 0 auto;
}

- 7,016
- 2
- 28
- 41
It's always a good practise to wrap your container in any kid of wrapper (say div) and then asssign css margin: 0 auto; so that it centrally alligns to the screen. Eg:
<div class="wrapper">
All the stuff in here
</div>
.wrapper{ margin: 0 auto; // Along with other reset style sheets }

- 4,986
- 4
- 27
- 45
I suggest you to use calc() in css. Differently from other solutions proposed by others, with the following code:
.wrapper
{
width:calc(100% - 50px);
margin:0 auto;
}
you can have wrapper div always centered and primarily flidly adaptable to Browser window width, leaving always the same amount of px to both margins. So if you change window width, it will be only the width of Wrapper that will change and not it's margins from sides.
UPDATE: For IE8 retro-compatibility, there are some js polyfills, like:

- 5,564
- 7
- 52
- 77
-
-
Sure, but support is increasing quickly ;-) please look at [Calc()](http://caniuse.com/#feat=calc) – Luca Detomi Apr 03 '14 at 14:05
-
Increasing quickly for Chrome, Firefox and other mobile browsers, sure. IE8 however will never support it natively, if IE8 is a requirement this is a pretty big issue. – Michael Apr 03 '14 at 14:25
-
You're right, but for IE8 exists js polyfills such as: * [Calc-Polyfill](https://github.com/closingtag/calc-polyfill) * [PolyCalc](https://github.com/CJKay/PolyCalc) – Luca Detomi Apr 03 '14 at 14:42
-
You can do a bad trick (but easy) with two divs (the second one inside the first one). The first one will have margin-left 50% and, only if you know the widht px of your page, the second div will have margin-left: -ZZZpx, being ZZZ the half of your width size.

- 820
- 1
- 7
- 23