Why does the CSS box model need a hack?
- 0
- Add a Comment
I am willing to bet tomorrow’s lunch money that you have found yourself wondering at least once why the CSS box model needs a hack? OK, I just tossed that out there, call it fishing if you like. Regardless, this article on “hacking the CSS box model” is quite informative, even if you are already familiar with such a method of working with this instance.
According to the W3C, an assigned ‘width’ (and ‘height’) of a box refers to the ‘content area’ of a box only. The padding, borders, and margins are then added to this value to arrive at the total box width. If the ‘width’ property is omitted, the total box width is the same as the ‘content area’ of the surrounding container element.
All well and good. Unfortunately, all CSS enabled versions of IE before IE6/strict use a different box model. In that model, the padding and borders are counted as part of any assigned ‘width’ or ‘height’. In the absence of borders and padding, the two models agree. However, if a box has an assigned “width’, and if borders and/or padding are added, the standard box model causes the overall box width (between the outer border edges) to increase, while in IE’s model the ‘content area’ gets squeezed by the same amount. This is a major problem for proper page layout.
