Empty element background used by next element in IE
In IE6, the paragraph following the blank paragraph is displayed with the background color of the blank paragraph, which I assume is wrong! It works correctly in Firefox, but I haven't tested IE7.
Is there a CSS solution for this problem or do I need to remove the empty element?
(I would prefer not to remove empty elements as it requires writing code to check if all are empty before printing it out)
Behavior does not change using strict or transient doctics (added this comment in response to answers)
Interestingly, the effect does not occur with the text color, only the background color.
<html>
<head>
</head>
<body>
<p style='background-color:green'>Green content</p>
<p style='background-color:red'>Red content</p>
<p>Unstyled background working because previous red element is not empty</p>
<p style='background-color:green'>Green content</p>
<p style='background-color:red'></p>
<p>Unstyled background broken because previous red element is empty</p>
<p style='color:green'>Green content</p>
<p style='color:red'>Red content</p>
<p>Unstyled text color working because previous red element is not empty</p>
<p style='color:green'>Green content</p>
<p style='color:red'></p>
<p>Unstyled text color working even though previous red element is empty</p>
</body>
</html>
source to share
A blank paragraph is meaningless - it means you are probably writing incorrect HTML.
Also, your example does not have a DOCTYPE - a valid DOCTYPE is required for browsers to interpret your code correctly, without it you will be stuck in quirks mode.
But anyway, the simplest workaround for this error is to simply set a background for your current unleaded item.
source to share
I just tested this in IE7 and can confirm it happens there too.
It looks like the unpinned paragraph does n't actually have a red background, it's just that IE7 draws a red box for the empty paragraph and then draws the next paragraph on top.
You can see it yourself by trying this code:
<p style='background-color:green'>Green content</p>
<p style='background-color:red; margin-left:50px'></p>
<p>Unstyled background broken because previous red element is empty</p>
You should see that the red paragraph is 50px to the left.
Why he does this, everyone can guess. Adding some code to check if a paragraph is going to be empty isn't too hard, plus it removes useless markup from your output and avoids the problem altogether. Let it go?
source to share