table can't be centered

He has: 688 posts

Joined: Feb 2001

Please check out this link in IE. The tables (border=0) that contain the personnel names and email addresses are centered. Now look at that link in Firefox and you'll see that those tables get shoved to the left.

Because can't be used in XHTML I use the following code to center items in both IE and Firefox.

.center {
display: block;
text-align: center;
margin: 0 auto 0 auto;
}
'

This code validates and works well for all sorts of items in both IE and Firefox. But as in the case linked above, I just can't get it to work on a table. I tried my 'center class' on both the table and a div surrounding the table but still no luck. Any ideas? Thanks.

02bunced's picture

He has: 412 posts

Joined: May 2005

Providing it is a fixed-width table, do

margin-left: auto;
margin-right: auto;

to see it in action, go to easternbaptist.org.uk and look at the table on the front page

Busy's picture

He has: 6,151 posts

Joined: May 2001

align="center" in the table tag will validate (just not with strict), if you are using strict wrap the table in a centered div

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

02bunced is right. I tried adding the following to your style sheet, and it worked. As long as it doesn't mess up any other tables, you should be fine.

table{
    margin-left: auto;
    margin-right: auto;
}
'

He has: 688 posts

Joined: Feb 2001

Thanks! Odd that table won't obey the rules that other elements do. I'll add that code later tonight but isn't that the same code as I already have in my generic .center css? Mine (give to me by somebody here) uses margin: 0 auto 0 auto; . Doesn't this code already say left and right are auto margins?

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

It is, but that only centers any elements that are of class center, not things within them.

He has: 688 posts

Joined: Feb 2001

I'll try that code later anyway, but I must say that, although it isn't in my code at this moment, I did try class="center" on the table tag itself (rather than the containing div) and that didn't work either. I'm not disagreeing, I just want to fully understand the situation so I can learn from this.

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Yep, I tried that too with the same results (or lack thereof). I think you're right in that tables act a little different from "normal" elements. I know I've run into various quirky things, but I've never really researched why it all was happening. If you learn more, I would like to know what you find out.

He has: 688 posts

Joined: Feb 2001

Got it. Thanks.

FYI, I changed my "center" css and applied it to the table as we were discussing but it still didn't work. It only worked when I changed it to inline css. Odd. But as long as it's working I'm going to just move on and not worry about the why.

Thanks again.

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

No prob! I'm relatively new to CSS myself, so I know how annoying some of these "new" problems can be to figure out.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.