Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


I'd say it depends on what you need it for:

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.

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


@ShitalShah: I'm not quite sure what you mean by "this", but I've implemented countless horizontal menu and the float left and overflow is IMO the cleanest and most reliable way to implement horizontal menu across many different browsers, old and new. For adding separators, I'd usually use left border (or background images for fancier separators, if you don't need to care about old browser's then you can also use CSS3 border-image). I don't quite understand what you mean by differing heights, want to explain more or add a fiddle?

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 best thing about float: left and overflow: auto is that it works all the way back to IE6 without hacks, probably even further.

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.

This cleared up a lot about clearing and I didnt know that overflow could do this. Im used to the :after method where u add a content: "." and visibility:hidden. Thanks for this

This excellent point, not mentioned in MANY popular tutorials. Couple of questions: Is this actually tested in different browsers? What's your method of choice to draw separator between two blocks such as nav and content? The reason I ask is because if above divs are of different height it would be difficult.

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


@ShitalShah: I'm not quite sure what you mean by "this", but I've implemented countless horizontal menu and the float left and overflow is IMO the cleanest and most reliable way to implement horizontal menu across many different browsers, old and new. For adding separators, I'd usually use left border (or background images for fancier separators, if you don't need to care about old browser's then you can also use CSS3 border-image). I don't quite understand what you mean by differing heights, want to explain more or add a fiddle?

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 best thing about float: left and overflow: auto is that it works all the way back to IE6 without hacks, probably even further.

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.

This cleared up a lot about clearing and I didnt know that overflow could do this. Im used to the :after method where u add a content: "." and visibility:hidden. Thanks for this

This excellent point, not mentioned in MANY popular tutorials. Couple of questions: Is this actually tested in different browsers? What's your method of choice to draw separator between two blocks such as nav and content? The reason I ask is because if above divs are of different height it would be difficult.

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


@ShitalShah: I'm not quite sure what you mean by "this", but I've implemented countless horizontal menu and the float left and overflow is IMO the cleanest and most reliable way to implement horizontal menu across many different browsers, old and new. For adding separators, I'd usually use left border (or background images for fancier separators, if you don't need to care about old browser's then you can also use CSS3 border-image). I don't quite understand what you mean by differing heights, want to explain more or add a fiddle?

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 best thing about float: left and overflow: auto is that it works all the way back to IE6 without hacks, probably even further.

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.

This cleared up a lot about clearing and I didnt know that overflow could do this. Im used to the :after method where u add a content: "." and visibility:hidden. Thanks for this

This excellent point, not mentioned in MANY popular tutorials. Couple of questions: Is this actually tested in different browsers? What's your method of choice to draw separator between two blocks such as nav and content? The reason I ask is because if above divs are of different height it would be difficult.

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


@ShitalShah: I'm not quite sure what you mean by "this", but I've implemented countless horizontal menu and the float left and overflow is IMO the cleanest and most reliable way to implement horizontal menu across many different browsers, old and new. For adding separators, I'd usually use left border (or background images for fancier separators, if you don't need to care about old browser's then you can also use CSS3 border-image). I don't quite understand what you mean by differing heights, want to explain more or add a fiddle?

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 best thing about float: left and overflow: auto is that it works all the way back to IE6 without hacks, probably even further.

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.

This cleared up a lot about clearing and I didnt know that overflow could do this. Im used to the :after method where u add a content: "." and visibility:hidden. Thanks for this

This excellent point, not mentioned in MANY popular tutorials. Couple of questions: Is this actually tested in different browsers? What's your method of choice to draw separator between two blocks such as nav and content? The reason I ask is because if above divs are of different height it would be difficult.

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


inline-block

Of the options you asked about:

Other techniques you may have missed? Yes.

  • 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 that table-cell is designed to be used in a context of being inside elements that are styled as table and table-row; using table-cell in isolation is not the intended way to do it, so you may experience different browsers treating it differently.
  • float:left; I dislike floats because of the need to have additional markup to clear the float. As far as I'm concerned, the whole float 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.

@AlexMorley-Finch: Indeed, that is one good solution. Be aware, however that it doesn't work if you're using font sizes in ems. That means it may not be a suitable solution for all cases. You may also like to see this answer I posted separately, which covers this point, and other options for dealing with the white space issue.

A good method for removing the whitespace that display:inline-block creates, you can set the font-size to 0 on the container element, then set the desired for the inline elements themselves. This avoids to need to remove whitespace in the html

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.

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)

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


inline-block

Of the options you asked about:

Other techniques you may have missed? Yes.

  • 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 that table-cell is designed to be used in a context of being inside elements that are styled as table and table-row; using table-cell in isolation is not the intended way to do it, so you may experience different browsers treating it differently.
  • float:left; I dislike floats because of the need to have additional markup to clear the float. As far as I'm concerned, the whole float 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.

@AlexMorley-Finch: Indeed, that is one good solution. Be aware, however that it doesn't work if you're using font sizes in ems. That means it may not be a suitable solution for all cases. You may also like to see this answer I posted separately, which covers this point, and other options for dealing with the white space issue.

A good method for removing the whitespace that display:inline-block creates, you can set the font-size to 0 on the container element, then set the desired for the inline elements themselves. This avoids to need to remove whitespace in the html

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.

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)

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


inline-block

Of the options you asked about:

Other techniques you may have missed? Yes.

  • 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 that table-cell is designed to be used in a context of being inside elements that are styled as table and table-row; using table-cell in isolation is not the intended way to do it, so you may experience different browsers treating it differently.
  • float:left; I dislike floats because of the need to have additional markup to clear the float. As far as I'm concerned, the whole float 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.

@AlexMorley-Finch: Indeed, that is one good solution. Be aware, however that it doesn't work if you're using font sizes in ems. That means it may not be a suitable solution for all cases. You may also like to see this answer I posted separately, which covers this point, and other options for dealing with the white space issue.

A good method for removing the whitespace that display:inline-block creates, you can set the font-size to 0 on the container element, then set the desired for the inline elements themselves. This avoids to need to remove whitespace in the html

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.

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)

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


I'd say it depends on what you need it for:

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.

Note
Rectangle 27 0

html float:left; vs display:inline; vs display:inline block; vs display:table cell;?


I'd say it depends on what you need it for:

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.

Note