CSS display: inline-block: why it rocks, and why it sucks
Tweet Follow @robertnymanUsually when you want a horizontal list, you need to use float in the CSS code to make it work, with all its drawbacks. However, there is an alternative with display: inline-block.
Problems with float
The problem when you have float in your CSS code is that you need to take some precaution to make the surrounding element to encompass the floated elements, and also to avoid following elements in the code to sneak up next to it. Another problem is that if you have a floated list that will take up several rows (visually speaking) and the content is of varying height, you are in for a world of hurt.
Enter display: inline-block
This is where the magic value inline-block for the display property comes into play. Basically, it’s a way to make elements inline, but preserving their block capabilities such as setting width and height, top and bottom margins and paddings etc. A simple example will look like this:
<style>
ul#display-inline-block-example,
ul#display-inline-block-example li {
/* Setting a common base */
margin: 0;
padding: 0;
}
ul#display-inline-block-example li {
display: inline-block;
width: 100px;
min-height: 100px;
background: #ccc;
}
</style>
<ul id="display-inline-block-example">
<li>Item one</li>
<li>Item two</li>
<li>Item three</li>
</ul>
Rendered results
- Item one
- Item two
- Item three
As you can see, display: inline-block; helps us here to render three square gray boxes next to each other. Awesome, right? However, with varying content, we need to add the property vertical-align: top to mkae sure everything is aligned to the top.
<style>
ul#display-inline-block-example,
ul#display-inline-block-example li {
/* Setting a common base */
margin: 0;
padding: 0;
}
ul#display-inline-block-example li {
display: inline-block;
width: 100px;
min-height: 100px;
background: #ccc;
vertical-align: top;
}
</style>
<ul id="display-inline-block-example">
<li>Item one</li>
<li>Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two</li>
<li>Item three</li>
</ul>
Rendered results
- Item one
- Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two
- Item three
This will render just fine in Firefox, Safari, Google Chrome and IE 8. However, for older versions of Internet Explorer, we need to trigger hasLayout and also use a little hack to set the display to inline (note that this code could be in a separate stylesheet for IE, included via conditional comments – it’s inline here out of simplicity for this example):
<style>
ul#display-inline-block-example,
ul#display-inline-block-example li {
/* Setting a common base */
margin: 0;
padding: 0;
}
ul#display-inline-block-example li {
display: inline-block;
width: 100px;
min-height: 100px;
background: #ccc;
vertical-align: top;
/* For IE 7 */
zoom: 1;
*display: inline;
}
</style>
<ul id="display-inline-block-example">
<li>Item one</li>
<li>Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two</li>
<li>Item three</li>
</ul>
Rendered results that will work in IE 7 too
- Item one
- Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two Item two
- Item three
The enormous drawback
I hope so far that I have managed to get you to realize the potential and simplicity of this approach. However, there’s one giant drawback, that might or might not apply to your use case. That is, since the elements become rendered inline, white-space in your HTML code will affect the rendering. That means, if we want perfect size and alignment, but we have space between the LI elements in our code example, it will render a 4 pixel margin to the right of each element.
Case in point, let’s make it more clear by giving the surrounding UL element a fixed width, wherein the LI elements should fit:
<style>
ul#display-inline-block-example,
ul#display-inline-block-example li {
/* Setting a common base */
margin: 0;
padding: 0;
}
ul#display-inline-block-example {
width: 300px;
border: 1px solid #000;
}
ul#display-inline-block-example li {
display: inline-block;
width: 100px;
min-height: 100px;
background: #ccc;
vertical-align: top;
/* For IE 7 */
zoom: 1;
*display: inline;
}
</style>
<ul id="display-inline-block-example">
<li>Item one</li>
<li>Item two</li>
<li>Item three</li>
</ul>
Rendered results
- Item one
- Item two
- Item three
As you can see, the third item will now fall down to the next row. Which sucks! Hard! However, if we were to place the LI elements directly after each other, they would all be rendered in the same line:
<ul id="display-inline-block-example"> <li>Item one</li><li>Item two</li><li>Item three</li> </ul>
Conclusion
So, the conclusion is that white-space dependent rendering blows! But, there are also great possibilities with display: inline-block, so I advise you to try it out, play around with it and see if it works fine in your specific context.

