An interesting conversation came up at work the other day: Should we use the CSS float
property now that we have CSS Grid and Flexbox?
The short answer
No! Well, mostly. I’d only use it today for wrapping text around images, though and I’d avoid using float
entirely for layouts.
The longer, more annoying answer
Before flexbox and grid, we had to use the CSS float
property to make grids and layouts. In fact, it was the first thing I learned about web design. On one hot summer afternoon I cracked open a copy of Designing with Web Standards by Jeffrey Zeldman and then moved a tiny red div with float: right
. It was magic. There was power in the float
.
It was so easy to move something around on the screen that I now wonder how many designers fell in love with the web simply because of how easy it is to use move things around like that.
But using float
to build complex layouts was always a hack: it was only really designed to let text wrap around an image.
img {
width: 150px;
float: left;
}
The problems with float
begin when we try to build giant layouts and magazine-style grids. But that’s what we had to do since there were no alternatives back then like we do today.
One problem with the float
property is that you’d have to wrap floated elements with something called a clearfix that looked like this:
<div class="clearfix">
<div class="float-left">Column</div>
<div class="float-left">Column</div>
<div class="float-left">Column</div>
</div>
clearfix:after {
content: "";
display: table;
clear: both;
}
Jay Hoffman described the clearfix hack a while back:
The clearfix, for those unaware, is a CSS hack that solves a persistent bug that occurs when two floated elements are stacked next to each other. When elements are aligned this way, the parent container ends up with a height of 0, and it can easily wreak havoc on a layout. All you might be trying to do is position a sidebar to the left of your main content block, but the result would be two elements that overlap and collapse on each other. To complicate things further, the bug is inconsistent across browsers. The clearfix was invented to solve all that.
Things began to slowly change after that. Back in 2017, Rachel Andrew explained how browsers can handle the clearfix problem without any hacks at all. All we need is the following CSS to make the same fix:
.container {
display: flow-root;
}
The odd thing is that I didn’t know the flow-root
value existed until about three minutes before I typed that. But I guess this sort of defends the argument I’m about to make here: with CSS Grid and Flexbox we don’t really need float
at all. The property was really designed to do one thing: let text wrap around images. But now, with grid and flexbox, we have wonderful powers that can do all the heavy lifting for real layouts.
Back to the argument I was having at work. Some folks said that we should go back and delete all the instances of float
in our codebase because it’s old code and we can easily replace it with flexbox or grid. But this is where I’d say, Whoa! hold up a sec.
I don’t think having the float
property in a few places in our codebase is doing that much harm at all — this isn’t radioactive code that’s causing problems.
So should we be using CSS float
for anything besides letting text wrap around text? Nope. But should we all go out and immediately purge the web of CSS float declarations because it’s not pure and not the “correct” way of doing things? Nope again.
The nifty thing about the web is that old code shouldn’t break things. Just ask Chris. A website that isn’t using the fanciest CSS properties or the coolest tricks isn’t useless or bad. We’ve simply replaced float
with alternatives that are better. I think it’s a good lesson here that these CSS properties are likely going to stick around forever because they still have applicable use cases in modern web design.
And that’s okay.
It’s a pretty common question on stack overflow, which is a measure of how heavily the expectation of the use of floats was tainted by the layout hacks.
CSS Exclusions will further substantially narrow the use cases for floats. It remains to be seen whether floats will still retain their niche once exclusions are widely available.
You nailed it. Wrapping text around an image (an exceedingly rare case); nothing more.
Gotta say — never liked the ol’
float
.I didn’t liked the name
flow-root
. It would be more appropriate to use a name starting withblock-
.flow-root
seems illogical as it seems applicable to the inner elements while designer wants to style the container element.The word
root
is also confusing; it seems for the root element of the web page/document.BTW, the preview feature, while writing comment here, has a problem. I am not able to view full preview of the comment; it is clipped within the height of the comment textbox/textarea, and I am not able to scroll. (Using Firefox for Android version 85.1.3)
Totally agree with you :)
The short answer was enough for me to mark this article to read cause it’s 99.9% my use case for it.
My first reaction to this was to look at Can I Use to see check on the current state of Flexbox and Grid availability. It looks as if Flexbox is available on around 98% of browsers, Grid on 95% (give or take). On the face of it, that means that around 1 user in 50 will have trouble with a Flexbox-based site and 1 user in 20 will not see a Grid-based site the way you intend it. The latter figure is clearly way too high: if 5% of your users can’t use your site, something’s wrong.
But then it occurred to me that the question isn’t really how many users can’t use Grid, but which users can’t use Grid. Unsurprisingly it turns out to be mostly mobile users on smaller, older devices. And what these users probably need isn’t a complex float-based layout but something much simpler. Put another way, maybe the basic layout styles supported by simple HTML/CSS are not merely adequate but actually appropriate for these users.
There will be some exceptions. If you don’t have Flexbox, there’s no obvious way to turn a list into a horizontal row of items without using float or (shudder) tables. Overall, though, it looks as if we really have reached the tipping point as far as Flexbox and Grid are concerned, and float for layouts can indeed be allowed to die.
I’ve repeatedly run into issues with printing flex-displayed content – page breaks slicing through a line of text or content cut off at a page break entirely – and floats are often the easiest way to fix the problem.
A pull quote on a fluid layout is the only other use case for floats since both flexbox and grid would cause the layout to be very hard to manage. This is the real use of float as designed and the other layouts were created by exploiting the relationship between clear and float.
I see clear as the real loser since there are much less cases than float to use it.
Images are not the only thing you may want to wrap text around. There are a number of other things.
Someone else mentioned a pull quote, which is another great example.
You might want a small video, similar to an image, maybe used as a replacement as a gif, with text wrapped.
Drop caps are another fairly common use for float.
Let’s not forget, float was the first fluid design element and it is still one of the best ways to add interest to long blocks of text.
Many print magazine layout elements can be reproduced most easily with floats. Under the use case of pull quotes one should include all kinds of page enrichment: tips, info blocks, mini link menus, related content, author info etc.
oh, and shape-outside needs float too.
For me, using float instead of grid is more like thinking in 3D, instead of 2D, with float I can easily overlap elements in a more semântic way, using z-index tô controle what is closer tô the screen in a realístic way
Sometimes when you have no control over the order of elements in a component, float might come in handy.
Otherwise Flex!
Float is perfect for assigning an image to the right or left side within the flow of an article. Using flex or grid for that purpose would be a hack.