Box model and Quirks mode

One difficultie in the design of javascript components results from differences of interpretation from the browsers. To make simple, the W3C specifies that the width or the height of an element has to reflect its actual contents. That is that the dimensions of padding, border and margins have to be to add to the specified dimensions. Regrettably, Microsoft in the first versions of his browser (up to internet explorer 5) use another model, that is than the size of an element correspond to its global size (including borders, set the margins).

You will say « Ok but they fixed it since version 6 « .
Yes but not because of the Quirks mode (by opposition to the standard mode) which allows the browse to insure a retro compatibility by preserving the former mode of renderer.

And when can you say that you are in Quirks mode (among others)?

By using an HTML page without DOCTYPE in other words in the majority of the cases

To observe the phenomenon just test the following code in a HTML page.

You will observe according to browsers the difference beetween the fixed size (width and height) and the real occupied size (offsetWidth and offsetHeight) by passing the mouse on the div.

Then what are the solutions

  • Insert in your page the following doctype.
  • Use browser detection and at the need re-size elements

Most of the ajax frameworks use the 2nd solution because they can’t be sure of the pages in which they are executed.

You should not forget to test the mode of renderer to affect or not the new sizes. Just test the property document.compatMode

  • if the value is « CSS1Compat then you are in standart mod and you don’t need to change the size
  • if the value is « BackCompat » you are in quirks mode and you have to set the size

Here is a squeleton of function that change the width of an object

This probleme can seem harmless but the construction of complex components require the assembly of a large number of HTML element and size are very rigorous. A gap of 1 or 2px have naturally consequences on the coherence of the component.

And so, all this work for this old bug…..