Specifics On Floated Elements
- A left-floated box will shit to the left until its leftmost margin edge (or border edge if margins are absent) touches either the edge of the containing block, or the edge of another floated box.
- If the size of the floated box exceeds the available horizontal space, the floated box will be shifted down.
- Non-positioned, non-floated, block-level elements act as if the floated element is not there, since the floated element is out of document flow.
- Margins of floated boxes do not collapse with margins of adjacent boxes.
- The root element () cannot be floated.
- An inline element that is floated is converted to a block-level element.
Clear The Floats
The container div itself has no height and no float, since it only contains floating elements. Therefore the background color and border stubbornly stays at the top of the floating columns.
Soultion One: Float The Container
- <div>
- <div style="float:left;width:30%;"><p>Some content</p></div>
- <p>Text not inside the float</p>
- <div style="clear:both;"></div>
- </div>
Soultion Two: Add Extra Markup
- <div>
- <div style="float:left;width:30%;"><p>Some content</p></div>
- <p>Text not inside the float</p>
- <div class="clearfix"></div>
- </div>
- .clearfix {
- clear: both;
- }
You could also do this with by means of a br tag with an inline style. In any case, you will have the desired result: the parent container will expand to enclose all of its children. But this method is not recommended since it adds nonsemantic code to your markup.
Soultion Three: The :after Pseudo-Element
The CSS class clearfix is applied to any container that has floating children and does not expand to enclose them.
- .clearfix:after {
- content: ".";
- display: block;
- height: 0;
- clear: both;
- visibility: hidden;
- }
But this method does not work in Internet Explorer up to version 7, so an IE-only style needs to be set with one of the following rules:
- .clearfix {
- display: inline-block;
- }
- .clearfix {
- zoom: 1;
- }
Depending on the type of issue you are dealing with, one of the above two solutions will resolve the issue in Internet Explorer. It should be noted that the zoom property is a non-standard Microsoft proprietary property, and will case your CSS to become invalid.
Soultion Four: The Overflow Property
By far the best, and easiest solution to reslove the collapsing parent issue is to add overflow: hidden;
or overflow: auto;
to the parent element. However, this does not work in IE6. But, in many cases, the parent container will have a set width, which fixes the issue in IE6. If the parent container does not have a width, you can add an IE6-only style with the following code:
- /* This fix is for IE6 only */
- *html .clearfix {
- height: 1%;
- overflow: visible;
- }
In IE6, the height property is incorrectly treated as min-height, so this forces the container to enclose its children. The overflow property is then set back to visible, to ensure the content is not hidden or scrolled.
The only drawback to using the overflow method (in any browser) is the possibility that the containing element will have scrollbars or hide content. If there are any elements with negative margins or absolute positioning inside the parent, the will be obscured if they go beyond the parent's borders, so this method should be used carefully. It should also be noted that if the containing element has to have a specified height or min-height, then you would definitely not be able to use the overflow method.
回應 (Leave a comment)