171 Comments
February 24th, 2010 at 13:53
A little trick to overcome the white-space issue, but still have more or less readable/maintainable code, is to wrap line endings and indentation inside HTML comments:
<code><ul id="display-inline-block-example"><!–
–>Item one<!–
–>Item two<!–
–>Item three<!–
–></code>
It still looks a little messy, but at least it’s more readable compared to a one-liner.
February 24th, 2010 at 14:09
Or you could – gasp!- lose the end-tags…
Was that me who said that? No….
February 24th, 2010 at 15:07
Very interesting post Robert!
As usual, I have a few thoughts to add
White-space dependent rendering is usually a very-bad-idea[tm].
Different developers might not understand that they've made invisible mistakes when updating your carefully crafted code. I am not suggesting we should cater to the lowest common denominator, but this shit is tricky to spot.
Also, you have no control over what happens to your code once it leaves the server, different proxies do very different things to html, and then there is Opera Turbo … does anyone know how it treats whitespace? Some sites suggest that it uses OBML (Opera Binary Markup Language) to transfer pages … I wouldn't be surprised to find that it "optimizes" whitespace.
Furhtermore, the http://reference.sitepoint.com/css/display seems to suggest that IE7 automagically triggers hasLayout for elements with inline-block display, yet you set it in your example. Any thoughts on the different perceptions on this?
February 24th, 2010 at 15:11
I use inline-block from time to time. It can save you a few lines of extra CSS, getting floats to behave. And I love how logical IE7 is — first you declare something inline-block, then switch it to inline to get it working, because that makes perfect sense.
February 24th, 2010 at 18:53
Mathis,
Good input! I have tested that approach, although it indeed feels quite messy.
Lars,
Man, that's pragmatic.
But yes, it would work.
Morgan,
Thanks!
And yes, white-space dependent code sucks, and with the associated trickery, it becomes a maintenance/code handover risk.
With IE 7, in my testing at least, setting hasLayout and changing display to inline has been necessary.
David,
Yeah, IE 7 is a mess…
February 25th, 2010 at 4:35
this can fix the last problem
ul{letter-spacing:-.25em}
li{letter-spacing:normal;}
ul's letter-spacing value is different by font, it may be:
Verdana -.5em
Arial -.25em
Tahoma -.333em
see more http://www.99css.com/?tag=inline-block
you can translate it from chinese to english by using google translate:)
February 25th, 2010 at 12:18
ytzong,
Interesting! I need to experiment with that!
February 25th, 2010 at 12:56
I recently used "display:inline-block" and came the same conclusion.
I have another fix to propose:
ul{font-size:0}
li{font-size:13px;}
In theory you should not be bothered anymore with white-spaces.
February 25th, 2010 at 15:48
Thomas,
Cool, another option to test!
February 25th, 2010 at 16:23
@Thomas
Google's Chrome Browser can't define font-size which value less then 12px at least on chinese version, so your method would be unavailable
February 26th, 2010 at 1:43
The white-space issue is also apparent when using display: inline.
February 26th, 2010 at 11:02
ytzong,
Interesting, didn't know that about Google Chrome.
John,
Absolutely, good point.
February 26th, 2010 at 16:12
I've been using this approach a lot more recently — for layout as well as lists — and I really don't see the white space thing as a serious issue. You just need to be aware of it. And while floats can be made to work in these types of situations, they feel more "hackish" to me and can be a real pain.
February 26th, 2010 at 17:49
Peter,
Right, getting floats to behave exactly as desired can definitely be a pain.
February 27th, 2010 at 14:24
[...] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ [...]
February 27th, 2010 at 17:29
Just wondering, but wouldn't the whitespace property applied to the ul tag be helpfull ?
Didn't test it out or anything, just a hunch
February 27th, 2010 at 21:59
I just happened to be visiting this page using an old Mozilla 1.7 browser. Lotsa rendering problems here.
February 27th, 2010 at 23:21
<code>:before { content:'009'; }</code> is one way to fake whitespace (e.g., indenting) if it is not in HTML.
February 28th, 2010 at 13:30
Here’s another fix for the problem, using a monospaced font and negative values for <code>margin-left</code> and <code>margin-right</code>: http://nerd.vasilis.nl/remove-whitespace-inline-b…
March 1st, 2010 at 9:32
[...] CSS display: inline-block: why it rocks, and why it sucks Via / CSS Globe [...]
March 1st, 2010 at 13:35
Jan,
It's an interesting thought, but apparently it can't be used to remove white-sapce.
Jon_E,
Yes, since this is a private web site, there's no time or need, from my point of view, to adapt a design to a web browser used by less than 0.01% of my visitors.
Sarven,
It's interesting, but only in web browsers that support the <code>:before</code> pseudo protocol.
Mathias,
Interesting! Seems a bit tricky, but might be a viable option. Thanks!
March 1st, 2010 at 17:44
This is a great article about css inlin display, very helpful and informative.
March 1st, 2010 at 17:47
Jordan,
Thank you!
March 1st, 2010 at 19:15
Display:inline-block is also handy when you want to align traditionally block-level content ( say a list or a small form ) within a line-box ( The block level parent ). Remember vertical-align:middle or text-align:center ? This all works with elements that are display:inline-block. Rarely needed, but nice to have when you need it.
March 1st, 2010 at 19:18
dougwig,
Absolutely, good point!
March 2nd, 2010 at 14:31
CSS display: inline-block: why it rocks, and why it sucks…
Usually when you want a horizontal list, you need to use float in the CSS code to make it work, with all its drawbacks. However, there is an alternative with display: inline-block….
March 2nd, 2010 at 23:34
inline-block is the shizzle! In my opinion floats were never meant to be used as a complex layout mechanism, but rather for having text wrap around images and stuff like that.
One huge advantage is that it removes all the hassle of clearing. On the downside though is the gap issue that you mentioned. Is there any logical reason as to why it should behave that way?
March 3rd, 2010 at 11:44
Anders,
Yep, the gap is the only actual issue with it. I believe it comes from a long, troublesome heritage of respecting white-space between inline elements, which is, if you ask me, a terrible thing.
March 4th, 2010 at 21:25
Hi there,
i'm not exactly sure (I'm french) to understand, but problems I often encountered with float, came from "clear" property from the parent element.
To solve it, I use "overflow:hidden" property on parent element, for exemple :
<code>
ul#display-inline-block-example,
ul#display-inline-block-example li {
/* Setting a common base */
margin: 0;
padding: 0;
}
ul#display-inline-block-example {
display: block;
position:relative;
overflow:hidden;
width:100%;/*for exemple*/
}
ul#display-inline-block-example li {
float:left;
width: 100px;
min-height: 100px;
background: #ccc;
}
</code>
What do you think about this tip ?
March 5th, 2010 at 11:32
Christophe,
Using <code>overflow: hidden</code> to make a parent element contain all its floated children elements is a great approach, but it won't be sufficient when the floated children span across multiple rows and and when the items aren't of equal height.
March 7th, 2010 at 5:42
Tables! Let do all the work!
tOM
March 7th, 2010 at 5:44
(actually meant <td>)
March 8th, 2010 at 12:01
tOM,
Well, there are a number of downsides to that to, in terms of semantic value and representation, certain lack of width control etc.
March 23rd, 2010 at 9:21
[...] ???????float:left; ????display:inline-block; ????????? ?????CSS display: inline-block: why it rocks, and why it sucks [...]
April 26th, 2010 at 22:03
[...] According to Robert Nyman: Basically, it’s a way to make elements inline, but preserving their block capabilities such as setting width and height, top and bottom margins and paddings etc. [...]
June 1st, 2010 at 19:59
Hi Robert!
Thank you for this interesting article!
For my personal usage, "display: inline" rocks
I use it to get perfect images gallery and I align for example 3 thumbnails horizontally on a line with equal space on the left and right of my pics.
My CSS code is very simple as I use a "float:left;" with "width:33%;" and an img class including "display: inline".
I used to have display:block and my thumbnails were not perfectly centered. When I switched to inline, they just moved to a perfect equally aligned position.
So for me, yes this is a magic value
Cheers!
Roger
June 2nd, 2010 at 2:08
Roger,
Glad you liked it!
July 9th, 2010 at 17:23
Thank you.
/* For IE 7 */
zoom: 1;
*display: inline; was important for me.
July 25th, 2010 at 13:33
[...] » CSS display: inline-block: why it rocks, and why it sucksRobert’s talk – Web developm… [...]
September 1st, 2010 at 22:18
[...] [...]
September 27th, 2010 at 17:37
Hello,
Have you tried a mix between <code>font-size: 0</code> on parent, and <code>letter-spacing: -1px</code> to prevent the font-size bug on Chrome ?
This seems to work on every browsers
September 27th, 2010 at 18:19
Matbaa,
Yes, that's a good way to trigger layout.
Raphael,
Thanks! It's an interesting approach, but part of me just fear that font-size: 0 could affect some devices (e.g. all kinds of mobile web browsers) or just break in the future, so I would be afraid to use it.
But, perhaps it just magically works for all.
September 27th, 2010 at 18:22
So we just have to wait that CSS3 white-space-collapse: discard; is understood by some browsers
September 28th, 2010 at 12:37
Raphael,
Exactly!
September 30th, 2010 at 21:31
nice posts.. thanks….
October 8th, 2010 at 12:43
[...] you need to know more about inline-block, then check the site here, it’s a good overview (even if it’s a little over the top on the “drawback” [...]
October 15th, 2010 at 9:29
Dang. The <code>font-size:0;</code> works pretty good. My Windows machine is acting up so I can't test it right now, but it looks fine for now. I've always just worked with the inline coding when using inline-block. Not a biggie.
One thing I love about inline-blocks too is the ability to put block elements inside of them. It makes navigation and pagination awesome because you can center stuff.
This is my snippet of code I use for inline-blocks. I'll usually assign multiple elements to it so I don't have to put it in my CSS twice.
<code>
#element_1,#parent > div{
display:inline-block; /* For modern browsers */
*display:inline; /* Hack for IE 7 and less */
zoom:1; /* Hack for IE */
_height:1%; /* Hack for IE6 */
vertical-align:top; /* Just makes sure everything is at the top */
}
</code>
I use inline-blocks for everything now. All my layout is inline-blocks.
October 15th, 2010 at 11:18
Buttars,
Good point!
October 17th, 2010 at 18:46
[...] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ [...]
November 6th, 2010 at 16:33
[...] CSS display:inline-block: Why It Rocks and Why It Sucks [Robert Nyman] [...]
December 1st, 2010 at 5:58
Robert,
Thank you for writing this!! ‘display:inline-block’ helped me with a problem I had while trying to make a navbar that was coded in the HTML as a list and had a set background image for a:hover.
I am a total newb, but this helped with the whitespace problem for me:
#menubar ul li a {
margin-right:-4px
}
It seems like a really simple solution, and it looks great in firefox, but will I have trouble down the road across browser platforms??
December 1st, 2010 at 10:32
davo2323,
You’re welcome!
Your solution might work, but there is also, as you mention, a possible risk cross-platform.
December 21st, 2010 at 21:02
[...] Traduzido e adaptado daqui. [...]
January 28th, 2011 at 1:43
what about white-space:nowrap ? I’ve used this to get around the white-space issue with inline-block. Obviously then on one line…
January 28th, 2011 at 8:57
SirG,
Well, if you have it all on one line, it could work, although its main purpose is to take care of white-space within an element’s content.
But then again, as long as you have no white-space in your HTML in the beginning it would work to, but it’s not an optimal approach.
February 11th, 2011 at 22:27
Beautiful tutorial! As a web developer quite embarrassingly I haven’t used inline-block but definitely plan to from here on out. Thank you.
February 13th, 2011 at 21:52
Eric,
Thanks, glad you liked it!
February 16th, 2011 at 14:36
[...] display: inline-block comes to the rescue. But this new tool in my chest is not perfect. In “CSS display: inline-block: why it rocks, and why it sucks,” Robert Nyman [...]
February 23rd, 2011 at 8:41
[...] Inline-block: why it rocks, and why it sucks Jag har lagt en del tid på att få inline-block att fungera om man zoomar i sin browser, men ett elakt white-space förstör. [...]
March 20th, 2011 at 13:51
[...] CSS display: inline-Block: Why It Rocks, And Why It Sucks [...]
March 22nd, 2011 at 19:53
I didn’t know the “strong” tag could be used inside CSS
March 23rd, 2011 at 9:57
rolfen,
Ha ha! Formatting error – it’s fixed now.
April 6th, 2011 at 7:09
Lars Gunther wrote:
Why does that work? It seems like magic…
April 6th, 2011 at 9:59
Sparrow,
The magic of the web browser automatically self-closing the tags…
June 17th, 2011 at 15:56
[...] FUENTE: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ [...]
July 8th, 2011 at 11:39
I can say that I’m quite a floater, but after I’ve checked some sites with CSS lint it said I use too many floats.
I knew about ‘display: inline-block’, but never got it act the same like floats until I found your useful article.
Thanks for the article and also thanks to some commentators that came with useful additions to it.
July 9th, 2011 at 0:30
Ralph,
Glad it was of use to you!
August 5th, 2011 at 16:58
A better solution for list items is to use:
nav li{float:left; width:auto;}
Has exactly the same visual effect without the headache.
August 5th, 2011 at 20:08
Louisa,
That will not work with content over multiple rows, where the content isn’t of equal height.
August 15th, 2011 at 2:27
Here’s how I get rid of the white-space:
<ul>
<li>block1</li
><li>block2</li
><li>block3</li>
</ul>
It’s perfectly valid, and I haven’t seen anyone mention it here yet…
August 15th, 2011 at 12:29
stephband,
It is, but it’s sad to have to use such an approach, I think.
August 21st, 2011 at 16:32
hi,
very helpful U solve one of my biggest probs with lists…
tnx tnx tnx!
August 31st, 2011 at 13:00
haha stephband ,you are genius!
September 16th, 2011 at 19:20
Though you might be interested in my CSS framework that uses inline-block. I use a monospace font to eliminate the whitespace issue. I’ve tested in thoroughly on all browsers. Obviously Firefox < 2.0 has rendering problems but I'm prepared to live with that. I also use a hack to target Opera because it doesn't understand negative letter-spacing on inline-block nodes, so I have to use word-spacing.
Anyhoo, check it out guys and let me know what you think.
September 16th, 2011 at 22:58
camslice,
Cool, thanks for sharing!
September 17th, 2011 at 16:14
This article saved my day: CSS display: inline-Block: Why It Rocks, And Why It Sucks
October 2nd, 2011 at 16:49
As soon as browsers start to support display:box or display:grid, inline-block will be obsolete.
October 5th, 2011 at 2:22
I’ve been converting a bunch of forms that use tables to using div/span and had already been using the inline-block to get everything to align horizontally but the vertical alignment was not how I wanted. Your suggestion of using the (I guess obvious now) “vertical-align:top” works great. Excellent and helpful post.
David
October 5th, 2011 at 8:58
davidtg,
Glad it was of help to you!
October 8th, 2011 at 5:40
Thank you so much for the tutorial. This saved me tonight!!!!
October 12th, 2011 at 13:28
Fantastic, been struggling with centered lists for years, this solves all my issues! Ta.
October 12th, 2011 at 17:07
Brandon, Laurence,
You’re welcome!
October 13th, 2011 at 22:17
very cool tutorial! i’m thinking of abandoning floats for layout and using display:inline-block or CSS tables framework here http://www.vcarrer.com/2010/10/two-lines-css-framework.html
@camslice or @Robert, was there a link included in his/her comment for us to view an alternative solution?
October 17th, 2011 at 16:30
Chris,
No other link, unfortunately.
And good luck! I believe inline-block and such can be good in some scenarios, but also find floats to still be a viable solution.
October 19th, 2011 at 4:53
[...] Robert Nyman???? Basically, it’s a way to make elements inline, but preserving their block capabilities such as [...]
October 21st, 2011 at 21:19
[...] See also: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ [...]
October 22nd, 2011 at 1:42
Thank you for this helpful article (with helpful comments also)!
I thought I ‘d never figure this alignment stuff out. Finding your site made my day! =)
October 22nd, 2011 at 11:20
missycola,
Good to hear!
November 2nd, 2011 at 21:50
Just throwing in 2 cents: it seems inline-block only works in quirks mode for Firefox. In all DOCTYPE instances I tested, having DOCTYPE declaration at all, causes inline-block to behave as though it were inline instead.
November 2nd, 2011 at 21:57
Declaring any type of DOCTYPE causes Firefox to display inline-block to behave as if display:inline rather than inline-block. QUIRKS Mode specific, which leaves off all the good functionality of the robust doctypes. Seems like its an either or.
November 3rd, 2011 at 0:00
[...] It may suit your needs better, but it’s difficult to say. display: inline-block comes with rendering drawbacks, as does float of [...]
November 3rd, 2011 at 3:36
Cypher,
Not really sure where you get that from: display:inline-block definitely works with a strict doctype in Firefox, and has always done so.
November 14th, 2011 at 5:05
For those who like the font-size:0 solution but want to preserve the relative size of their font (which is important for accessibility):
ul {font-size:1%}
li {font-size:font-size:10000%;}
November 14th, 2011 at 21:29
Nicolas,
Cool, thanks for the tip!
December 4th, 2011 at 14:42
Hi,
to get rid of the white space issue
ul#display-inline-block-example li
{ ..
white-space: nowrap;
}
might work.
December 5th, 2011 at 10:25
Hanno,
Sorry, but that only applies within element contents, not between elements.
December 6th, 2011 at 18:58
To use inline-block, even for page layout, you should use the following CSS:
.container {letter-spacing: -4px;
word-spacing: -4px;
}
.container .inline-block-element {
display: inline-block;
vertical-align: top;
letter-spacing: normal;
word-spacing: normal;
/* for older IE's, as mentioned in the poster's original article */
zoom: 1;
*display: inline;
}
The above has been tested cross-browser and cross-platform. I’m not sure if anyone has posted about this in the comments but using inline-block for layout is the only way for centre aligning variable width content blocks or menu items, for example.
More about using inline-block for page layout on my blog: Holy Grail 3 column layout without using float
December 7th, 2011 at 14:57
BlueAngel,
It’s true about vertical aligning, although overall I don’t find it to be the best approach for main layouts. I just find it counter-intuitive with negative margins and such, even if it works in practice.
December 10th, 2011 at 3:22
Very usefull when i put social icon on the right top of my site, thank you very much for this tutorial!
December 13th, 2011 at 12:14
BlueAngel,
I wouldn’t relay on that. The right gap width is proportional to the font-size; i.e. you get 4 pixels when the parent element font-size is between 12 and 15 pixels.
This is also the reason why setting the parent element font-size to zero can be a fix to the issue.
December 24th, 2011 at 6:45
Something you can do to get around the white space issue, in some instances, is wrap the inline content in a container, and set the background image/styling for the container…sure you still have the “dead space” but visually your vistors won’t be the wiser. Of course, this isn’t a perfect solution for 100% of instances, but it works in some cases.
December 31st, 2011 at 1:18
Patrick,
Yep, it could work in some cases, but it’s not really optimal.
January 3rd, 2012 at 4:19
I always carried a bit of hate for display inline block, mainly because it doesn’t work with older versions of IE (surprise!). However, I might give it a go in my next project now that I know how to properly use it. Thanks for sharing this with us!
January 3rd, 2012 at 8:25
Adele,
Yes, there is a way at least. Good luck!
January 4th, 2012 at 14:50
Actually this is wrong:
“Basically, it’s a way to make elements inline, but preserving their block capabilities such as setting width and height, top and bottom margins and paddings etc.”
Since top and bottom margins, paddings osv. is ignored for inline elements:
“If you try to add dimension to an inline element, some properties will be applied, some properties will be partially applied and others will not be applied at all. The most noticable properties are width, height, margin and padding.
Margins operate in the same way as padding on inline elements. In the example below, 50px of margin has been applied to all sides of the element. While the left and right edges are effected, the content above and below are not:”
Source: http://www.maxdesign.com.au/articles/inline/
January 4th, 2012 at 17:02
magnemg,
Not sure what you mean. As described by me in the sentence you quoted, they are like inline elements but with block element abilities like margin, padding etc on all sides intact (i.e. working).
January 9th, 2012 at 7:01
[...] For the menu elements, instead using float I chose the inline-block alternative. If you want to find out more about inline-block pros and cons, I’d recommend Robert Nyman’s article: CSS display: inline-block: why it rocks, and why it sucks. [...]
January 14th, 2012 at 19:00
Hi there! I want to thank you a LOT! You have explained that perfectly! I used that for the menu in my web site!
Again – THANK YOU!
January 14th, 2012 at 23:44
Hristiyan,
Good to hear – glad it was of help!
January 17th, 2012 at 18:26
Why dont you just use display: table-cell;
January 18th, 2012 at 10:06
Se3ker,
Because a table cell and inline block element don’t really have the same rendering characteristics.
January 25th, 2012 at 6:01
If you want to use 3 elements in line (300px total for 3*100px) – just set font-size:0 for container. This is very logical in this case and now you can use all benefits from inline-blocks, like align:justify!
January 25th, 2012 at 10:01
Vladimir,
Yes, it works. Although I’d argue that it’s not really logical with font-size for spacing.
January 25th, 2012 at 14:03
[...] For the menu elements, instead using float I chose the inline-block alternative. If you want to find out more about inline-block pros and cons, I’d recommend Robert Nyman’s article: CSS display: inline-block: why it rocks, and why it sucks. [...]
February 22nd, 2012 at 8:01
[...] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ [...]
February 28th, 2012 at 15:56
[...] [...]
February 29th, 2012 at 7:00
[...] Read more about the CSS display inline-block value: Why it rocks, and why it sucks. [...]
March 1st, 2012 at 17:26
Why not simply use display:block;float:left; for the LI definition??
This way the list items are displayed side by side, retain block properties that allow you to set width, padding, etc; and works in all major browsers.
March 1st, 2012 at 21:02
Valthino,
The cons are described under the Problems with float heading. Also, display: block is not needed since giving it float will give you access to those properties by itself.
March 2nd, 2012 at 21:34
I handle the white-space like this.
ul { white-space: nowrap; }
li { white-space: wrap; }
Now you have all your li tags in one line and wrapping inside each li.
March 2nd, 2012 at 23:58
Ram,
Yes, it could work for some scenarios.
March 12th, 2012 at 21:50
[...] cuenta de que todo es flexible. Todo se adapta a tu pantalla. Que surgen cientos de iniciativas e ideas que te empujan a adaptarte a los cambios, que te ayudan a explorar nuevas formas de hacer las [...]
April 7th, 2012 at 4:27
very nice trick. IE always has problems with CSS and with this inline-block, fix everything. thanks
April 30th, 2012 at 21:44
Thanks!It helped me a lot!!!
May 10th, 2012 at 17:09
Thank you very much for your excellent explanation of this option. Here are my two cents to remove the “sucks” part from the title:
I solved this annoyance with a stupidly simple trick: simply avoid to place any space-like character (like the carriage return itself) between the consecutive tags, i.e.:
<div ….><div ….><div ….>
or
<li ….><li ….><li …>
instead of
<div ….>
<div ….>
<div ….>
or
<li ….>
<li ….>
<li …>
It works flawlessly in Chrome, Firefox and IE9. The elements are displayed next to each other with no separation. It seems the browsers consider any carriage return embedded in the code as a blank character meant to be displayed as text (while it does not make much sense if you think about it, since such return characters are part of the structural glue of the code, not the visible content of the page). If you stick the tags together with no blank space or carriage return between them, the inline-block works fine and the visible gap disappears.
May 10th, 2012 at 17:13
Sorry, I forgot the semicolons!
<div ….><div ….><div ….>
or
<li ….><li ….><li …>
instead of
<div ….>
<div ….>
<div ….>
or
<li ….>
<li ….>
<li …>
May 11th, 2012 at 13:36
Manuel,
Yes, it solves the problem, but I hate that we have to take white-space into account.
May 15th, 2012 at 4:49
Note this:
Block
Takes up the full width available, with a new line before and after (display:block;)
Inline
Takes up only as much width as it needs, and does not force new lines (display:inline;)
June 7th, 2012 at 15:11
ul{letter-spacing:-.25em}
li{letter-spacing:normal;}
works for me but the white space is still there. my li’s are 300 wide and 900px for the UL won’t fit i have to give the UL 920px … (?)
June 7th, 2012 at 21:07
hans,
It can be a bit tricky, so you need to experiment a bit with it.
June 9th, 2012 at 0:42
Hey Robert,
I’ve been using this code for a while now to combat this issue:
ul {
display: table;
font-size: 0rem;
width: 100%;
}
li {
display: inline-block;
font-size: 1rem;
}
…with
{display:inline;zoom:1;}for IE, of course.It seems to solve the issue everywhere I’ve tested.
June 12th, 2012 at 22:01
Bill,
Interesting approach with the rem unit!
June 14th, 2012 at 15:54
Thanks, Robert.
I know that 0rem === 0em === 0px === 0, but in this case, keeping the rem on the zero seems to allow browsers that do not recognize the unit to skip that property, meaning I have less compensation to do in the proceeding rule. Everybody else seems to fix the issue with the display:table/display:inline-block combination. I’ve yet to find a situation where it didn’t adequately compensate for the extra spacing.
Anyway, thought you and your readers might find it helpful.
June 14th, 2012 at 19:57
Bill,
Yep, any combo/alternative approach is good to know about!
July 18th, 2012 at 12:07
[...] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ [...]
July 18th, 2012 at 13:15
hi to all can any one solve this task.
http://www.codeproject.com/Questions/423710/How-to-display-multiple-ul-inside-li-as-single-blo
July 20th, 2012 at 15:37
[...] what you mean…Actually this is quite a nasty one, and I found the reason here: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/.Basically when you have a list item that is "display: inline-block;" styled (which the [...]
July 22nd, 2012 at 8:50
By combining several methods, mentioned in the comments above, I found the ultimate solution, independent of the font and its size:
ul { font-size: 1%; *word-spacing:-1%; }
li { font-size:10000%; word-spacing normal }
along with the common code.
Works in Ie6+, FF 3+ , webkit, etc..
July 31st, 2012 at 16:53
jawahar,
Hopefully someone will see the link and can help you.
marian,
Glad you found a way that works for you!
August 5th, 2012 at 5:30
[...] Uh-oh! We set all the margins to zero (0) pixels, but the space between each list items is still there. This is caused, bizarrely enough, by the whitespace between the HTML list item tags. To solve this, remove the new line and tab characters between the closing tag (</li>) of one list item and the opening tag (<li>) of the next. Earlier version of Microsoft’s Internet Explorer require another step. [...]
August 16th, 2012 at 17:17
[...] of an inline element.Inline-Block is a potential alternative to floating elements but certainly isn’t without its issues in these circumstances.Another display value, that isn’t as well used is list-item, which does exactly what it says [...]
August 16th, 2012 at 19:48
ul { word-spacing: -1em; }
li { display: inline-block; *display: inline; *zoom: 1; word-spacing: normal; }
There, you have it. Works fine.
P.S.: If you want a line of items with a gap in between them (i.e. 10px), I recommend the following:
ul { word-spacing: -1em; }
li { display: inline-block; *display: inline; *zoom: 1; margin-left: 10px; word-spacing: normal; }
li:first-child { margin-left: 0; }
That way the last item on the row won’t break.
August 17th, 2012 at 3:07
[...] Inline-Block is a potential alternative to floating elements but certainly isn’t without its issues in these circumstances. [...]
August 20th, 2012 at 11:48
Luis,
Thanks for the suggestions!
August 20th, 2012 at 21:49
[...] Inline-Block is a potential alternative to floating elements but certainly isn’t without its issues in these circumstances. [...]
September 28th, 2012 at 16:53
This is a great article, and the comments have been really helpful. It’s a shame that the flaws of inline-block essentially boil down to legacy and semantics, though. Fortunately, the company I work for only supports specific browsers (only the latest 2 versions of IE, also, thank God!) in our contracts so I can get away with some of these risky fixes.
October 1st, 2012 at 12:41
Mike,
Thanks! And good to hear it’s an option for you!
October 3rd, 2012 at 3:41
[...] Read: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ Share this:TwitterFacebookLike this:LikeBe the first to like this. This entry was posted in [...]
October 4th, 2012 at 21:05
[...] is very important to know the distinction between these terms, see this website for more [...]
October 4th, 2012 at 21:06
[...] is very important to know the distinction between these terms, see this website for more [...]
October 25th, 2012 at 19:42
This is an extremely useful post. Thanks for sharing. This saved my rear end last night when I was trying to debug some display issues!
November 11th, 2012 at 18:34
[...] contents occupy. Inline elements will sit next to each other as far as their container will allow. Inline-block level elements shrink-wrap around their contents like inline level elements, while allowing [...]
November 15th, 2012 at 0:08
[...] bottom of the wrapper. So be it, for now.Without any unwanted white-space but including IE hacks (a small inline-block fix for IE6/7 is also implemented here using zoom: 1; and *display: inline), the final solution looks [...]
November 19th, 2012 at 5:41
[...] go into all the detail of that, but suffice it to say it took a lot of Googling, bookmarking key pages and finally using the awesome resource that is Stack Overflow. I couldn’t possibly [...]
November 22nd, 2012 at 0:06
inline-block works on IE6&7 but ONLY if you declare it on an element that is naturally inline such as span.
November 22nd, 2012 at 12:03
VBAssassin,
Thanks, good to know!
December 10th, 2012 at 7:26
I am a little confused here. . . Why not HTML ?. . . not all are depracated
item1item2item3
This is like the Windows Update Scam from Microsoft to get our visited websites History. Windows never is protected or is better until you buy the next Windows version. Microsoft sell our information to Walmart, Arlines, etc. Windows Updates is a marketing tool. To Retrieve and UpLoad our Information. . . Mike@mikescomputertips.com
December 10th, 2012 at 7:35
Ahh ! here we go again. . .
<table width="250"><tr>
<td>item1</td><td>item2</td><td>item2</td>
</tr></table>
December 11th, 2012 at 10:41
thanks for your css article, I’m using display: inline-block; to expand the container div.
January 17th, 2013 at 22:23
I notice that this property (display:inline-block) is needed if you want to stretch text with transform:scale.
January 22nd, 2013 at 17:23
Thanks friend…
i’ll try it little by little..:)
February 12th, 2013 at 22:06
.plop1{
position:relative;
right:0;
}
.plop2{
position:relative;
right:4;
}
.plop3{
position:relative;
right:-200;
top:-100;
}
ul#display-inline-block-example,
ul#display-inline-block-example li {
/* Setting a common base */
margin: 0;
padding: 0;
}
ul#display-inline-block-example {
width: 300px;
border: 1px solid #000;
}
ul#display-inline-block-example li {
display: inline-block;
width: 100px;
min-height: 100px;
background: #ccc;
vertical-align: top;
/* For IE 7 */
zoom: 1;
*display: inline;
}
Item one
Item two
Item three
February 12th, 2013 at 22:09
Sorry for that last comment by me. It seems I can’t comment the full code to you.
Add this in your style tag:
.plop1{
position:relative;
right:0;
}
.plop2{
position:relative;
right:4;
}
.plop3{
position:relative;
right:-200;
top:-100;
}
Place the classes:
class=”plop1″ to item 1
class=”plop2″ to item 2
class=”plop3″ to item 3
February 18th, 2013 at 0:36
Thank you for this. I was needing something to update on of our photos page. This helped alot!
March 1st, 2013 at 5:36
[...] Menurut Robert Nyman: [...]
April 1st, 2013 at 8:29
I can fix this with this:
CSS:
#wrapper {
display: table;
width: 1000px;
}
#wrapper #divOne {
display: inline-block;
width: 100px;
}
#wrapper #divTwo {
display: inline-block;
width: 800px;
}
#wrapper #divThree {
display: inline-block;
width: 100px;
}
The HTML:
Content
Content
Content
And tadaaaaa!
April 16th, 2013 at 14:37
[...] Read more about the CSS display inline-block value: Why it rocks, and why it sucks. [...]
April 18th, 2013 at 6:55
[...] For the menu elements, instead using float I chose the inline-block alternative. If you want to find out more about inline-block pros and cons, I’d recommend Robert Nyman’s article: CSS display: inline-block: why it rocks, and why it sucks. [...]
April 22nd, 2013 at 21:27
No, its quite OK to use them; the drawback you mentioned is secondary on screens – maybe in 1% of all cases you need that absolute perfect 100%-correct alignment, i’d say. So, why make it harder than its actually is?
April 26th, 2013 at 2:01
[...] Aligning: If you have a few images, or divs that you want to line up horizontally, you have two options: 1) you can use the CSS float (will cover in a later article) or you can change the default display from block to inline-block. To see more about how this works (and an example of the drawbacks of inline block, see this wonderful blog post.) [...]
May 4th, 2013 at 15:27
[...] is great when you don’t want to use floats, the only issue with this method, as discussed here, is that white space in your HTML affects the positioning of your [...]
Write a comment