Web Design by DTW



Post new topic Reply to topic  [ 2 posts ] 
  Print view Previous topic | Next topic 
Author Message
 Post subject: how to center a site using css A quick tutorial
PostPosted: Mon Sep 14, 2009 4:54 pm 
Offline
Site Admin

Joined: Wed Jul 29, 2009 11:26 pm
Posts: 29
This is the most commonly asked question on the forum and as such it makes sense to have a pinned tutorial at the top of all the sections that it has been asked in

Tutorial:
How to center content using CSS

The best way to center a page is to use a wrapper that contains what you want to center, and this can be done in two simple stages.


Stage one- The CSS
Within your css add something that looks like this code:

Code:
body {        text-align:center;}#wrapper {        margin: 0 auto;        width: xxxx; /* Replace the xxxx with the the width of your site (eg 800px)*/        text-align:left;}



A quick explanation of what margin: 0 auto; will do


In the body add text-align:center as this will fix a problem that some browsers have with margin:auto. However this will make all the text in anything be centered so to fix this in the wrapper you will tell all the text to go back to the left, this way you are resetting the default position of the text and will place the text left unless you tell it in a different div/class to be otherwise.

in the wrapper, the 0 is there to tell the top and bottom not to have a margin, this can be changed if you want there to be a top and bottom margin.

The auto tells the left and the right to automatically set a margin. So it takes into account the space that remains and divides it between the left and right margins, making the content centered on the page.



Stage two – The HTML

Now that you have the CSS part done all you need to do is apply the wrapper to the HTML. This is simple, anything that you want to be centered on the page shoul be placed inside the wrapper like this:


<div id="wrapper"><!-- Place everything you want to be centred here--></div>



And that is all that you need to do to center something on a page




EDIT:

For a more in depth look check out this tutorial
Code:
http://webdesignpond.co.uk/wdp/tips-tutorials/how-to-horizontally-center-a-site/

To vertically center a site check out this tutorial
Code:
http://webdesignpond.co.uk/wdp/tips-tutorials/how-to-vertically-center-a-site/



Top
 Profile  
 
 Post subject: Re: how to center a site using css A quick tutorial
PostPosted: Mon Sep 14, 2009 4:56 pm 
Offline

Joined: Mon Sep 14, 2009 3:26 pm
Posts: 16
Thanks club webd for this nice post I just normally create 1 whole div and all the contents and div inside the main div and center it using CSS.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Protected by Anti-Spam ACP



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Clubwebdesigners v1.1