We’ve seen that the float can be used to place an HTML to the side of other content.
How does it do this and why does it seem to overlap other HTML elements?
- Floated Elements Flow Sideways
- Floating Elements Wrap If There Is Not Enough Space
- Wrapping is Dependent on Which Way the Element Floats
- Percentage as a Dimension
- margin:auto Has No Effect on Floats
- Expanding a Floating Child to Fit Its Parent
- Playing Around
Floated Elements Flow Sideways
HTML elements flow from top to bottom in the order in which they appear in the HTML document. This is called normal flow.
Floating elements are outside of normal flow and do not stack top to bottom. Instead, they stack from left-to-right for left floated elements and right-to-left for right floated elements.
Consider the following code:1
<div class="box yellow left"></div> <div class="box lime left"></div> <div class="box orange left"></div> <div class="box cyan left"></div> <div class="box yellow right"></div> <div class="box lime right"></div> <div class="box orange right"></div> <div class="box cyan right"></div> <div class="box yellow centre"></div> <div class="box lime centre"></div> <div class="box orange centre"></div> <div class="box cyan centre"></div>
Which gives the following output:
You will notice that:
- 3 sets of coloured boxes are declared in the same order (.yellow, .lime, .orange, and .cyan)
- each set of boxes is given a different alignment class (.left, .right, or .centre)
- the boxes on the left stack from left-to-right in the same order they were declared
- the boxes on the right stack from right-to-left in the same order they were declared
- the boxes in the middle stack from top to bottom in the same order they were declared
- because the .left boxes and .right boxes are not in normal flow, the .centre stacked boxes occupy their place in the normal flow.
Recall that (relative to their parent):
- floating elements float up as high as they can. They stop when they either encounter the top margin of their parent or the bottom margin of a non-floating element.
- floating elements float as far to the side as they can. They stop when they either encounter the side margin of their parent or the side margin of another floating element.
This is exactly what we observe in the example above:
- the floated elements are declared at the top of the document, so they float up to the margin of the parent – they are not stopped by the bottom margin of other floating elements
- the floated elements flow as far to the side as they can and are stopped either by the side margin of the parent (the yellow block) or the side margin of another floating element (the other blocks).
Floating Elements Wrap If There Is Not Enough Space
Elements in normal flow stack one atop the other and continue down the bottom of the page as far as needed.
Floated elements do not stack sideways as far as they can go – they are constrained by the width of their parent (assuming you don’t make the child wider than its parent). Any floating elements for which there is not enough room to stack sideways will flow to the next line.
Consider the following 4 left floating blocks (they are scaled to each take up 25% of the available width):
They all line up in a single line as expected.
If we make them a little wider, say 26% of the width of the parent container:
We see that the blue block, because it doesn’t fit, automatically wraps to the next line and moves as far left as it can.
A similar thing happens for right floated elements – except everything moves as far right as it can:
Wrapping is Dependent on Which Way the Element Floats
Sometimes, you can get some unexpected float behaviour if you don’t remember that floating elements:
- float up as high as they can
- then float to the side
- in the order they are declared in the document.
Consider the following:
The sum of both floats is wider than the parent, so:
- the floats can’t both fit on the same line (250px + 250px is greater than the 400px of the parent)
- the floats wrap
- they wrap in the order they were declared in the document (the right element is placed first, then the left).
Of course, you can have elements float to the left and right on the same line. If we change the width of the floats in the above example to 100px, we get:
Remember that parts of the parent CSS Box is under both the left and right boxes, but the content in the parent box is never under a floating element.
Percentage as a Dimension
Up till now, we have always used pixels as a dimensioning unit. CSS has many ways of dimensioning things. It can be easier to work with float widths as a percentage of the parent container width. When using % as a dimension, it refers to the width of the container as a percentage of the parent width.
Consider the following:
The yellow box is 30% of the width of its parent.
The green box is 50% of the width of its parent.
If the width of the parent changes, the widths of the child elements will adjust automatically. This can be useful if you want your web content to adapt its size to the browser window width.
It is also useful if you want to quickly divide up the available real estate. Consider the following:
Main content goes here.
Sidebar content goes here.
Each floating box gets the specified percentage of the parent’s area. The parent size can be easily changed without having to update the individual sizes of the floating elements.
Expanding a Floating Child to Fit Its Parent
A floating child element will only fill out the parent’s height only if the parent has a definite height specified (e.g. 200px). If no height is specified for the parent or a percentage height is specified for the parent, the floating child will not expand to fill the height of the parent – even if .clearfix is used to expand the parent around the floating children.
Using the same code as above, but
- setting the parent height to 100%,
- setting its background-color to yellow, and
- clearing the floats after the last float (so the parent expands around the floating children):
Main content goes here.
Sidebar content goes here.
We see that the parent (yellow) has closed around its children. It has expanded to the height of the sidebar box, but the main content box (despite having its height set to 100%) has not expanded to the bottom of the parent.
If the parent does not have a fixed height, it is not possible to make a floating child expand to the height of its parent without either:
The only time this (usually) becomes an issue is when children and parents have different background colours, borders, or other properties that makes it obvious the floating child and parent are not the same height.
Floating children will always respect the width of the parent – as long as you do not make the child wider than the parent.
margin:auto Has No Effect on Floats
Setting margin to auto4 on a floating element will have no effect. This is because floating elements automatically float as far as they can to the left or right.
However, you can set a fixed margin on a floating element and it will respect it:
- Floating elements do not stack top to bottom.
- left floated elements stack from left-to-right.
- right floated elements stack from right-to-left.
- Floating elements stack in the order they appear in the document.
- If there is not enough room on a line to fit a floating element, the element moves down (wraps) to the next line and moves as far to the side as it can.
- Instead of pixels, dimensions can be given as a percentage. The percentage is relative to the dimensions of the parent.
- A floating child can be made to fit the full height of its parent by setting its height to 100%. This only works if the parent has a defined height in pixels.
- margin:auto, which is commonly used to centre HTML elements, is ignored when applied to floating elements.
- Floating elements respect non-auto margin values.
The best way to learn is to play around with what you have learned.
You have previously seen that you can use the div tag to section content in a document. Prior to seeing the float property, the only possible flow of content was vertical – from top to bottom. With float it is possible to get content to flow side-to-side.
How would you section and style the following page layout using div and float?
- Make it look good in your browser on your computer. Don’t try to make it look good at all possible screen resolutions.
- Think about how to section the document. Remember that divs can nest inside other divs.
- The layout above is a rough template to give you direction on how to layout your document.
- A general rule of thumb in web design is to make your layout 960px wide (which is then centred in the browser)5.
- Feel free to use larger boxes, but in the article, I am somewhat constrained for space.↩
- Which will be covered in a future tutorial.↩
- CSS is incredibly flexible and there are lots of choices for positioning elements, but those are for future tutorials.↩
- Remember that auto is how we get elements to centre inside a container. Effectively, we are asking the User Agent (browser) to apply equal margins to the left and right side.↩
- This tends to work fine for desktop browsers, but not so well for mobile browsers. Nevertheless, it is a good rule of thumb for dividing up and laying out content on your page.↩