Last Updated: February 25, 2016
· cub3

IE9 Phantom Table Cell Generation

So your'e here because you have a HTML table that works on every browser except IE9, for some reason on IE9 displays a phantom cell that doesn't exist in the HTML anywhere (even when you use it's inspector tool) and you have no idea what to do....

This problem has plagued me for awhile now and pops up every now and again, I thought i'd share a quick guide on why it happens and how to fix it

Pretty much what happens is your codebase will looks a bit like this:

    <td>Cell one</td>
    <td>Cell two</td>
    <td>Cell three</td>

Every (normal) browser parses that code with this kind of logic

"ok so a table with one row and three cells, lets display that.."

But not IE9, IE9 reads through tables and second guesses itself, it parses it and thinks

"ok so a table with one row and three cells, but there's some spaces? did they mean four cells? hmm well lets display four"

and therefore screws up your columns..

The only way to fix to fix this is to have the code formatted in this way:

<table><tr><td>Cell one</td><td>Cell two</td><td>Cell three</td></tr></table>

So IE9 wont see any spaces and think we need more cells than whats actually written.

The problem with the solution though is once you remove all the spaces, the code becomes really hard to read, so what I suggest is that you leave this until the end of your project so its easier work on in the meantime.

Or, if you like, you can use a regexp to remove whitespace like this:

var expr = new RegExp('>[ \t\r\n\v\f]*<', 'g');
tableHtml = tableHtml.replace(expr, '><');

But with very large tables i'd suggest that'll be a nightmare on browser memory and the other fix is easier...

Supported Documentation

And Discussions

TL;DR - IE9 Bug that reads whitespace in tables as "cells" and generates phantom cells that screw everything up