This post is about CSS floats. Well floats are pretty simple. There is nothing complicated about them. Complication only arises of two things. One is using them in a complex manner and the second is cross browser compatibility issues.
Let’s recap what we know about browsers. Every tag in HTML or XHTML basically creates an “element”. And that element can be either of two types when it comes to rendering. Either it can be an inline element or it can be block element.
Inline elements, when appear in succession, would be placed let to right (or right to left if page direction is right to left which can be set in the html tag’s dir attribute i.e dir=”rtl”) till the end of line.
While the block level elements sit on top of each other and expand horizontally to fully expand the space.
But we can do an interesting thing to a block level element. We can order it to float it self. But where it should float? We can ask it to float either on the right side or on the left side. So when we ask an element to float, it would be plucked out of the sequence in which it was previously appearing. And when plucked out then of course that space would be pretty much empty. What do we do with that space then? Well we do not have to do anything. If there is another element just after the one being floated, it would take the place of the floated element.
Astute reader may ask that because block level elements fully expand till the width available so if they are floated left or right, it would have no difference other then that the element after the floated one would be taking place of the floated element.
Quit correct. That is how it would be happening. But think of this. You have a block level element whose dimensions are 50px (i.e 50px in width and 50px in height). If order this element to float towards right (and we will see how to do that) then what will happen?
Well simply two things:
- The element’s right margin would be touching to the right boundary of the containing element
- The element after the floated element would be pulled 50px up because the space would be freed up by the floated element
So that is how floats work. There’s only one thing that is left about them. And that is that what if you do not want the element after the one being floated to come up and fill up the space? Yes that’s perfectly possible as well. We can do that and we will see in a while how.
Suppose there is an element with class
float-it and another element with class
So here’s the markup:
And we apply the following CSS:
So as we have set the float property to right, the box is pushed to right. Possible values for float property are:
none is useful if an element is already floating and you want to turn that off.
So that was about floats. And its only that much believe me. Only one thing that I have not touched is clearing which is even more easier then floats. Let’s recap about the floats. We have a float property with which we can ask an element to float itself either to the left or right side of the containing block (that can be body, or the div in which that element appears). The element just after the floated element would be pulled up to take up the space because the element above it is now floating and no more in the document flow.