Solution (Clean Solution)
Date of the Solution
Fri Jul 17 01:29:24 2009
Fixed Versions
all of the affected
Description
If you are not perfectly sure that you have semantic markup, chances are that you would want to read the "Clean Markup Solution" instead.
This fix uses only two rules (possibly even one) in your main stylesheet and is suitable for rare cases when you have a text node as a sibling of a block-level element and you want to apply text-align
to that text. Let's take a look:
Give me the aligns!
Oh noes!
Give me the aligns!
Oh noes!
- HTML Code:
-
<div id="container1">
Give me the aligns!
<p>Oh noes!</p>
</div>
<div id="container2">
Give me the aligns!
<p>Oh noes!</p>
</div>
- CSS Code:
/* contain float - irrelevant to the bug, but something you might need to do */
div {
overflow: hidden;
display: inline-block;
}
div { display: block; } /* restore original display, IE hasLayout method */
/* END of float containing */
#container1 { text-align: center; }
#container2 { text-align: right; }
p {
width: 50%;
float: left;
}
Let me explain the idea of the fix, since the code has some extra bits that are not exactly relevant. What fixes the bug is the float
, simple as that. Yes, this fix is not always possible to implement, if that's the case you may want to try the "Clean Markup solution" for this bug instead.
Besides p { float: left; }
and the rest of the code from the demo you will notice a block of CSS code marked with float containing
Microsoft, it is there because we are floating the descendants (<p>
s), thus we need to contain them in the parent elements. The technique is known as float containment and is outside the scope of this tutorial. I may note that the div { display: inline-block; } div { display: block; }
is a method to give "layout" in IE which may be not suitable or not needed in your situation.