IE7 disappearing content bug

Sophie Dennis, Cayenne Web Development. 31 Oct 2006. Last updated 3 Nov 2006.

Update 3 Nov 2006

Aleksander Vacić has encountered another instance of this disappearing content bug, this time with suckerfish-style navigation lists. Again the bug is triggered by a background-color on a parent element. His fix involves moving the background to a different parent element. The reason this works is that element the background is now applied to is floated, and therefore hasLayout.

Read on for more about this bug and how to fix it...

Introduction

This is intended as a quick overview of the "disappearing image/content bug" in IE7, which occurs when a floated image appears within a paragraph which contains text in italics. While this page details an incredibly specific bug involving italic text, images and background properties, which occurs in IE7 but not other versions of IE, similar "peekaboo" style bugs can be seen in other cases in Internet Explorer. It appears to always be triggered by an ancestor element having a background-color or background-image set, but not "layout". Removing the background CSS property, or triggering layout, both correct the problem.

This page quickly demonstrates what triggers the bug and how to fix it. It's pretty basic at the moment and hardly a work of art, but hopefully it'll be useful to people trying to track this bug down. You need to view the page in IE7 to see the effect of the different examples.

I hope to expand the explanation, tidy the page up and add screenshots for those not using IE7 soon.

What's Going On?

This page details a very specific instance of this kind of disappearing content bug which only occurs in IE7, but the same core combination of CSS properties trigger similar disappearing content bugs in earlier versions of IE as well. So here's a quick summary:

The Bug - examples

Example 1: Basic styling, all OK

Jane Dorner (artist) with her sculpture 'Ampersand River'A paragraph of text with some words in italics in it, with an image floated to the left. All seems well.

Example 2: Background property on parent, image disappears

Jane Dorner (artist) with her sculpture 'Ampersand River'A paragraph of text with some words in italics in it with an image floated to the left. The paragraph has a background-color set. Now if the words in italics wrap over more than one line, the image is hidden from the point where the italics begin. The image still occupies the correct space within the document flow, but it appears to be "masked" by the background color. The bug confusingly does not seem to occur if the italics begin on the first line of the paragraph.

Example 3: Fix bug with hasLayout on parent

Jane Dorner (artist) with her sculpture 'Ampersand River'A paragraph of text with some words in italics in it. The paragraph also has a background-color set, but we've triggered hasLayout on the paragraph (we've used min-height, as its unobtrusive and cross-browser, but you could use whatever trigger method works best for your layout). Now if the words in italics wrap over more than one line, the image remains visible.

Example 4: Bug occurs with any ancestor

Jane Dorner (artist) with her sculpture 'Ampersand River'A paragraph of text with some words in italics in it. This paragraph now has a parent <div> on which the background is set. The bug occurs no matter what ancestor element has a background set.

Example 5: Bug fixed by hasLayout on any container within the ancestor

Jane Dorner (artist) with her sculpture 'Ampersand River'A paragraph of text with some words in italics in it. The same as above, but we've again triggered hasLayout on the paragraph. Even though the offending background property is on the <div>, triggering hasLayout on either the <div> or the <p> corrects the bug.