What is CSS Float?
Float is the positioning property of the CSS. In a traditional print layout, the text can be centered around the picture as needed. This is generally referred to as "text wrapping." In web design, a page element that applies the float property of CSS is like a picture surrounded by text in a printed layout. The floating element is still part of the page flow. This is a distinct difference from page elements that use absolute positioning. Absolutely positioned page elements are removed from the page stream, just as the text boxes inside the print layout are set to ignore page wrapping. An absolutely positioned element does not affect other elements, nor does the other element affect it, regardless of whether it is adjacent to other elements.
The Fload property has four values available: left and right float elements in their respective directions, None (the default) causes the element to not float, and Inherit will get the float value from the parent element.
The use of float
In addition to simply wrapping text around a picture, floating can be used to create all page layouts.
Float is also useful for small layouts. For example, this small area of the page. If we use float on our little avatar image, the text in the box will automatically adjust to the position when the image size is resized:
The same layout can be achieved by using relative positioning in the outer container and then using absolute positioning on the avatar. In this way, the text is not affected by the size of the avatar image, and does not change depending on the size of the avatar image.
Clear float
Clear is a related property of float (float). An element that is set to clear float does not move upward to the boundary of the float element, as set by float, but instead ignores the floating downward movement. As below, a picture of the top thousand words.
In the example above, the sidebar floats to the right and is shorter than the main content area. The footer (footer) then jumps up to the possible space as required by the float. To resolve this problem, you can clear the float on the footer (footer) so that the footer (footer) stays below the floating element.
#footer {Clear:both;}
The purge (clear) also has 4 possible values. The most common is the both, clear the left and right sides of the float. Left and right can only clear One direction of the float. None is the default value and is used only if you need to remove the specified purge value. Inherit should be the fifth value, but it is very strange that IE does not support (this is not surprising, IE has always been so maverick-sugar with tomato note). Only the left or right floats are cleared, but they are seldom seen in practice, but they are absolutely useful.
The great collapse
One of the more puzzling things about using float (float) is how they affect the parent element that contains them. If the parent element contains only floating elements, its height collapses to zero. This problem can be difficult to notice if the parent element does not contain any visible backgrounds, but this is a very important issue.
The visual opposite of collapse is worse, look at the following situation:
When the block-level elements above are automatically extended to accommodate floating elements, unnatural whitespace wrapping occurs in the text flow between paragraphs, and there is no effective way to correct the problem. In this case, the designer will complain more than the collapse of the complaint (do not understand, not the design is completed before the page encoding? -Sugar companion tomatoes).
To prevent weird layouts and cross-browser problems, the collapse problem is almost always handled. After we float the element in the container, the container ends before it clears the float.
Technology to clear floating
If you know exactly what the next element will be, you can use Clear:both; To clear the float. This method is very good, it does not need to hack, does not add the additional element also makes it has the good semantics. Of course things don't all work out like this, and there are several other tools that need to be cleaned up in the toolbox.
- The empty div method literally, is an empty div. Sometimes some other elements may be used, but the div is most commonly used because it does not have a default browser style, no special features, and is generally not styled by CSS. This method is ridiculed by pure semantics because it is only for performance and has no contextual meaning to the page. Admittedly, they're right from a strict point of view, but this approach works and doesn't hurt.
- The overflow method sets the overflow CSS property on the parent element. If this property of the parent element is set to auto or hidden, the parent element expands to include the float. This method has good semantics because he doesn't need extra elements. However, if you need to add a new div to use this method, in fact, the empty Div method is just like no semantics. Also remember that the overflow property is not defined to clear the float. Be careful not to overwrite the content or trigger unwanted scroll bars.
- The simple Purge method uses a smart CSS pseudo-selector (: After) to clear the float. Rather than setting overflow on the parent element, just add an extra class like "Clearfix" to it. This class uses the following CSS:
. clearfix:after { content: "."; Visibility:hidden; Display:block; height:0; Clear:both; }
Different situations require a different floating purge method. Take a grid with a different style block as an example.
In order to visually better link similar blocks, you need to open new lines where necessary, here is where the color changes. If each color group has a parent element, we can use overflow or a simple cleanup method. Or, use an empty div method between each group. The extra div doesn't exist before, so you can try it yourself to see which method is good.
< reference: http://www.nowamagic.net/csszone/css_AllAboutCss.php concise Modern Magic >
CSS Floating Properties Float Introduction