1

How can you center an entire table in Internet Explorer 7+?

I want to avoid on centering the <td> element. I want to center the entire table with width:auto;nearly 1050 pixels.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Subodh Bansal
  • 385
  • 1
  • 4
  • 12

4 Answers4

2

text-align: center; on the body element should do it if the document is in quirks mode, otherwise the usual way of centering will also work even without a width on a table margin: 0 auto

added:

here's a link to a previous answer on the subject, covers both quirks and standards

CSS way to horizontally align table

Community
  • 1
  • 1
clairesuzy
  • 27,296
  • 8
  • 54
  • 52
  • It *shouldn't*, but will if IE is in Quirks mode (which you should ensure it is not) – Quentin May 07 '11 at 12:22
  • thx, David a bit quick lol - I was adding more info as you spoke, I agree docs shouldn't be in quirks but sometimes people have to work with legacy code still – clairesuzy May 07 '11 at 12:27
1

You can use margin: 0 auto to center.

This should be fine in IE7 although I don't have it to test with.

Working Demo

Town
  • 14,706
  • 3
  • 48
  • 72
1

Make sure you have a Doctype that triggers standards mode, then set margin-left: auto; margin-right: auto; on the table. Further reading: Centring using CSS

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

According to this blog post you should be able to use CSS, http://csssky.blogspot.com/2007/04/to-center-table-using-css-only.html

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

However, there seems to be an issue with IE 7 that they recommend adding the following to the beginning of the document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
user739866
  • 891
  • 1
  • 9
  • 18