Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

TUTORIAL: how to center a site using css

Featured Replies

Thanx Dizi for very cool explanation, this was uneasy but I managed to do this! Thanx a lot!

  • 1 month later...

ok, Dizi centering site is very simple BUT, what if you wanted the height of the element change as content starts to exceed the height of that element.

 

it's wierd how the browser minds work but all you have to do is add a float to the element... well no, the centering won't work then. So you have to add a div inside that, the same width maybe some padding (remember less width when adding padding) then add a float: left to it, here is an example:

 

THE CSS

 


#wrapper { width: 900px; margin: 0px auto; } /* Centers Content */
#container { width: 880px; padding: 0px 10px; float: left; background: #FFF; } /* Creates an AutoHeight */

 

THE HTML

 


<div id="wrapper">
<div id="container">
<!-- All Content Goes Inside Here -->
</div>
</div>

 

Floating is very tricky, but can be handy in cases like this.

Something to add, you don't need this:

 


body { text-align: center; }

 

This only centers text, never centers divs. Hope this was a handy extra to Dizi's technique.

  • 2 weeks later...

THANK YOU!!! :D I've been looking for ages how to do this, and this is the first thing I've found that actually works!

  • Author
body { text-align: center; }

 

This only centers text, never centers divs. Hope this was a handy extra to Dizi's technique.

 

 

Actually as I explained, that is needed for older browsers that are still used. As some older browsers will not centre a container with margin 0 auto, so the text-align fixes this as it is a strange quirk where these browsers will see text-align: center and will centre everything....so if you want the site to centre across as many browsers as possible, then keep it in :)

  • 2 weeks later...

I do the following:

 

* {
		 margin: 0 auto;	  - New Browsers mostly [Firefox yumm]
		 padding: 0;
}

body {
		text-align: center;	- For Older Browsers [IE yuck]
}

i generally just create a container and make the margin auto. seems to work fine?

  • 2 weeks later...

Summarising how to center a page in the viewing window (viewport):-

 

The first post on this topic said:-

#wrapper {

margin: 0 auto;

width: xxxx; /* Replace the xxxx with the the width of your site (eg 800px)*/

text-align:left;

}

 

This is fine unless any elements inside the #wrapper are position: absolute as position: absolute elements are taken out of the normal flow and will take their top and left positions from the top left corner of the immediate parent, normally the body, so they remain fixed to the top left corner of the window.

 

If you add position: relative to #wrapper, any position: absolute elements inside will then take top and left positions from the top left corner of the #wrapper so that if the #wrapper moves to center in any window resolution, the position: absolute elements will move with it, retaining their positions inside. A parent element without position: relative does not have this effect on position: absolute elements. So a safer code is:-

 

#wrapper {

margin: 0 auto; position: relative;

width: xxxx; /* Replace the xxxx with the the width of your site (eg 770px)*/

text-align:left;

}

 

I would suggest 770px instead of 800px as an example so that there is space for a vertical scrollbar in older screen resolutions of 800*600px and it won't create a horizontal scrollbar.

 

The other main method of centering a page is:-

 

#wrapper {

position: absolute; left: 50%; width: 770px; margin-left: -385px;

}

 

where the margin-left is always negative half the div width. This works well for small centralised divs like a small splash photo link but is unreliable for divs or images with a large width. It works well centering at higher window resolutions but at lower resolutions which are less wide than the div the negative margin-left takes the left edge out to the left beyond the screen edge. There is no way to scroll to reach the content beyond the left side.

  • 1 month later...

This is really helpful, glad it's pinned.

I create a container and use,

 

position:relative;

margin-left:auto;

margin-right:auto;

 

Seems to work fine for me and doesn't seem to be any problems with different browsers.

  • 4 weeks later...

Hi I usually just add this to the wrapper or container div:

 

</head>

 

<body>

<div align="center" id="wrapper">

//everything inside the wrapper

