TUTORIAL: how to center a site using css A quick tutorial
#41
Posted 02 April 2009 - 01:04 PM
#42
Posted 03 May 2009 - 05:06 PM
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.
#43
Posted 16 May 2009 - 07:19 AM
#44
Posted 16 May 2009 - 10:02 AM
hire202, on May 3 2009, 18:06, said:
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
#45
Posted 28 May 2009 - 02:49 AM
* {
margin: 0 auto; - New Browsers mostly [Firefox yumm]
padding: 0;
}
body {
text-align: center; - For Older Browsers [IE yuck]
}
#46
Posted 29 May 2009 - 10:08 AM
Thank you for providing us with the example code, and a brief explanation of the problem. However, we do not fully understand the problem you are experiencing
#47
Posted 02 June 2009 - 12:51 PM
#48
Posted 11 June 2009 - 08:17 AM
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.
#50
Posted 12 July 2009 - 08:18 PM
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.
#51
Posted 07 August 2009 - 12:37 PM
</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.
#52
Posted 07 August 2009 - 12:44 PM
#53
Posted 16 August 2009 - 01:54 AM
body {
width: xxxx;
margin:0px auto;
position: relative;
}
#55
Posted 06 September 2009 - 01:57 PM
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 */
}
#56
Posted 06 September 2009 - 02:55 PM
I wrote a short tutorial on how to vertically center a site without this happening it can be found here:
http://webdesignpond...-center-a-site/
#58
Posted 04 January 2010 - 12:10 AM
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;
}
#61
Posted 07 June 2010 - 09:23 AM
#62
Posted 06 July 2010 - 01:37 PM
Dizi, on 13 November 2007 - 07:54 PM, said:
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
#63
Posted 06 July 2010 - 02:16 PM
Jessica, on 06 July 2010 - 01:37 PM, said:
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.
#64
Posted 06 July 2010 - 03:09 PM
MyshMash, on 04 January 2010 - 12:10 AM, said:
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.
#65
Posted 21 August 2010 - 04:08 PM
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 !
#66
Posted 21 August 2010 - 05:58 PM
georgebower1, on 21 August 2010 - 04:08 PM, said:
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
#67
Posted 01 December 2010 - 05:28 PM
my method
#68
Posted 03 June 2011 - 03:40 PM
#69
Posted 14 June 2011 - 02:28 PM
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
#70
Posted 14 June 2011 - 02:32 PM
#72
Posted 23 November 2011 - 12:16 PM
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;
}
This post has been edited by Adam Roberts: 23 November 2011 - 12:17 PM
#73
Posted 26 November 2011 - 08:24 AM
Adam Roberts, on 23 November 2011 - 12:16 PM, said:
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
This post has been edited by Dizi: 26 November 2011 - 08:25 AM
Help


























