In Today’s article, we are going to discuss HTML Floats. HTML elements are usually blocks that stack one below another, so you will find a new block below the previous one, in the order you have them in the HTML file.
float is a CSS property that allows you to “float” elements to the right or to the left of other elements in the HTML file. Something that you already know how to do with Flexbox, right?
Why are floats important?
Even though floats have decreased in usage with the addition of new techniques like Flexbox or CSS Grid, it’s worth knowing how to use them. For example, when you have to float an image inside a block of text, the best tool to do it is still a
float. Also, there are plenty of CSS codebases that still use floats. When you need to work on existing code based on floats you should be able to do it.
HTML Float Element Explained
The float property is a valuable and powerful asset to any web designer/developer working with HTML and CSS. Tragically, it can also cause frustration and confusion if you don’t fully understand how it works.
Let’s start with the definition of a float. According to the W3C:
A float is a box that is shifted to the left or right on the current line. The most interesting characteristic of a float (or “floated” or “floating” box) is that content may flow along its side (or be prohibited from doing so by the “clear” property). Content flows down the right side of a left-floated box and down the left side of a right-floated box.
The float property has four values that we can apply to it:
none. Each value is pretty self-explanatory. For example, if you assign
float: left to an element, it will move to the left-most boundary of its parent element. The same idea applies if you were to assign
float: right; to an element. That element would be sent off to the right-most boundary of its parent element. The
inherit value tells an element to inherit the float value of its parent element. The value
none is the default value and tells an element not to float at all.
Here is a simple example
Let an image float to the left:
Let image be displayed just where it occurs in the text (float: none):
In page layout programs, the boxes that hold the text can be told to honor the text wrap, or to ignore it. Ignoring the text wrap will allow the words to flow right over the image like it wasn’t even there. This is the difference between that image being part of the flow of the page (or not). Web design is very similar.
In web design, page elements with the CSS float property applied to them are just like the images in the print layout where the text flows around them. Floated elements remain a part of the flow of the web page. This is distinctly different than page elements that use absolute positioning. Absolutely positioned page elements are removed from the flow of the webpage, like when the text box in the print layout was told to ignore the page wrap. Absolutely positioned page elements will not affect the position of other elements and other elements will not affect them, whether they touch each other or not.
What are floats used for?
Aside from the simple example of wrapping text around images, floats can be used to create entire web layouts.
Clearing the Float
Float’s sister property is clear. An element that has the clear property set on it will not move up adjacent to the float like the float desires, but will move itself down past the float. Again an illustration probably does more good than words do.
In the above example, the sidebar is floated to the right and is shorter than the main content area. The footer then is required to jump up into that available space as is required by the float. To fix this problem, the footer can be cleared to ensure it stays beneath both floated columns.
Clear has four valid values as well. Both is most commonly used, which clears floats coming from either direction. Left and Right can be used to only clear the float from one direction respectively. None is the default, which is typically unnecessary unless removing a clear value from a cascade. Inherit would be the fifth, but is strangely not supported in Internet Explorer.
Problems with Floats
Floats often get beat on for being fragile. The majority of this fragility comes from IE 6 and the slew of float-related bugs it has. As more and more designers are dropping support for IE 6, you may not care, but for the folks that do care here is a quick rundown.
- Pushdown is a symptom of an element inside a floated item being wider than the float itself (typically an image). Most browsers will render the image outside the float, but not have the part sticking out affect other layout.
- Double Margin Bug – Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin.
- The 3px Jog is when text that is up next to a floated element is mysteriously kicked away by 3px like a weird forcefield around the float.
- In IE 7, the Bottom Margin Bug is when if a floated parent has floated children inside it, bottom margin on those children is ignored by the parent.