Containing Floats includes float and containingfloats

Source: Internet
Author: User

Containing Floats includes float and containingfloats
Http://www.complexspiral.com/publications/containing-floats/ is often used as a tip tool for layout because of its powerful floating function and useful. You may have seen the situation in figure 1, which is implemented only by two div elements. Each div has a floating image. Figure 1. It is incorrect. This may not be the author's intention, but considering the style used, this is the correct layout. We created it like this:

div.item {border: 1px solid; padding: 5px;}div.item img {float: left; margin: 5px;}
The above are all styles. The result shown in Figure 1 is that the div element is not "stretched" to include the floating image. From another point of view, this is because the floating element "extends" the bottom of the div element. This is neither a bug nor a CSS defect. In fact, many authors usually want this extended behavior. The examples shown in Figure 1 are not what they want. Understanding ProblemsSo when is it true that the author wants to float out the effect of containing elements? Very simple: What is the most common usage of floating in history. Consider 2 and generate the basic tag structure for this effect. Figure 2. Floating images and text (http://meyerweb.com/other/china/jul31.html)
<p> ...text...  ...text...</p><p> ...text...</p>
This practice traces the flow of text around the image a long time ago. This is why Netscape1.1 has added this capability to the Web. It is also possible for CSS to use the attribute float. But take a closer look at Figure 2. A floating image extends its elements. We can see more clearly by adding borders to the paragraph, as shown in 3.
Figure 3. Add a border on a paragraph
So now we know why floating elements stretch out the elements that contain them. If you do not stretch out, figure 2 will look like Figure 4. Figure 4. If floating elements expand their parent elements, this is unacceptable to the designer. Therefore, in order to retain the traditional Web design and the expectations of the author, CSS is designed to allow floating elements to stretch out the bottom of the elements that contain them. Although this is required for general text streams, the main problem occurs when floating is used for layout purposes. Clear SolutionIf float is used in a non-table layout, You need to stretch the elements that contain them to include them. At present, a structured hack is required. Since we need to put an element at the bottom of the contained element to clear the previous floating bottom, clear is our answer. We only need to insert a block-level element before the container ends and clear the floating. Consider:
<div class="item">  Widget 6144 <br>$39.95 
Now we apply the following rules to the preceding labels to obtain the effect shown in Figure 5.

div.item hr {display: block; clear: left; margin: -0.66em 0;  visibility: hidden;}
Figure 5 use horizontal rules to force scaling by ensuring that the hr element is a block-level and part of a normal flow, and that the floating is cleared, we force the div to "stretch and hold" the left floating image. The top and bottom margin of negative numbers usually have the effect of disabling the space occupied by hr. However, this is not accurate, and there are also different browsers. Semi-mysterious horizontal rules make it difficult to predict exactly what will happen. The valid height of hr may be 0, a small positive number, or even a negative height. Therefore, to precisely clear the effect, the author can use div instead of hr to create a clear effect. For example:
div.clearer {clear: left; line-height: 0; height: 0;}<div class="item">  Widget 6144 <br>$39.95 <div class="clearer">&nbsp;</div></div>
Set Float to fix FloatThere is one way to avoid over-using the structured hack discussed so far, although these structured hack are sometimes necessary. In most browsers, as defined in CSS2.1, floating elements expand to include any floating elements inherited from it. Therefore, in the small product example, we can remove the "clearer" element and replace it with these styles:
div.item {float: left; border: 1px solid; padding: 5px; width: 60%;}div.item img {float: left; margin: 5px;}
Note that we float the image and "item" div. By setting the div width to a height of 50%, we ensure that the div does not appear in the same row, but vertically stacked together. Effect 6.
Figure 6 stretch using float to include floating elements.
In terms of tag and style, it is obvious that this is easier to manage. However, the hack discussed so far is still useful. Suppose you want to put some suggestion text below the product. To prevent text from flowing to the right of the product, we need to insert the clear hack. This will guide us to create the following tag, as shown in 7.

<div class="item">  Widget 6144 <br>$39.95</div><div class="item">  Widget 6145 <br>$44.95</div> <div class="clearer">&nbsp;</div> <p>Widgets are sold on an "as is" basis without    warranty or guarantee.</p>

Figure 7 Floating and hack Implementation of the desired Layout
The purge div effectively pushes the normal stream down and forces the content behind any removed element to follow the floating div.
The potential defect of floating inclusion is that browser-dependent consistent interpretation of the layout of multi-layer nested floating elements. If these floating elements are part of a more complex layout and the situation becomes more fragile, this Part may use float, location, or table. This does not mean that these la s cannot be implemented. However, they may involve a lot of experimentation and errors, avoiding obscure float and other layout bugs that may be hidden inside the rendering engine.
Summary
By understanding the relationship between floating and normal streams, and understanding how clearing can be used to process normal flows around floating, the author can regard floating as a very powerful layout tool. Since float is not originally used for layout, it may be necessary to make them as expected. This may involve floating elements, "clear" elements, or the combination of the two.
Looking into the future, there are various proposals to enhance CSS, allowing the author to declare that an element is stretched to include any floating element in it. Obviously, it is very popular to add this capability to CSS. However, this capability has not been added as of the time of writing, and it may take a long time to support this capability.

Source http://files.cnblogs.com/jeffma/code.rar

Incorrect CSS floating margin doubling

If an edge is in the same direction as the float, the margin is doubled when the margin is applied to the floating box on the edge. This error only affects the first floating of a row once or multiple times.

Impact: Internet Explorer 6.0

This error only affects ie and does not affect other browsers.

Add display: inline;

Adobe official solution:

Problem
When a margin is applied to a floated box on the same side as the ction the box is floated AND the floated box is the first one inside its containing box, the margin is doubled. (In other words, a left-floated box with margin-left set to move it away from its containing box will be twice as far away from the left edge of the containing box as intended .)

Solution
Specify display: inline; for the affected float.

Detailed description
This bug only reproduces when the floated element's margin extends in the same direction as the float itself, and is between the float and the container box's inside edge. subsequent floats with similar margins won't reproduce this bug-just the first float in any row of floated elements.

For example, floatbox {float: left; margin-left: 10px;} is correct in FF. But in IE, 20 is changed. Change to floatbox {float: left; margin-left: 10px; display: inline ;}.

In div + css, how does one close floating elements?

<Div>
<Div style = "float: left"> abc1 </div>
<Div style = "float: left"> abc2 </div>
<Div style = "clear: both;"> </div>
</Div>

You can understand the above Code. No matter how many floating elements are in the layer, add a layer with the clear: both attribute at the end.

You can put the. clear {clear: both} code in the CSS file, and then directly use <div class = "clear"> </div>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.