CSS float: considerations, tips and macetes for good layouts on the web
July 11, 2008 11 comments
Understand the correct functioning and dynamics of the CSS property "float" is fundamental to the development and structuring of good layouts for Web. Learn to use properly float on web sites and web projects, in general, is of extreme importance because, basically, is through the rules using CSS float is possible that compose layouts without the use of tables (feedback), which brings many benefits such as economy in time of loading, matching the W3C standards and hence an increase performance in general.
What is "Float"?
Float is a property of CSS positioning. If you are familiar with projects for printed media, you can think, in a similar way, an image in a layout where the surrounding text when necessary.

As for web design, an inserted image is still part of the flow of the page. This means that if changes are made in size or whether elements around you change, the page will automatically be readjusted ( "reflow"). This differs from the page where the elements are positioned so absolute. Elements positioned so absolute are removed from the flow of the web page. Elements positioned absolutely will not affect any other elements of the page, whether they are in contact, or not.
For the floats that are used?
Besides simple examples, such as placing a picture next to a block of text, floats are used to create layouts for Web.

Floats are also useful for layouts of small bodies. See, for example, this part of a web page:

These types of layouts can be manipulated using absolute positioning within relative positioning, but elements that float (CSS property "float") are more flexible. Let's assume that the image size of an avatar need to be changed. With floats, the box can be restructured to accommodate a larger size, as an absolute positioning create problems:

Problems with floats
"Floats are fragile." They are full of "setbacks" and cross-browser quirks. Perhaps the most significant is the need for "clear" floats (CSS property "clear") in some situations. First, see some examples of why some floats must be "clean" and then how to do this "cleansing".
Arrumar the float to adjust the height of the parent element
Elements that contain elements float not calculate its height as it is to be expected. In fact, if the parent element contains only elements floats, browsers will render the point at zero (as if it were "height: 0").

If you give a "clear" before closing the tag of the parent element, you repair it.

Clear the float to start a new line
Let's say you have a series of floating elements.

So let's say you want to create a pause in this grid of elements in order to start a new line. Because, you know, iss makes sense only visually speaking.

By giving "clear" only to the left or right
The two examples above are examples of how the generic use of "clear" to the "cleansing", or give clear both to the right, on the left. Because floats can be both right, as the left, and you can obviously make a float just left or just right. This can be helpful when doing the cleaning of both sides ( "clear: both") is problematic.

