Full Version: Align your page to the center of the browser window
Dream.In.Code > Programming Tutorials > CSS Tutorials
xfodder
Before reading this tutorial, please read the CSS beginners tutorial

You may have seen sites where the page content will always stay in the center of the browser window no matter the size (www.csnation.net for example).

This is very easily achieved with css here is how:

Firstly create a div in your html code and give it an id, something useful like wrapper (since we want to incapsulate your content inside this div).

CODE

<html>
<head>
    
    <title>untitled</title>
    <link rel="stylesheet" href="css/style.css" type="text/css" />

</head>

<body>
    
           <div id="wrapper"></div>

</body>
</html>


Next define a style in a css document for the wrapper div, make sure you set the margin to 0 auto , (the width can be of your choice)
CODE

#wrapper {
    margin: 0 auto;
    width: 700px;
}


Now create a new div INSIDE the wrapper div, name it something useful like content
CODE

<html>
<head>
    
    <title>untitled</title>
    <link rel="stylesheet" href="css/style.css" type="text/css" />

</head>

<body>
    
           <div id="wrapper">

                 <div id="content"> Content Here </div>

           </div>

</body>
</html>


Next define a new style for the content div in your css document, (the width can be of your choice)
CODE

#wrapper {
    margin: 0 auto;
    width: 700px;
}
#content {
    width: 100%;
}


Now make sure all your content (including other div's) goes inside the content div, your content will now always be in the middle of the browser window !
Tuzoid
I'm not sure if these are the same, but I've always used:
margin-left:auto;
margin-right:auto;


May just be me though smile.gif
xfodder
QUOTE(Tuzoid @ 12 Nov, 2006 - 07:41 PM) *

I'm not sure if these are the same, but I've always used:
margin-left:auto;
margin-right:auto;


May just be me though smile.gif


well actually it is the same but using 'margin: 0 auto' is just a quicker way to set the left and right margins to auto smile.gif
Niemi
Quite right, but it only centers the DIV horizontally, not vertically.
xfodder
QUOTE(Niemi @ 15 Mar, 2007 - 07:38 AM) *

Quite right, but it only centers the DIV horizontally, not vertically.

thats true, im unsure of how to center DIV's vertically ... still finding my feet smile.gif
AndyBo
QUOTE(xfodder @ 30 Mar, 2007 - 08:28 AM) *

QUOTE(Niemi @ 15 Mar, 2007 - 07:38 AM) *

Quite right, but it only centers the DIV horizontally, not vertically.

thats true, im unsure of how to center DIV's vertically ... still finding my feet smile.gif



Also for some strange reason when applied a border to outer wrapper in IE is ok when wrapper hight set to "blank", in Mozilla its not showing around that inner cell. When I add some numbers to hight it does show the border around whole thing in Mozilla.
Any suggestions? blink.gif
WhiteFang
extremely helpful mate - my blog now has no flaws (i hope)
jbharding
All well and good, and it works! Thanks! My webpages are centered.

Here is the end result: IDKPress Website

BUT I want to add color, or design to the left and right margins. I believe this is possible with an outer wrapper. The problem is, how does the outer wrapper get the screen size?
capty99
jb , you set the outer wrapper to 100% if you want to do it like that.

or you can just use a backgroundimage...

but if you really want content out there you should look at a three-column layout with the center being a static size and the outsides being flexible.
jbharding
QUOTE(capty99 @ 11 May, 2008 - 10:28 AM) *

jb , you set the outer wrapper to 100% if you want to do it like that.

or you can just use a backgroundimage...

but if you really want content out there you should look at a three-column layout with the center being a static size and the outsides being flexible.



Thanks! I get it - extremely helpful!!!
Phantom
Doesn't seem to work in IE 7
jbharding
QUOTE(jbharding @ 11 May, 2008 - 10:37 AM) *

QUOTE(capty99 @ 11 May, 2008 - 10:28 AM) *

jb , you set the outer wrapper to 100% if you want to do it like that.

or you can just use a backgroundimage...

but if you really want content out there you should look at a three-column layout with the center being a static size and the outsides being flexible.



Thanks! I get it - extremely helpful!!!


I am currently using Adobe Golive, but am considering a switch to Dreamweaver, which handles CSS well, or maybe Joomla. Which would you pick?
Thanks
nickmorss
I have developed a more elegant solution. it only requires one Div and is fully browser compatible. Check it out here http://pistachiomonkey.com/blog/archives/133
seanmac
QUOTE(nickmorss @ 24 Jul, 2008 - 04:01 AM) *

I have developed a more elegant solution. it only requires one Div and is fully browser compatible. Check it out here <a href="http://pistachiomonkey.com/blog/archives/133" target="_blank">http://pistachiomonkey.com/blog/archives/133</a>


Hi,
I went to look at your link:
"http://pistachiomonkey.com/blog/archives/133"
but got a 404 page missing... Any chance I can still find this info somewhere?
Cheers
Sean
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.