float:left; vs display:inline; vs display:inline-block; vs display:table-cell;
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.
float:left vs display:inline-block
One advantage of using inline blocks is that you can center them horzontally using `text-align: center;' on the container element.
The space you write about is due to whitespace resulting from using line-breaks in the code.
float:left; vs display:inline; vs display:inline-block; vs display:table-cell;
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.
Using CSS display:inline-block or float:left with mixed amounts of text
Using table based markup is not the answer. However, iff you don't need to support IE7 or lower, you can use display:table
to solve this. Check out this demonstration i threw together. Edit the amount of content in the second child div to see the effect.
jsfiddle demonstrating display:table
.parent {
display:table;
}
.child {
display:table-cell;
}
Basically, you tell the parent
element to act like a table, the two child
elements to act like table cells. This gives you the benefits of the table layout without the accessibility problems and extra markup of html tables. As I mentioned though, this doesn't work in IE7. If you need old IE support, you'll have to resort to less graceful workarounds :(
What are the most important advantages of using flexbox instead of just using display: inline
...arguments why flexbox is a good alternative to the old inline/float-approach...
Why Flexbox?
For a long time, the only reliable cross-browser compatible tools
available for creating CSS layouts were features like floats and
positioning. These work, but in some ways they're also limiting and
frustrating.The following simple layout designs are either difficult or impossible
to achieve with such tools in any kind of convenient, flexible way:
Vertically centering a block of content inside its parent.
Making all the children of a container take up an equal amount of the available width/height, regardless of how much width/height is
available.Making all columns in a multiple-column layout adopt the same height even if they contain a different amount of content.
MDN flexbox
Related Topics
Angular Material Mat-Drawer in Full Height Flex, Content Overflow Auto
How to Make a Floated Div 100% Height of Its Parent
What Does the "≫" (Greater-Than Sign) CSS Selector Mean
How to Have Multiple Background Images Using Css
Can Media Queries Resize Based on a Div Element Instead of the Screen
Better Way to Set Distance Between Flexbox Items
Can the :Not() Pseudo-Class Have Multiple Arguments
Css Image Size, How to Fill, But Not Stretch
Css: How to Say .Class:Last-Of-Type [Classes, Not Elements!]
Is the Viewport Meta Tag Really Necessary
How to Make an Svg Scale With Its Parent Container
Font Scaling Based on Width of Container
Reset/Remove CSS Styles For Element Only
Border-Radius in Percentage (%) and Pixels (Px) or Em
Css '≫' Selector; What Is It