Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,589 Programmers for FREE! Ask your question and get quick answers from experts. There are 826 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Align your page to the center of the browser window

2 Pages V  1 2 >  
Reply to this topicStart new topic

> Align your page to the center of the browser window, with css

Rating  5
xfodder
Group Icon



post 30 Oct, 2006 - 12:25 AM
Post #1


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 !

This post has been edited by xfodder: 30 Oct, 2006 - 11:10 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Tuzoid
Group Icon



post 12 Nov, 2006 - 03:41 AM
Post #2
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
Go to the top of the page
+Quote Post

xfodder
Group Icon



post 2 Jan, 2007 - 10:12 AM
Post #3
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
Go to the top of the page
+Quote Post

Niemi
*



post 14 Mar, 2007 - 02:38 PM
Post #4
Quite right, but it only centers the DIV horizontally, not vertically.
Go to the top of the page
+Quote Post

xfodder
Group Icon



post 30 Mar, 2007 - 08:28 AM
Post #5
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
Go to the top of the page
+Quote Post

AndyBo
*



post 2 May, 2007 - 09:03 AM
Post #6
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

This post has been edited by AndyBo: 2 May, 2007 - 09:06 AM
Go to the top of the page
+Quote Post

WhiteFang
*



post 21 Apr, 2008 - 07:34 AM
Post #7
extremely helpful mate - my blog now has no flaws (i hope)
Go to the top of the page
+Quote Post

jbharding
*



post 11 May, 2008 - 10:10 AM
Post #8
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?


This post has been edited by jbharding: 11 May, 2008 - 10:11 AM
Go to the top of the page
+Quote Post

capty99
Group Icon



post 11 May, 2008 - 10:28 AM
Post #9
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.
Go to the top of the page
+Quote Post

jbharding
*



post 11 May, 2008 - 10:37 AM
Post #10
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!!!
Go to the top of the page
+Quote Post

Phantom
*



post 14 May, 2008 - 02:36 AM
Post #11
Doesn't seem to work in IE 7
Go to the top of the page
+Quote Post

jbharding
*



post 15 May, 2008 - 11:01 AM
Post #12
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
Go to the top of the page
+Quote Post


2 Pages V  1 2 >
Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/11/08 07:37PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month