how to Centering a table with CSS

Any problem with HTML can be discussed here.
blue-sky
Posts: 33
Joined: Tue Jun 12, 2007 3:41 pm

how to Centering a table with CSS

Post by blue-sky »

how to Centering a table with CSS


The most annoying thing for a web designer is definitely the large difference between the two biggest browsers, Internet Explorer and FireFox. Simply adding text-align: center; to your bodys style tag centers a **** in Internet Explorer, but not in FireFox. This is not a bad thing for firefox as it was not added for a reason and thats because when adding text-align to your body tag, you are aligning cells and tables, not text so therefore Mozilla FireFox is right in a way.


There is a VERY easy solution to this problem, however it is not compliable with W3C CSS standards. Simply doing this will center your page in IE and FF:

Code: Select all

body /* IE */
{
text-align: center;
}

body /* FF */
{
text-align: -moz-center;
} 

Simple and does work, but it is not valid W3C HTML, which every website should be.

The Solution
There are absolutely no clear solutions on the internet, they are all very confusing and do not make sense at times, so I will try my best to explain.

Ok lets create a simple website first and put a table block in the center.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<title>Moo</title>
</head>

<body>

<table border="1" width="267" height="202">
   <tr>
      <td height="202" width="267">Hello World!</td>
   </tr>
</table>

</body>

</html>

Ok, I am going to use internal CSS so I don't lose you.

Ok to center a table in both FF and IE you need to add the following to your table tag.

Code:
<table style="" border="1" width="267" height="202">


We have now added a style tag, this is what internal CSS is and you can use external CSS, which I recommended.

Ok now that you have added the style tag, you can now add the vital CSS in order for it to work.

To get it to work you need to take the width of your table, put it in the CSS style area and make both right and left margins auto, you also need to add a style tag to your body area with text-align in there, this is to satify IE.

Heres the final code (don't forget to add your doctype to every page you make Smile)

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Moo</title>
</head>
<body style="text-align: center;">
<table style="width: 267px; height: 202px; margin-left: auto; margin-right: auto; border: 1;">
   <tr>
      <td height="202" width="267">Hello World!</td>
   </tr>
</table>
</body>
</html>

Try it...


csoftdev
Posts: 28
Joined: Fri Dec 07, 2007 4:49 pm

Post by csoftdev »

how about
<table style=width:100%>
<tr>
<td style=text-align:center>
.... your table ....
</td>
</tr></table>
Tails5
Posts: 1302
Joined: Wed Mar 15, 2006 8:09 am
Contact:

Post by Tails5 »

For me the [html]<table style="text-align: center"> ... </table>[/html] always works
Webmaster Yoda: You must confront the cPanel. Then, and only then, a webmaster will you be.
Julius Caesar: Veni, vidi, posti
Gebbo
Posts: 554
Joined: Tue May 16, 2006 3:22 pm

table

Post by Gebbo »

yeah thats the same for me as well.
.............................:: Spirit of Fire ::..................................

Image
Flipper3
Posts: 353
Joined: Tue Feb 28, 2006 12:34 am

Post by Flipper3 »

I have never included -moz- when declaring the text-align. Is it really necessary?
Tails5
Posts: 1302
Joined: Wed Mar 15, 2006 8:09 am
Contact:

Post by Tails5 »

Never in my experience, I've always been able to just use "center"
Webmaster Yoda: You must confront the cPanel. Then, and only then, a webmaster will you be.
Julius Caesar: Veni, vidi, posti
TheCrymsonLegends
Posts: 1246
Joined: Wed Feb 16, 2005 6:59 am

Post by TheCrymsonLegends »

I've never done it I normally just use the following code:

Code: Select all

align=center valign=center
Depending on where you put it, it aligns both the tables and text. If you put it in the table then you align the table on the browser and if you put it in the table row then you align the text. I don't normally use css but there are easier ways to take out the major code section such as an example below:

Code: Select all

table.hd      {
            color: #C68E17;
            background-color: #8A4117;
            border-width: 2px 2px 2px 2px;
            border-style: outset outset outset outset;
            border-color: #8A4117 #8A4117 #8A4117 #8A4117
            }
When you use the table and style area you would put it in the table line with "hd" for the style.

Code: Select all

 <table style="hd" align="center" width="90%"> 
Gives you more code with smaller text to allow you to make the code smaller per page to allow for less bandwidth usage per page, to get the most out of your bandwidth.

Hope this helps other people and also this could of been put into the How-To section for points, grantes SHAdmin feels it is good.[/b][/color]
Reached 5000 Credits! The highest of any member on Smokyhosts! New milestone for Me!
thetarget
Posts: 496
Joined: Sat Jul 09, 2005 9:10 am

Post by thetarget »

TheCrymsonLegends wrote:I've never done it I normally just use the following code:

Code: Select all

align=center valign=center
Depending on where you put it, it aligns both the tables and text. If you put it in the table then you align the table on the browser and if you put it in the table row then you align the text. I don't normally use css but there are easier ways to take out the major code section such as an example below:

Code: Select all

table.hd      {
            color: #C68E17;
            background-color: #8A4117;
            border-width: 2px 2px 2px 2px;
            border-style: outset outset outset outset;
            border-color: #8A4117 #8A4117 #8A4117 #8A4117
            }
When you use the table and style area you would put it in the table line with "hd" for the style.

Code: Select all

 <table [b]style="hd"[/b] align="center" width="90%"> 
Gives you more code with smaller text to allow you to make the code smaller per page to allow for less bandwidth usage per page, to get the most out of your bandwidth.

Hope this helps other people and also this could of been put into the How-To section for points, grantes SHAdmin feels it is good.[/b][/color]
a little typo here? or am i wrong about class='hd' rather than style='hd'
jasondsouza
Posts: 348
Joined: Thu Jan 12, 2006 8:24 pm
Contact:

Post by jasondsouza »

Tails5 wrote:For me the [html]<table style="text-align: center"> ... </table>[/html] always works
this is more than enough to align the text in a table using css... or you can also use style id's and then link it to a external style sheet...
--jasondsouza
ME working on a very new site. (Coming soon)
Come to my web Site
thetarget
Posts: 496
Joined: Sat Jul 09, 2005 9:10 am

Post by thetarget »

All in all. There are so many techniques, but do note that some parameters are inherited, planning takes a bigger role.
Locked