</div>

 

 

I did try it the css way (auto, 0) but in some browsers it didn't center, so I looked at the source code on other websites and found this worked best for me.

align="center" is depreciated HTML attribute. HTML should not include any layout elements/properties. CSS should take care of that.

  • 2 weeks later...

Sorry for bumping this topic, but you can also achieve this by just giving the body tag a css command. One draw back is that everything will be auto aligned.

body {
width: xxxx;
margin:0px auto;
position: relative;
}

  • 3 weeks later...

Heres my 10pence worth

 

horizontally and vertically center a DIV

 

 

.wrapper_div {

position: absolute;

width:500px;

height: 200px;

top: 50%;

left: 50%;

margin-left: -250px; /* should be half the width px */

margin-top: -100px; /* should be half the height px */

}

  • Author

Problem with that one is if the browser window is too small for the content the content goes off the top of the screen.

 

I wrote a short tutorial on how to vertically center a site without this happening it can be found here:

 

http://webdesignpond.co.uk/wdp/tips-tutorials/how-to-vertically-center-a-site/

  • 1 month later...
  • 2 months later...

Can you tell me what's wrong with this code?

I'm trying to centre my menu but when I do float: left it get's rid of my bg color...:

 

#wrapper {
background-color: #83e36f;
width: 750px;
margin: 0 auto;
text-align:left;
}


#menu ul {
list-style: none;
padding: 0;
margin: 0;
}

#menu li {
float: left;
margin: 0 0.15em;
}

  • 3 months later...
  • 1 month later...
  • 5 weeks later...

This won't work if you want other parts of your site to span across the whole page.

 

For example you want your header to span the full width of a page and then the rest of your content to be central. As applying a width to the body element will mean that everything on the page will only span that width you have set. So setting your header width to 100% when you have set the body width to maybe 700px the header will only span 100% of the that 700px width and not 100% of the page.

 

 

So only use this technique if you want the entire site to be a fixed width :)

 

I've not managed to read through all of the replies, but this is similar to what I'm attempting to achieve. The header spanning the full width of the screen, and the footer, each with their own individual background colors, and the content being centered. But as you said, the mentioned technique here only works with an entire site being a fixed width.

 

Would you be able to expand on what the basics are of achieving the above?

 

Regards,

Jessica

I've not managed to read through all of the replies, but this is similar to what I'm attempting to achieve. The header spanning the full width of the screen, and the footer, each with their own individual background colors, and the content being centered. But as you said, the mentioned technique here only works with an entire site being a fixed width.

 

Would you be able to expand on what the basics are of achieving the above?

 

Regards,

Jessica

 

Have the header and footer outside the 'wrapper' div, then repeat the header and footer horizontally.

Can you tell me what's wrong with this code?

I'm trying to centre my menu but when I do float: left it get's rid of my bg color...:

 

#wrapper {
background-color: #83e36f;
width: 750px;
margin: 0 auto;
text-align:left;
}


#menu ul {
list-style: none;
padding: 0;
margin: 0;
}

#menu li {
float: left;
margin: 0 0.15em;
}

 

Nevermind - Christopher answered already.

  • 1 month later...

could someone tell me why my site wont centre, i have tried the wrapper technique

 

you should be able to view my html and css on my website

 

my site is www.fifagamingleague.net/website

 

i am usign that extension for the moment instead of wiping away my old site

 

if it helps this is what i designed my site to look like

 

http://img840.imageshack.us/img840/4899/wholsesitelook.jpg

 

if there is anyone that could i would be very very appreciative.

 

thanks a lot !

could someone tell me why my site wont centre, i have tried the wrapper technique

 

you should be able to view my html and css on my website

 

my site is www.fifagamingleague.net/website

 

i am usign that extension for the moment instead of wiping away my old site

 

if it helps this is what i designed my site to look like

 

http://img840.images...lsesitelook.jpg

 

