Demo 1: Long String
12345678910123456789101234567891011121314
- HTML Code:
-
<div id="container">
12345678910123456789101234567891011121314
</div>
- CSS Code:
#container {
width: 10em;
margin: 1em 0;
}
This first demo shows Expanding Width Bug caused by a very long string without any spaces. It's very easy to see what is going on in IE version below 7. The #container is expanded to accommodate the entire string, whereas in any sane browsers and IE version 7 the container is only as wide as specified ( 10em ). There actually two known to me fixes for this issue. I may note that none of them produce the exact same rendering as in standards compliant browsers, if you know of such a fix please let me know.
Demo 2: Overflowing Descendant
- HTML Code:
-
<div id="container">
<div id="inner">
Try to spot it!
</div>
</div>
- CSS Code:
#container {
width: 10em;
margin: 1em 0;
/* styling info - not normally included in demo
code but is shown here to demonstrate a very common, real-life example */
background: #ddd;
color: #000;
border: 3px solid #000;
}
#inner {
width: 300%; /* excessive width will cause overflow */
}
As opposed to the first demo, this one demonstrates a pretty common real life example. You have some element ( #inner ) that you have put into another element with set width ( #container ). You had no idea what you were doing, were drunk, sleepy, whatever the case may be you ended up having the descendant ( #inner ) width to be more than the one of the ancestor ( #container ). Perhaps your padding and width add up to a larger value (read up on box model if that is not what you expected to happen). If you take a look at the demo, no obvious problems in sane browsers, even IE7 is fine. You take a look in IE 6 and below - whoa! Triple width bug?
What actually happens is, due to excessive width, #inner overflows out of the #container. The effect is not visible in sane browsers because there is no background or border on the #inner; however, affected browsers simply contain the entire #inner (i.e. include overflow ) inside the parent element ( #container ).