Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Does IE 6 Center Your Table Content?

Rated 4.23 (Ratings: 14) (Add your rating)

Log in to add a comment
(8 comments so far)

Want more?

 
Picture of jaylard

James Aylard

Member info | Full bio

User since: November 18, 1999

Last login: November 18, 1999

Articles written: 7

With the release of Internet Explorer 6, some developers are discovering that content in table cells that used to be left-justified is now centered. The cause is a little obscure, but the solution is simple.

Basically, the problem occurs when both of two conditions are present:

  1. A table is nested within either another table or a block element (such as a <div>) where the outer table or block element has its align attribute set to &quot;center&quot;, typically to create a "jello" layout where the content remains at the center of the screen.
  2. Internet Explorer 6 is operating in "standards-compliant" mode
Producing the Problem

To illustrate, here is a simple snippet of HTML code that will produce cell-content centering in Internet Explorer 6:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>IE 6 Centering Behavior</title>
  </head>
  <body>
    <div align="center">
      <table border="1">
        <tr>
          <td>Here is some data to make this cell a bit wide.</td>
        </tr>
        <tr>
          <td>Is this cell centered?</td>
        </tr>
      </table>
    </div>
  </body>
</html>

In previous versions of Internet Explorer, this code could reliably be expected to display as in Figure 1, below:

Sample table as rendered in Internet Explorer 5
Figure 1

By contrast, Internet Explorer 6 centers not only the nested table itself, but also the content of its cells as in Figure 2, below:

Sample table as rendered in Internet Explorer 6
Figure 2

Fixing the Problem

The actual cause of this rendering quirk in Internet Explorer 6 is its new, standards-compliant rendering mode. Although it is unclear to me that the W3C HTML 4.01 Specification requires this behavior, the IE development team apparently interpreted the spec otherwise (or else it is a rendering bug that will be banished in a future release). So, the key to eliminating this problem is simple: turn off the standards-compliant rendering mode.

How, you ask? The secret is in the document type declaration (sometimes referred to as a doctype declaration, but not to be confused with the document type definition, or DTD). And here's how you do it:

  • The following doctype declaration causes IE 6 to render the page in standards-compliant mode:

    &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;<br>
    &nbsp;&nbsp;&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;


  • But with a simple modification -- eliminating the URI portion of the doctype declaration -- IE 6 reverts to what is commonly called "quirks" mode, and renders the table in a manner consistent with previous versions of IE:

    &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;

In case you are wondering, the doctype declaration without a URI will validate using the W3C's validator. So, there you have it: compliant code that will help you stay centered, instead of your table cells. Happy coding!

No Need to Turn Off Standards Compliance

Submitted by KristinB on September 3, 2001 - 12:10.

Or better yet instead of turning off standards compliance you could use CSS to set your table's contents to left align. Instead of using DIV ALIGN=CENTER in your HTML You could also use CSS to set the left and right margins of your BODY to "auto". This will center your content in standards compliant browsers but will not work in Explorer 5.5. To get it to wor in Explorer 5.5 you can set your BODY to text-align: center and then set your DIVs, TD, etc. within the BODY to text-align: left. This exploits an Explorer bug that should center your content. This technique must be used together with left and right auto margins to center content on standards compliant browsers as well. More on this technique can be found here:

http://bluerobot.com/web/css/center1.html

login or register to post comments

RE: No Need to Turn Off Standards Compliance

Submitted by jaylard on September 4, 2001 - 08:51.

KristinB,

A point of clarification: the method that I described above does not turn off standards-compliance per se (in other words, the code will still validate as standards-compliant); it only turns off the so-called "standards-compliance" mode of Internet Explorer 6 (in this example).

The suggestion at bluerobot.com appears to be a good one. Another alternative would instead be to explicitly set text-align: left on the td element (and to other elements if required) in the style sheet, and then to use classes or ids to set text-align: center on particular elements where required for centering.

Whether one particular method is preferable to another will likely depend on the needs of the designer and of the particular page.

login or register to post comments

RE: No Need to Turn Off Standards Compliance

Submitted by KristinB on September 4, 2001 - 10:25.

Sorry I meant standards compliance mode as opposed to what is called by some "quirks mode". I personally think its best to include the full Doctype declaration with URL in order to get used used to coding for compliant browser behavior instead of continuing to cater to older non-compliant behaviors. This should also insure more consistent cross browser behavior (for newer browsers at least, some of which may not have a "quirks" mode).

I also briefly mentioned setting contents to left align as one alternative that you can use together with DIV ALIGN=CENTER. Sorry I didn't make that more clear.

login or register to post comments

Article on DocType

Submitted by KristinB on September 4, 2001 - 10:35.

Here's a great article by Eric Meyer that does a really good job at explaining doctypes and browser behavior modes:

http://www.oreillynet.com/lpt/a//javascript/synd/2001/08/28/doctype.html

login or register to post comments

RE: No Need to Turn Off Standards Compliance

Submitted by jaylard on September 4, 2001 - 10:53.

KristinB,

Fundamentally, I agree with you. But one unfortunate side-effect of doctype-triggered standards-compliance modes in the latest crop of browsers is that those modes have quirks of their own. Those quirks often require workarounds -- the very thing that cross-browser standards-compliance was advertised as forever relegating to the dark past.

For instance, this very centering behavior is not exhibited by Mozilla/Netscape 6.x, whereas the line-height distortion exhibited by Mozilla/Netscape 6.x in its standards-compliance mode is not exhibited by IE 6 in its standards-compliance mode. Perhaps one development team or the other got it wrong on one or both of these points -- or, more likely, the specs are not as clear-cut as one might assume.

Don't get me wrong: I am a strong advocate of standards-compliance. As much as is practical, I prefer a standards-compliant approach to a non-standards-compliant one. But we need to be realistic that standards-compliance is not a state of perfection, and that sometimes the quirks introduced by a particular browser's standards-compliance mode are quirkier than its so-called "quirks" mode. And in some cases, switching off that standards-compliance mode -- especially when true fidelity with the standard can be maintained -- is preferable, or at least an acceptable option.

login or register to post comments

Ohhhhh!

Submitted by oiboi on January 9, 2002 - 18:38.

So thats what those little tags are for! I am enlightened!

login or register to post comments

Doctype definition tag

Submitted by KristinB on January 10, 2002 - 00:52.

The main purpose of the Doctype definition is to inform browsers what version of HTML (or XHTML, etc.) you are using. It's required if you want your HTML to validate. To check if your HTML is valid you can go here: http://validator.w3.org.

login or register to post comments

This method solves another problem!

Submitted by mats on June 11, 2004 - 04:08.

I was searching the MS knowledge base for hours on end for a solution for a problem where IE 6.0 rendered pages incorrectly (incorrectly being a substancial understatment, the page elements would multiply themselves across the whole page or parts of it would simply black out completely). Changing the doctype this way fixed the problem!

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.