CSS: centering tables

onelin

Senior member
Dec 11, 2001
874
0
0
For those in the know, I'm curious...

what's the proper way to center a table? I don't want to use <center> if I don't have to, but all of the CSS centering that I see is through text-align properties, and I can't recall if that affects containers.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Try this (might or might not work):

Wrap a div around the tables, and use
{
text-align: center; // this is for IE .. its box model is fscked up
margin-left: auto;
margin-right: auto;
}
 

onelin

Senior member
Dec 11, 2001
874
0
0
thanks. you know, I was JUST in the middle of trying that!!! :) we'll see if Opera likes it
 

onelin

Senior member
Dec 11, 2001
874
0
0
negative on that :( You were right about IE, it did center it while opera and firefox do not.

There has to be a standardized way to center containers...
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Define the width of the table in your CSS. It should center it (works on Firefox) :)
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Here's something I whipped up quickly:

-------------------------------------------------------------------------------------

<html>
<head>
<style>
#wrapper {
text-align: center;
margin-left: auto;
margin-right: auto;
width: 200px;
}
</style>
</head>

<body>

<div id="wrapper">
<table border="1">
<tr>
<td>aaaaaaaaaaaaa</td>
<td>aaaaaaaaaaaaa</td>
</tr>
</table>
</div>

</body>
</html>

-------------------------------------------------------------------------------------
 

onelin

Senior member
Dec 11, 2001
874
0
0
Neat hack! However, I just tried it without a fixed width and it breaks :( (200px works great, however 60% does not) ...em also appeared to be imbalanced

I tacked on a text-align: left for my table CSS to make sure it's text isn't also centered.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Here's page: Linkie

I tried this on my previous code:

<html>
<head>
<style>
#wrapper {
text-align: center;
}

table {
margin-left: auto;
margin-right: auto;
}
</style>

</head>

<body>

<div id="wrapper">
<table border="1">
<tr>
<td>aaaaaaaaaaaaa</td>
<td>aaaaaaaaaaaaa</td>
</tr>
</table>
</div>

</body>
</html>

.. and it's working fine without the width now. w00t! :D
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
:):beer::D

I have a whole bunch of useful links on CSS and xHTML. Maybe I should post them and request for a sticky!