if there is anyone that could i would be very very appreciative.

 

thanks a lot !

 

To center a html container just set its width value and then margin-left:auto / margin-right:auto. It will only center if you set its width.

 

#mainone {margin-left:auto;margin-right:auto;text-align:left;width:950px;}

 

and get rid of the margin-left:40px from all of your other containers like div#logo and #navbar-top

  • 3 months later...
  • 6 months later...
  • 2 weeks later...

For beginners in css I always recommend them to start with a global reset. I use Eric Meyer's reset: http://meyerweb.com/eric/tools/css/reset/

 

This is the way I do it.

 

<div id="main-wrapper">

<div id="header"><!--declared height--></div>

<div id="body-container"></div>

<div id="footer"><!--declared height--></div>

</div>

 

ex:

#main-wrapper{ margin:0 auto; width:967px;}

 

For the header, body-container and footer. same width as the #main-wrapper. I use float:left; instead of margin:0 auto; in this three containers. Opps. with the exception of the main-wrapper of-coarse.

 

I suggest you use Firebug Add-ons for Firefox in studying the structure of other sites...

 

There are many ways. This is just the way I do it :)

Opps. Some designers may argue that it's not ideal to use a global reset. I guess it depends on the designer. A reset makes my life easier :)

Opps. Some designers may argue that it's not ideal to use a global reset. I guess it depends on the designer. A reset makes my life easier :)

 

and mine :)

  • 5 months later...

You don't need to use text-align: center; on the body. Nor text-align left on the wrapper. The code in the first post is trying to center main div twice.

 

Here's how to do it successfully..

 

HTML:

<body>
  <div id="wrapper">
     <!-- Header / Content / Sidebar / Footer html here -->
  </div>
</body>

 

CSS:

body {
  background: #000;
}
#wrapper {
  width: 960px; /* Change to the width of your design */
  margin: 0 auto;
  background: #fff;
}

Edited by Adam Roberts

  • Author

You don't need to use text-align: center; on the body. Nor text-align left on the wrapper. The code in the first post is trying to center main div twice.

 

 

The Original post was written in 2007 when some popular browsers at the time (IE6 for example) required you to use text-align as they ignored margin:0 auto and other browsers ignored text-align and required margin:0 auto. So at the time of writing you did need to do both :pp

Edited by Dizi

  • 4 weeks later...
  • 5 months later...

All you have to keep in mind is you must have a fixed width.

 

For example, give your primary container a fixed width:

 

div#wrapper { width: 1000px; }

 

However, after applying the fixed width, it is still not centered. Now add the following to what we already made:

 

div#wrapper { width: 1000px; margin: 0 auto; }

 

margin: 0 auto

is the same thing as

margin: 0 auto 0 auto

, or,

margin-top: 0; margin-right: auto; margin-bottom: 0; margin-left: auto;

  • 1 year later...
  • 1 year later...

Main Block elements can be center aligned by setting the left and right margins to "auto".

set the left and right margins to auto specify that they be supposed to divide the available margin evenly. The outcome is a centered Block element:

main_div{

margin-left: auto;

margin-right: auto;

width: 70%;

background-color: #b0e0e6;

}

Main Block elements can be center aligned by setting the left and right margins to "auto".

set the left and right margins to auto specify that they be supposed to divide the available margin evenly. The outcome is a centered Block element:

main_div{

margin-left: auto;

margin-right: auto;

width: 70%;

background-color: #b0e0e6;

}

underscores are invalid for class names in css

  • 8 months later...

For elements which has

position: relative;

to center them they must have defined width, float: none; and margin: 0 auto;

For elements which has

position: absolute;

to center them set left: 0; right: 0; and margin: 0 auto; and define its width.

Edited by Dr.Iggy

  • 2 years later...

Hi I am new to the designing and am as thick as f.... when it comes to coding could you please do an in depth explanation of exactly how to do it. i want to centralized Auto my video see website

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.