CSS way to horizontally align table
Asked Answered
C

11

107

I want to show a table of fixed width at the center of browser window. Now I use

<table width="200" align="center"> 

But Visual Studio 2008 gives warning on this line:

Attribute 'align' is considered outdated. A newer construct is recommended.

What CSS style should I apply to the table to obtain the same layout?

Consultative answered 19/11, 2008 at 6:17 Comment(1)
So, which solution did you go for in the end?Coyote
J
199

Steven is right, in theory:

the “correct” way to center a table using CSS. Conforming browsers ought to center tables if the left and right margins are equal. The simplest way to accomplish this is to set the left and right margins to “auto.” Thus, one might write in a style sheet:

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

But the article mentioned in the beginning of this answer gives you all the other way to center a table.

An elegant css cross-browser solution: This works in both MSIE 6 (Quirks and Standards), Mozilla, Opera and even Netscape 4.x without setting any explicit widths:

div.centered 
{
    text-align: center;
}

div.centered table 
{
    margin: 0 auto; 
    text-align: left;
}


<div class="centered">
    <table>
    …
    </table>
</div>
Jambeau answered 19/11, 2008 at 6:28 Comment(3)
You can get the table to center with margin: 0 auto; in IE6 and up if you ensure your page has a valid doctype declaration.Coyote
@Marco: I have not right now this information, but that may be a good base for a question on StackOverflow.Jambeau
you are perfectly right. TABLE do not need to specify a width to get horizontally centered. I tested it. Whilest DIV do need a width to be specifed to get horizontal centering.Mensural
W
20

Try this:

<table width="200" style="margin-left:auto;margin-right:auto">
Wulfenite answered 19/11, 2008 at 6:27 Comment(0)
C
2

Simple. IE6 and above will happily center your table with "margin: 0 auto;" if only the page renders in "standards" mode. To make this happen you need a valid doctype declaration, such as

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

or

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

True, IE5.5 and below will still refuse to center the table but perhaps you can live with that, especially if the page is still functional with the table left aligned. I think by now users of IE5.5 and below are fairly used to some odd looking websites - but you still need to ensure that those visual glitches don't render your site unusable.

Happy coding!

EDIT: Sorry, I should perhaps point out that you do not have to have a "strict" doctype to get IE6 and up into "standards" rendering mode. I realised it might seem that way from the doctype examples I posted above. For example, this doctype declaration will of course work equally:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Coyote answered 19/11, 2008 at 21:5 Comment(0)
H
2

Although it might be heresy in today's world - in the past you would do the following non-css code. This works in everything up to and including today's browsers but - as I have said - it is heresy in today's world:

<center>
<table>
   ...
</table>
</center>

What you need is some way to tell that you want to center a table and the person is using an older browser. Then insert the "<center>" commands around the table. Otherwise - use css.

Surprisingly - if you want to center everything in the BODY area - you just can use the standard

text-align: center;

css command and in IE8 (at least) it will center everything on the page including tables.

Hebetic answered 22/11, 2015 at 19:57 Comment(0)
C
0
style="text-align:center;" 

(i think)

or you could just ignore it, it still works

Carlsen answered 19/11, 2008 at 6:22 Comment(0)
E
0

This should work:

<div style="text-align:center;">
  <table style="margin: 0 auto;">
    <!-- table markup here. -->
  </table>
</div>
Ectogenous answered 19/11, 2008 at 6:30 Comment(0)
B
0

I'm just learning this and what finally worked for me was to first make a table with three rows. Set the margin for the left and right rows to 50%. Then put a single row, fixed width table inside of the "table data" of the center "table row".

Bilow answered 2/10, 2009 at 2:36 Comment(0)
S
0
<style>
    .abc {
        text-align: center;
    }
</style>

<table class="abc">
    <tr>
        <td>Item1</td>
        <td>Item2</td>
    </tr>
</table>
Scissile answered 19/12, 2013 at 9:50 Comment(0)
K
0

Add to the div style:

width: fit-content;
Koal answered 25/8, 2019 at 8:44 Comment(0)
P
0

Basically, it works like,

<table align="center">
...

But, you can do it in better way using CSS, and this will be a better approach to use CSS as it provides dynamic behavior to a web page and is responsive.

  <table style="text-align:center;">

Also, if you need to display only table on a page then you can go with body tag alignment only as shown below,

 <body style="text-align:center;">
<table>
  ...
</table>

If you want any other suggestions please let me know. Will surely help you on this. Also using bootstrap features this would be more easy and interactive.

Plated answered 25/8, 2019 at 9:14 Comment(0)
G
0

if you use bootstrap, you can add text-center class in element table.

  <table class="table table-bordered text-center">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">First</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th scope="row">1</th>
          <td>Mark</td>
        </tr>
        <tr>
          <th scope="row">2</th>
          <td>Jacob</td>
        </tr>
      </tbody>
    </table>
Glass answered 27/9, 2022 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.