views:

137

answers:

4

Hello everyone,

I am developing an html page on Windows platform. I find when the resolution (or size, in pixels) of browser (display) is larger than the page size, the page will be aligned to the left of the browser, and I want to align the page to the center (middle) of the browser when the page size is smaller than browser.

Any ideas how to implement this and how to find the root cause why aligned to left? The html page is big and not convenient to paste html code here.

thanks in advance, George

+1  A: 

Any ideas how to implement this and

http://dorward.me.uk/www/centre/

how to find the root cause why aligned to left?

… because that is the default.

David Dorward
I find the same issue for IE and Firefox, you mean default alignment is left for both IE and Firefox?
George2
In your recommended document, it mentioned how to make it center for a specific element, like <h1>, my question is how to make it center for the whole page?
George2
You wrap all the content in an element (such as a div) and centre it.
David Dorward
Thanks, question answered!
George2
A: 
<style>
body
{
    width:800px;
    margin:auto;
}
</style>
Dave
I did not find any code dealing with alignment in your code snippet?
George2
In CSS, setting the left and right margins of an element to “auto” causes that element to be centered within its containing block.
Paul D. Waite
Thanks! My html page contains a lot of master pages (legacy code) and I do not want to touch them too much. Could you recommend me a safe and efficient way to make the page align center without touching these master pages? :-)
George2
Some versions of Internet Explorer (7 and lower IIRC) will not allow you to set a width on the body element.
David Dorward
A: 

you simply need to add, text-align: center; to your body to cover legacy browsers and add:

text-align: left;
margin: 0 auto;

to your first container div ... this will be centered horizontally in all browsers, no matter how old.

Jonny Haynes
Why I need to add both text-align: center; and text-align: left;?
George2
text-align: center; on the body is to align everything on the page in the center ... because of this you need to overwrite it in your container div so that the default position of your images/body copy etc is left, as it would be naturally.
Jonny Haynes
1. You mean add "text-align: center" to body element and add "text-align: left;" to other div? 2. My html page contains a lot of master pages (legacy code) and I do not want to touch them too much. Could you recommend me a safe and efficient way to make the page align center without touching these master pages? :-)
George2
Yes that's what I mean ... there is no other way ... this is how you align a page. Sorry.
Jonny Haynes
It's wrong to down vote without stating why!
Jonny Haynes
+2  A: 

First of all, you'll want to wrap your page content in a block (this block will be centered):

<div id="body">
    <!-- your page content here -->
</div>

Then you'll want to style it as being centered. Due to a little disparity in how Firefox and IE handle centering a block, you'll have to do 2 things to center this block.

1. Set the body as centering everything (for IE):


body {
    text-align: center;
}

2. Set the left and right margins of your interior block as 'auto'; and

3. Since centering text inherits to its child nodes, you want to set it back to left-alignment (unless, you do want all your text to be centered.. blah!):


#body {
    margin: 0px auto;
    text-align: left;
    width: 800px; /* set this width to how wide you want your content to be */
}
BranTheMan