Web Design Forum: TUTORIAL: how to center a site using css - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: how to center a site using css A quick tutorial Rate Topic: -----

#41 User is offline   3dfan 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 02-April 09
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

  Posted 02 April 2009 - 01:04 PM

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

#42 User is offline   hire202 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 14
  • Joined: 03-May 09
  • Reputation: 0
  • Gender:Male
  • Location:United Kingdom
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 03 May 2009 - 05:06 PM

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.
0

#43 User is offline   Cloaca 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 16-May 09
  • Reputation: 0
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 16 May 2009 - 07:19 AM

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!
0

#44 User is offline   Dizi 

  • Queen of the Spammers
  • PipPipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 5,654
  • Joined: 13-August 07
  • Reputation: 161
  • Gender:Female
  • Location:Newcastle, UK
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 16 May 2009 - 10:02 AM

View Posthire202, 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 :)
0

#45 User is offline   Tex0gen 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 69
  • Joined: 03-July 08
  • Reputation: 0
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 28 May 2009 - 02:49 AM

I do the following:

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

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

0

#46 User is offline   ranmori 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 29-May 09
  • Reputation: 0
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 29 May 2009 - 10:08 AM

simulation credit auto
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 :mellow:
0

#47 User is offline   craigpettit 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 174
  • Joined: 11-September 08
  • Reputation: 0
  • Location:Surrey UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 02 June 2009 - 12:51 PM

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

#48 User is offline   Wickham 

  • Web Guru
  • View gallery
  • Group: Moderators
  • Posts: 2,874
  • Joined: 11-June 09
  • Reputation: 257
  • Gender:Male
  • Location:Salisbury UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 11 June 2009 - 08:17 AM

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.
0

#49 User is offline   Crispy 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 327
  • Joined: 10-July 09
  • Reputation: 2
  • Gender:Male
  • Location:Wrexham, North Wales
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 12 July 2009 - 06:59 PM

This is really helpful, glad it's pinned.
0

#50 User is offline   ali7766 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 18
  • Joined: 27-August 08
  • Reputation: 0
  • Gender:Male
  • Location:North Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 12 July 2009 - 08:18 PM

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.
0

#51 User is offline   Kellyeld85 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 22
  • Joined: 07-August 09
  • Reputation: 0
  • Gender:Female
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 07 August 2009 - 12:37 PM

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.
0

#52 User is offline   Thomas Thomassen 

  • HTTP 503
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,254
  • Joined: 30-April 07
  • Reputation: 10
  • Gender:Male
  • Location:Trondheim, Norway
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 07 August 2009 - 12:44 PM

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

#53 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 16 August 2009 - 01:54 AM

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;
}

0

#54 User is offline   ashley_90 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 270
  • Joined: 23-March 09
  • Reputation: 1
  • Gender:Male
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 August 2009 - 11:01 AM

Thanks for this, makes things so much neater when coding!
0

#55 User is offline   swiped 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 242
  • Joined: 22-February 09
  • Reputation: 2
  • Location:London,Uk
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 06 September 2009 - 01:57 PM

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 */
}
0

#56 User is offline   Dizi 

  • Queen of the Spammers
  • PipPipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 5,654
  • Joined: 13-August 07
  • Reputation: 161
  • Gender:Female
  • Location:Newcastle, UK
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 06 September 2009 - 02:55 PM

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...-center-a-site/
0

#57 User is offline   chyssa 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 19-October 09
  • Reputation: 0

Posted 27 October 2009 - 03:48 AM

thanks for sharing your codes...
0

#58 User is offline   MyshMash 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 138
  • Joined: 06-December 09
  • Reputation: 3
  • Gender:Female
  • Location:UK
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 04 January 2010 - 12:10 AM

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;
}

0

#59 User is offline   ChristopherDarling 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 343
  • Joined: 27-November 08
  • Reputation: 7
  • Gender:Male
  • Location:Bristol - UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 04 January 2010 - 01:05 AM

add to #menu ul overflow:hidden;
0

#60 User is offline   lingal 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 30-April 10
  • Reputation: 0
  • Gender:Female
  • Location:Asia
  • Experience:Beginner
  • Area of Expertise:Designer

