float:left; vs display:inline; vs display:inline-block; vs display:table-cell;
Solution 1:
Of the options you asked about:
-
float:left;
I dislike floats because of the need to have additional markup to clear the float. As far as I'm concerned, the wholefloat
concept was poorly designed in the CSS specs. Nothing we can do about that now though. But the important thing is it does work, and it works in all browsers (even IE6/7), so use it if you like it.
The additional markup for clearing may not be necessary if you use the :after
selector to clear the floats, but this isn't an option if you want to support IE6 or IE7.
display:inline;
This shouldn't be used for layout, with the exception of IE6/7, wheredisplay:inline; zoom:1
is a fall-back hack for the broken support forinline-block
.display:inline-block;
This is my favourite option. It works well and consistently across all browsers, with a caveat for IE6/7, which support it for some elements. But see above for the hacky solution to work around this.
The other big caveat with inline-block
is that because of the inline aspect, the white spaces between elements are treated the same as white spaces between words of text, so you can get gaps appearing between elements. There are work-arounds to this, but none of them are ideal. (the best is simply to not have any spaces between the elements)
-
display:table-cell;
Another one where you'll have problems with browser compatibility. Older IEs won't work with this at all. But even for other browsers, it's worth noting thattable-cell
is designed to be used in a context of being inside elements that are styled astable
andtable-row
; usingtable-cell
in isolation is not the intended way to do it, so you may experience different browsers treating it differently.
Other techniques you may have missed? Yes.
Since you say this is for a multi-column layout, there is a CSS Columns feature that you might want to know about. However it isn't the most well supported feature (not supported by IE even in IE9, and a vendor prefix required by all other browsers), so you may not want to use it. But it is another option, and you did ask.
There's also CSS FlexBox feature, which is intended to allow you to have text flowing from box to box. It's an exciting feature that will allow some complex layouts, but this is still very much in development -- see http://html5please.com/#flexbox
Hope that helps.
Solution 2:
I usually use float: left;
and add overflow: auto;
to solve the collapsing parent problem (as to why this works, overflow: auto
will expand the parent instead of adding scrollbars if you do not give it explicit height, overflow: hidden
works as well). Most of the vertical alignment needs I had are for one-line of text in menu bars, which can be solved using line-height
property. If I really need to vertical align a block element, I'd set an explicit height on the parent and the vertically aligned item, position absolute, top 50%, and negative margin.
The reason I don't use display: table-cell
is the way it overflows when you have more items than the site's width can handle. table-cell will force the user to scroll horizontally, while floats will wrap the overflow menu, making it still usable without the need for horizontal scrolling.
The best thing about float: left and overflow: auto is that it works all the way back to IE6 without hacks, probably even further.
Solution 3:
I'd say it depends on what you need it for:
If you need it just to get 3 columns layout, I'd suggest to do it with
float
.If you need it for menu, you can use
inline-block
. For the whitespace problem, you can use few tricks as described by Chris Coyier here http://css-tricks.com/fighting-the-space-between-inline-block-elements/.If you need to make a multiple choice option, which the width needs to spread evenly inside a specified box, then I'd prefer
display: table
. This will not work correctly in some browsers, so it depends on your browser support.
Lastly, what might be the best method is using flexbox
. The spec for this has changed few times, so it's not stable just yet. But once it has been finalized, this will be the best method I reckon.