10 ms·
The end of the clearfix hack?
- dutchbrit 10y agoI usually use overflow:hidden on containers, what are the pro's and cons of this compared to clearfix?
- martin-adams 10y agoInteresting, I did not know that overflow:hidden resized the parent container to the floated elements. It looks like the difference may be some legacy IE6 thing.
- thomasahle 10y agoWhy doesn't it just crop the element to fit, as with everything else?
- martin-adams 10y agoThat was my immediate thought then I tried it out. I suspect it's to do with causing some hasLayout which recalculates the content dimensions.
- SippinLean 10y agohasLayout is IE-only and dead since IE7
- rossriley 10y agoYou can set overflow to visible too, just setting overflow to non-auto is enough to trigger it.
- SippinLean 10y agoNo, just auto, hidden, overlay, and scroll. Visible will not trigger the clearfix.
- deleted 10y ago[deleted]
- deanclatworthy 10y agoThis is my go-to fix also. Have used it for years with no side effects or bugs as a result.
- rimantas 10y agoAlways used that one and could not care less about clearfix.
- SippinLean 10y agoIn case you want elements to appear outside the parent, like a dropdown menu. In ancient times some browsers required setting a width or height as well.
- ryanSrich 10y agoThis will cut off larger diffuse drop shadows, which is one immediate downside I can think of.
- ygra 10y agoSo a draft standard that's implemented in the current beta (alpha?) versions of Chrome and Firefox. So the end for the hack is nigh ... in about 5 years, I guess.
- stupidcar 10y agoThis isn't really true anymore. All the major browser vendors push out new releases pretty frequently. Even Apple and Microsoft tend to put out a new release at least every six months or so. And because of the ubiquity of software / OS updates, these tend to get deployed to the majority of users fairly rapidly. For example, consider a feature like arrow function support in JavaScript: http://caniuse.com/#feat=arrow-functions http://caniuse.com/#feat=arrow-functions Click "usage relative" to scale by the relative popularity of different browser versions. Despite only being standardised and implemented in browsers last year, arrow functions are already supported in the large majority of deployed web clients, including Edge, Firefox, Chrome and Safari, both desktop and mobile. Nowadays, new features, particularly relatively small and uncontroversial ones like display: flex-root, can get implemented across browsers very quickly.
- Bahamut 10y agoA lot of people still have to support IE10/11, including myself - until browser support is mostly on evergreen browsers & browsers keep up to date on implementing the new standards, ability to use the standards hinges upon a couple of years buffer in some cases. It still is largely true unfortunately.
- stupidcar 10y agoAs I already said: browser support is mostly on evergreen browsers, and browsers do keep up to date implementing the new standards. The stats make this very clear. To say otherwise is to promote a view of the world that is ten years out of date. IE10/11 are at about ~4% global usage. Less than Opera Mini. And a large number of those users will have access to another browser. We've found many enterprises keep IE around to access legacy services, but let people install Chrome in order to access things that need a modern browser. The days when it was worth supporting IE for new development work has long since passed. Company policies that mandate doing so should be challenged as a waste of time and money.
- neals 10y agoI've moved away from float entirely and mastered the Flexbox. Which has made my live easier.
- masklinn 10y agoFloats remain useful for their original purpose: floating an inset piece of content inside a wider textual context e.g. an illustration picture or a pull quote. And for that you might still need a clearfix-type workaround.
- davnicwil 10y agoYes, and further to this - genuine question - Why would you use float-based layouts over flexbox for reasons other than backwards compatibility? About the only reason I can think of off the top of my head is that you / your team already fully grok floats, they are sufficiently powerful for your use case, and you don't want to invest the time to learn flexbox to achieve the same outcome as you could already achieve.
- andrei_says_ 10y agoSusy grid system is currently float-based and the most efficient way I'm aware of to create semantic CSS layouts with wide compatibility. http://susy.oddbird.net/ http://susy.oddbird.net/
- Bahamut 10y agoIn this case, flexbox actually is a bit awkward - one is often forced to pair flexbox with calc in this sort of situation. If the content, including text, is too long, it pushes the right container down a level unless the width is fixed to an appropriate size - this is doable with calc (for fluid layout) if one has control over the size of the left container, but otherwise it may be difficult to obtain the desired UI with flexbox.
- devwastaken 10y agoI really like flexbox, but its still missing functionality and somewhat misleading in its options. Everything is row-column based, but you can select between row or column, but in the column option, only justify-content will position content vertically. If you want something like forcing an element to be in the middle vertically, and have an element above it, you can't do that. align-self only works on rows. I really don't understand why they didn't make it work vertically if you have column mode.
- OJFord 10y agoI'm not really a front-end kind of person, but whenever I've written any CSS this has, I think, literally always bitten me. I try to read about it and understand it 'properly' every time, but clearly I never do. Why is the unhacked behaviour desirable? If I put a thing in a thing, surely I want it to stay in the thing?!
- masklinn 10y agoBecause the original purpose of floats is not layout (where you want the thing to stay in the other thing) it's typographic floats, like pull quotes or info boxes or pictures. In that case, even if your float is in a paragraph you don't want to "expand" the paragraph past the end of the float if the float is longer than the paragraph, you want the following paragraph to be laid out around the float instead. Here's a demo: http://codepen.io/anon/pen/ggGgWb http://codepen.io/anon/pen/ggGgWb Here's the "clearfixed" version which is nonsensical: http://codepen.io/anon/pen/xgXgry http://codepen.io/anon/pen/xgXgry That floats were press-ganged into layout does not mean their basic behaviour is bad, it just means they were used for something that was never intended (due to a lack of actual layout tools in CSS until CSS3) and they were not very good at it. And note that the lack of layout tools was not limited to "applicative" layouts, most papers are laid out in multiple text columns yet that fairly simple and common layout was not supported either until CSS3.
- schneidmaster 10y agoThis is a great explanation, thanks. I'd never quite understood this either.
- lomnakkus 10y agoThose demos look completely identical on my browsers (Chromium, Firefox). The CSS change is there, but the rendered output looks identical.
- masklinn 10y agoThat is odd, I just opened them in Firefox 51 and I do get the rendering I was expecting: http://imgur.com/a/Bke7v http://imgur.com/a/Bke7v
- bartl 10y agoI don't like the idea that this is tacked onto the "display" property. "display" is for the outer display properties of the div, but this flow-root is for the inside. You cannot have a div that is both `display: inline-block` and `display: flow-root` at the same time, for example.
- mercer 10y agoNothing that yet another otherwise meaningless wrapping div won't solve..
- masklinn 10y ago> I don't like the idea that this is tacked onto the "display" property. "display" is for the outer display properties of the div That's really not true[0], "display: flex", "display: grid" or "display: table" are all inner display properties, which is why inline-flex, inline-grid or inline-table exist ("display: flex" sets the outer display mode to "block" and the inner one to "flex", "display: inline-flex" sets the outer display mode to "inline" and the inner one to "flex"). Which is why CSS3 split display properties between outer display (block, inline or run-in) and inner display (flow, flow-root, table, flex, grid or ruby), which can be specified independently e.g. `display: inline flow-root`. Furthermore `display: inline-block flow-root` makes no sense 1. `inline-block` is specifically a "legacy" display property which impacts both inner and outer display (and thus is not compatible with a separate inner display property) 2. `inline-block` already means `inline flow-root` so `display: inline-block flow-root` would expand to `display: inline flow-root flow-root` [0] and has not been since CSS2 and display: table, possibly CSS1's display: list-item
- jpfed 10y agoThat makes it look like the "display" property will turn into a css shorthand for "inner" and "outer" display properties... is that the case?
- masklinn 10y agoIt is already that, it's been that pretty much all along, but for most of its history you couldn't set the inner and outer variants separately, you had to use "precomposed" values like inline-table. AFAIK you can't set the inner and outer display types via separate properties. Furthermore there are values which have more complex interactions/natures than just inner/outer e.g. "contents" (which removes the element's box but leaves the descendants — including pseudo-elements), "list-item" (which can be combined with any outer type but only the flow or flow-root inner types, and generates an internal pseudo-element), or the "internal values" which only make sense within wider display context (table and ruby).
- nailer 10y agoJust use flex and avoid the float mess entirely. (Obviously: starting from date supporting old browsers costs more than supporting them brings in, which depends on your product and your customers)
- JustSomeNobody 10y ago> The end of the clearfix hack? Not until all browswers support it.
- pix64 10y agoYou could make a JS polyfill fairly easy.
- dinodino 10y agoI do agree, we still need to wait for all older browser to implement this (especially think of all the mobile browsers nowadays). If making a polyfill is the solution, we risk ending up with a bigger polyfill file than the actual CSS implementation in the browser. If that is the case we can better do styling with JS.
- JohnDotAwesome 10y agoDoes anyone know the drawbacks of simply using: .container { display: table; width: 100%; } See bottom example here http://codepen.io/jrf0110/pen/xgXPGj http://codepen.io/jrf0110/pen/xgXPGj I've never had to use the clearfix hack (that's probably a lie, but I can't recall a time in my career where I didn't just use the display: table; width: 100%; method)
- camtarn 10y agoIt wasn't supported until IE8. For those of us who had to support IE6 till well after its sell-by date, prop-based clearing was an important tool in the toolbox.
- talmand 10y agoIf the browser doesn't support display: table, you could just put overflow: auto on the container and it'll expand to contain the floats.
- smaili 10y agohttp://caniuse.com/#feat=css-table http://caniuse.com/#feat=css-table
- drinchev 10y ago`float` should not be used for layouts, but for placement an image inside text ( e.g ), which is it's purpose. Nowadays I don't use floats anymore. Flexbox is the current layout recommendation and I encourage you not to create legacy CSS stylesheet layouts with float.
- camtarn 10y agoTo borrow and mangle a Zawinski quote: 'Some people, when confronted with a layout problem, think "I know, I'll use floats." Now they have two problems.'
- talmand 10y agoI've seen enough comments about using floats for layout that I think some people need to read the article. The example in the article is not using float for layout. It is using it for the original purpose, floating an element to the side for other elements to flow around. The problem shown is when the floated element is taller than the resulting containing parent. Which is a common problem and a valid use of float.
- dfar1 10y agoI will remove clearfix from my stylesheets once ie9 supports it, or in 2050.. .whichever one comes first. :)
- ino 10y agoI see that you can use "height: 100%" on the inner item to fill the height of the parent that has "display: flow-root". I've had difficulties implementing similar things in the past.
- bchen 10y agoTo make a child element the same height as the parent, you can set the parent container to position: relative, and in the child element, set it to position: absolute; top: 0; bottom: 0. The downside of this trick is that the child element is now absolutely positioned and no longer behaves the same. Alternatively, you can use flexbox to do this, if using it is an option.
- pix64 10y agoMy favorite use of this is making a resizable box that always maintains the same aspect ratio. Padding % is out of the element's width. So width: 100%, padding-top: 50% will make a block that is 2:1 that resizes automatically.
- wwweston 10y agoIsn't the other downside that you have to know beforehand which column in the layout is going to be of a larger (and possibly unknown) size?
- deleted 10y ago[deleted]