Posted 30 April 2010 - 05:41 AM

Thanks! It is helpful! :clapping:
0

#61 User is offline   GiffordBruno 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 13-May 10
  • Reputation: 0
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 07 June 2010 - 09:23 AM

The best way to center the content using CSS is using of wrapper and the div portion,thanks.
0

#62 User is offline   Jessica 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 06-August 09
  • Reputation: 0
  • Gender:Male
  • Experience:Beginner
  • Area of Expertise:I'm Learning

Posted 06 July 2010 - 01:37 PM

View PostDizi, on 13 November 2007 - 07:54 PM, said:

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
0

#63 User is offline   Frost Tear 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 713
  • Joined: 10-May 10
  • Reputation: 17
  • Gender:Male
  • Location:Bristol
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 06 July 2010 - 02:16 PM

View PostJessica, on 06 July 2010 - 01:37 PM, said:

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.
0

#64 User is offline   EpicWebs 

  • Dedicated Member
  • PipPip
  • View gallery
  • Group: Members
  • Posts: 169
  • Joined: 03-May 10
  • Reputation: 8
  • Gender:Male
  • Location:UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 06 July 2010 - 03:09 PM

View PostMyshMash, on 04 January 2010 - 12:10 AM, said:

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.
0

#65 User is offline   georgebower1 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 20-August 10
  • Reputation: 0

Posted 21 August 2010 - 04:08 PM

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 !
0

#66 User is offline   Synjyn 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 227
  • Joined: 15-August 10
  • Reputation: 35
  • Gender:Male
  • Location:London
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 21 August 2010 - 05:58 PM

View Postgeorgebower1, on 21 August 2010 - 04:08 PM, said:

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
0

#67 User is offline   kingy da killa 

  • Free man on the land
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,144
  • Joined: 24-November 10
  • Reputation: 94
  • Gender:Male
  • Location:London SW
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 01 December 2010 - 05:28 PM

<div id="wrapper" style="margin-left:auto;margin-right:auto;width:800px;>[website goes here]</div>
my method :)
1

#68 User is offline   Sazzad 

  • Expert
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 628
  • Joined: 21-February 07
  • Reputation: 38
  • Gender:Male
  • Location:New York, New York
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 03 June 2011 - 03:40 PM

I found this tutorial very helpful for those that wish to center their entire div tag: http://www.sohtanaka...-alignment-css/
0

#69 User is offline   anjie 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 14-June 11
  • Reputation: 0
  • Gender:Female
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 14 June 2011 - 02:28 PM

For beginners in css I always recommend them to start with a global reset. I use Eric Meyer's reset: http://meyerweb.com/...ools/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 :)
0

#70 User is offline   anjie 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 11
  • Joined: 14-June 11
  • Reputation: 0
  • Gender:Female
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 14 June 2011 - 02:32 PM

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 :)
0

#71 User is offline   kingy da killa 

  • Free man on the land
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,144
  • Joined: 24-November 10
  • Reputation: 94
  • Gender:Male
  • Location:London SW
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 14 June 2011 - 02:45 PM

View Postanjie, on 14 June 2011 - 02:32 PM, said:

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 :)
0

#72 User is offline   Adam Roberts 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 33
  • Joined: 07-November 11
  • Reputation: 1

Posted 23 November 2011 - 12:16 PM

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;
}

This post has been edited by Adam Roberts: 23 November 2011 - 12:17 PM

0

#73 User is offline   Dizi 

  • Queen of the Spammers
  • PipPipPipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 5,654
  • Joined: 13-August 07
  • Reputation: 161
  • Gender:Female
  • Location:Newcastle, UK
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 26 November 2011 - 08:24 AM

View PostAdam Roberts, on 23 November 2011 - 12:16 PM, said:

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

This post has been edited by Dizi: 26 November 2011 - 08:25 AM

0

#74 User is offline   bekyarov 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 34
  • Joined: 23-December 11
  • Reputation: 4
  • Gender:Male
  • Location:Bulgaria
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 23 December 2011 - 12:31 PM

This would be useful for the CSS beginners, good one :)
0

Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

18 User(s) are reading this topic
0 members, 18 guests, 0 anonymous users