Any reasonable programmer, not aware of this bug, would think that the most efficient way to dynamically create horizontal lines is to use createElement to create a div and then set the height of that div to 1px and the width to whatever they want. This is approach is actually correct and safe in all browsers EXCEPT on IE which seems to set the div’s height to 15px whenever you use a value under 15px. A possible workaround is to set the fontSize to 1px, however this would make the div2 pixels high, even if you ser fontSize to 0px you will still get a line which is 2 pixels high. The best way I found to workaround this BUG is to to create a CSS entry like this:
.line {border-top:black 1px solid}
and then set the div’s className to “line”.
If anyone at Microfoft reads this, please, hold on the release of IE8, remove all new features, and concentrate in making all IE7 features to work as they are supposed to, release that version as IE8, then and only then add new features you want to IE9. We do not need the bugs you have now and certially adding new ones won’t make anyone happier.
[jsudds.MSFT] Then you should be delighted to know that we've already fixed that bug in IE7. The behavior in question is a quirks mode artifact left over from the IE6 days. For backwards compatibility, you'll get the same behavior in IE7 if you forget to specify a standards-mode DOCTYPE. Please, please, please use standards mode whenever possible. It's the only reasonable way to get any semblance of control over your Web layout in any browser.
As for your example, there are two ways to create a single pixel-width line. You can either set your content height to 0 and specify one of the two borders that appear on either side of your content box, or you can set your content height to 1px and change the background color of the div.
The following code creates a pattern of alternating red and black lines. Plus, it works equally well in all major browsers, including IE7 and IE8 in standards mode.
<script>
// #1. Set background color of 1px div
var div2 = document.createElement('div');
div2.style.height = "1px";
div2.style.backgroundColor = "#f00";
document.body.appendChild(div2);
// #2. Set top border of 0px div
var div = document.createElement('div');
div.style.height = "0";
div.style.borderTop = "1px solid black";
document.body.appendChild(div);
// Alternate lines
document.body.appendChild(div2.cloneNode(false));
document.body.appendChild(div.cloneNode(false));
</script>
[george_5154] Ok, good point, you are perfectly right, I modified my code and I witness what you say is 100% correct and valid for this case. Using the “famous” DOCTYPE standard mode not only solved this problem, in fact it did correct some other problems I was experiencing. HOWEVER I must do insist, that you should make sure things work as expected in the current version before releasing a new one, because when you combine a DOCTYPE standard mode with a dynamically created table, document.body.clientWidth reports the width of the document minus the width of the scroll bar even when there is no scroll bar on the screen, in other words it becomes unreliable!. That, does not happen if I remove the DOCTYPE standard mode, and this problem only occurs with IE7.