If it had been given a clear on both sides ( "clear: both") in the example above, the second image would have been pushed down, which ends the text block.
Different techniques of Clear
Like everything in CSS, more than one way to do this.
Using "clear" exactly where you need
The CSS property "clear" is exactly what "says the box." The problem lies in where and how to apply an element to the page with clear property right.
- Apply "clear: both" the item immediately following that you need to be clear. Let us take an example near the top of a web page with layout of the header and footer wide total, with main content and a floating left sidebar to the right. In order that the footnote appears in the right place, you must clear the floating before him. In this simple example, you can apply apply clear: both the very bottom of div.
This technique is wonderful, because it works well without the need for codes superfluous. However, it sometimes fails to dynamic websites. And if, for example, you had to add a new page element, above the bottom and therefore below the other content of the page? Now you have to give a clear element in this rather than the bottom. Often it is easier to think about where we need a clear rather than on what must be one element is clear.
- Apply "clear" and clear the float in a vacuum. DIVs are very good because, generally, you have no style applied to them (as in an element of paragraph "p" for example) and they have no special feature (as a "<br />"). Where need to clear the float, just enter: <div style="clear: both;"> </ div>. Use in-line styles is not very "attractive", it is better to make a class "clear" and create a rule that makes the cleaning of the float, but that's only a matter of taste.
The method of empty DIV:
... part of floating element. </ div> <div class = "clear"> </ div> <p> ... ahhhh, I received a "clear"! </ p>
CSS:
div. clear (clear: both;) "Overflow: auto" in the parent element
It is difficult to explain why, but the implementation of the CSS property "overflow: auto" in the parent element will cause its height is calculated correctly. It will expand to encompass the entirety floats (floats) rather than "to stick." This can be very useful and is very clear "but has some drawbacks. The biggest of these is that, often, it makes sense to use the CSS rule in the parent element. Think of the times in which you must give a clear on several elements that have a father in common, in which case it will not vai help.
Another problem is that you may want to use the property "overflow" for other purposes. And if you want to hide the overflow of a split, in particular? You can not. You will have to include the split with another split to do that.
The hack "clearfix" (make clear to the pseudo-class "after")
Although old, the article positioniseverything about using a pseudo-class CSS to make clear is still valid. Explaining quickly, the technique is to add a little of substance after the element. This bit of content (an area, generally) is making clear the work, but is hidden from the visitor.
Here is a code, applied in a CSS class for any element that needs a clear:
/ * This needs to be first because FF3 is now supporting this * /. Clearfix (display: inline-block;). Clearfix: after (content: ""; display: block; height: 0; clear: both; font-size : 0; visibility: hidden;) / * Hides from IE-mac \ * / * html. Clearfix (height: 1%;). Clearfix (display: block;) / * End hide from IE-mac * / The article quoted warning that the technique is getting old and already talks about "overflow: self." I do not share the view that the technology is getting outdated. This is completely different from other technical overflow. With "clearfix," you apply to class in their own element, not the parent element. This means you can use it even when it is not a father, this means that you can continue to use it, even if there is a parent element that makes sense, and apply it wondering where the element needs to float a clear.
Another problem with float: pushes down ( "pushdown")
This issue deserves special attention, because people often have problems with issues of "jerks." Looking again the example set earlier this article, the split from the main content floats to the left of the split of the sidebar. This is a common structure in many blogs.
Whether fixed or fluids, both DIVs have a width specified. They supposedly behave themselves as floating elements, or if an element within the area from the main content is the width greater than this whole area (for example, an image very large) if it simply extends through copper and anything that is " No way. " The way that treats IE6 that is quite different. If one element is greater than the width of its parent element, IE6 the sidebar vai simply be pushed down ( "pushdown"), completely breaking the layout.

The solution? The best solution is not to place elements with width greater than that of its parent element. To better protect, and if your layout works differently (not specify heights), you can work with "overflow: hidden" to hide something that is needed. Yet another solution, which would use absolute positioning to position the sidebar for more direct. Remember, however, that absolute positioning cut the element of the flow of the page - something to take into consideration.
For some time left on A List Apart an article about making a "false" absolute positioning, Faux Absolute Positioning, which is an interesting reading and addresses a new technique of layout that brings many benefits of absolute positioning, keeping the flow of the page and not going against the "fragility" of the elements to float.
"Quirks" on elements float
Another thing to remember when dealing with IE6 is that if you apply a margin (CSS "margin") in the same sense that the float ( "left" or "right"), it should be double the margin.
For IE7, there is a little trick on his peculiar way it fails to lower margins (CSS "margin-bottom") in child elements within floating objects.














Congratulations on this mega-Article Tárcio! My students will love!
Excellent lessons Tárcio. Fantastic article amigão! : D
@ Gevã Schaefer
Legal, Gevã! Then to me what they thought! ;-)
@ Paulo Faustino
Thank you, Paul, but the credits go to Chris Coyier, I only translate rsrs…… Abraços!
Much tutorial and congratulations on blog, is really very good. ^ ^
Abraços!
@ Cayo aka Medeiros. yogodoshi
Thank you for your kind words and for the reference he made to Development web to your blog! I signed the feed of the blog yogodoshi for "switched figurines"! ;-)
Abraços!
Good article:)
I am aware of the issue and work on this issue. I thought to who's out and does not see much of the matter, will be more easily worship with this post, congratulations:)
@ Bfms
Thank you for consideration, Bruno! It really was my intention to help translate this excellent article on CSS floats.
Big hug to appear!
to with problems on my site for placement. If you can help me I would be grateful.
@ Patrick
Hi, so good? What is the problem that you are, colleague?
Trackback on July 11, 2008
Trackback on July 11, 2008