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
.
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 make 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.
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.
Or you could – gasp!- lose the end-tags…
Was that me who said that? No….
You could even put the after the newline, as in
Item one
Item two item two item two item two item two
Item three
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?
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.
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… 🙂
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:)
ytzong,
Interesting! I need to experiment with that!
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.
Really thanks to give solution for removing space,while using display:block and display:inline-block.Great keep going
Thomas,
Cool, another option to test!
@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
The white-space issue is also apparent when using display: inline.
ytzong,
Interesting, didn't know that about Google Chrome.
John,
Absolutely, good point.
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.
Peter,
Right, getting floats to behave exactly as desired can definitely be a pain.
[…] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
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 🙂
I just happened to be visiting this page using an old Mozilla 1.7 browser. Lotsa rendering problems here.
<code>:before { content:'009'; }</code> is one way to fake whitespace (e.g., indenting) if it is not in HTML.
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…
[…] CSS display: inline-block: why it rocks, and why it sucks Via / CSS Globe […]
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!
This is a great article about css inlin display, very helpful and informative.
Jordan,
Thank you!
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.
dougwig,
Absolutely, good point!
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….
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?
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.
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 ?
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.
Tables! Let do all the work!
tOM
(actually meant <td>)
tOM,
Well, there are a number of downsides to that to, in terms of semantic value and representation, certain lack of width control etc.
[…] ???????float:left; ????display:inline-block; ????????? ?????CSS display: inline-block: why it rocks, and why it sucks […]
[…] 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. […]
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
Roger,
Glad you liked it!
Thank you.
/* For IE 7 */
zoom: 1;
*display: inline; was important for me.
[…] » CSS display: inline-block: why it rocks, and why it sucksRobert’s talk – Web developm… […]
[…] […]
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
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. 🙂
So we just have to wait that CSS3 white-space-collapse: discard; is understood by some browsers 🙂
Raphael,
Exactly! 🙂
nice posts.. thanks….
[…] 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” […]
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.
Buttars,
Good point!
[…] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
[…] CSS display:inline-block: Why It Rocks and Why It Sucks [Robert Nyman] […]
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??
davo2323,
You’re welcome!
Your solution might work, but there is also, as you mention, a possible risk cross-platform.
[…] Traduzido e adaptado daqui. […]
what about white-space:nowrap ? I’ve used this to get around the white-space issue with inline-block. Obviously then on one line…
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.
Beautiful tutorial! As a web developer quite embarrassingly I haven’t used inline-block but definitely plan to from here on out. Thank you.
Eric,
Thanks, glad you liked it!
[…] 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 […]
[…] 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. […]
[…] CSS display: inline-Block: Why It Rocks, And Why It Sucks […]
I didn’t know the “strong” tag could be used inside CSS 😉
rolfen,
Ha ha! Formatting error – it’s fixed now. 🙂
Lars Gunther wrote:
Why does that work? It seems like magic… 😛
Sparrow,
The magic of the web browser automatically self-closing the tags… 🙂
[…] FUENTE: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
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.
Ralph,
Glad it was of use to you!
A better solution for list items is to use:
nav li{float:left; width:auto;}
Has exactly the same visual effect without the headache.
Louisa,
That will not work with content over multiple rows, where the content isn’t of equal height.
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…
stephband,
It is, but it’s sad to have to use such an approach, I think.
hi,
very helpful U solve one of my biggest probs with lists…
tnx tnx tnx!
haha stephband ,you are genius!
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.
camslice,
Cool, thanks for sharing!
This article saved my day: CSS display: inline-Block: Why It Rocks, And Why It Sucks
As soon as browsers start to support display:box or display:grid, inline-block will be obsolete.
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
davidtg,
Glad it was of help to you!
Thank you so much for the tutorial. This saved me tonight!!!!
Fantastic, been struggling with centered lists for years, this solves all my issues! Ta.
Brandon, Laurence,
You’re welcome!
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?
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.
[…] Robert Nyman???? Basically, it’s a way to make elements inline, but preserving their block capabilities such as […]
[…] See also: http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
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! =)
missycola,
Good to hear! 🙂
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.
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.
[…] It may suit your needs better, but it’s difficult to say. display: inline-block comes with rendering drawbacks, as does float of […]
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.
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%;}
Nicolas,
Cool, thanks for the tip!
Hi,
to get rid of the white space issue
ul#display-inline-block-example li
{ ..
white-space: nowrap;
}
might work.
Hanno,
Sorry, but that only applies within element contents, not between elements.
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
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.
Very usefull when i put social icon on the right top of my site, thank you very much for this tutorial!
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.
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.
Patrick,
Yep, it could work in some cases, but it’s not really optimal.
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!
Adele,
Yes, there is a way at least. Good luck!
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/
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).
[…] 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. […]
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! 🙂
Hristiyan,
Good to hear – glad it was of help!
Why dont you just use display: table-cell;
Se3ker,
Because a table cell and inline block element don’t really have the same rendering characteristics.
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!
Vladimir,
Yes, it works. Although I’d argue that it’s not really logical with font-size for spacing. 🙂
[…] 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. […]
[…] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
[…] […]
[…] Read more about the CSS display inline-block value: Why it rocks, and why it sucks. […]
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.
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.
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.
Ram,
Yes, it could work for some scenarios.
[…] 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 […]
very nice trick. IE always has problems with CSS and with this inline-block, fix everything. thanks
Thanks!It helped me a lot!!!
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.
Sorry, I forgot the semicolons!
<div ….><div ….><div ….>
or
<li ….><li ….><li …>
instead of
<div ….>
<div ….>
<div ….>
or
<li ….>
<li ….>
<li …>
Manuel,
Yes, it solves the problem, but I hate that we have to take white-space into account.
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;)
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 … (?)
hans,
It can be a bit tricky, so you need to experiment a bit with it.
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.
Bill,
Interesting approach with the rem unit!
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.
Bill,
Yep, any combo/alternative approach is good to know about!
[…] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
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
[…] 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 […]
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..
jawahar,
Hopefully someone will see the link and can help you.
marian,
Glad you found a way that works for you!
[…] 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. […]
[…] 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 […]
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.
[…] Inline-Block is a potential alternative to floating elements but certainly isn’t without its issues in these circumstances. […]
Luis,
Thanks for the suggestions!
[…] Inline-Block is a potential alternative to floating elements but certainly isn’t without its issues in these circumstances. […]
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.
Mike,
Thanks! And good to hear it’s an option for you!
[…] 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 […]
[…] is very important to know the distinction between these terms, see this website for more […]
[…] is very important to know the distinction between these terms, see this website for more […]
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!
[…] 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 […]
[…] 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 […]
[…] 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 […]
inline-block works on IE6&7 but ONLY if you declare it on an element that is naturally inline such as span.
VBAssassin,
Thanks, good to know!
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
Ahh ! here we go again. . .
<table width="250"><tr>
<td>item1</td><td>item2</td><td>item2</td>
</tr></table>
thanks for your css article, I’m using display: inline-block; to expand the container div.
I notice that this property (display:inline-block) is needed if you want to stretch text with transform:scale.
Thanks friend…
i’ll try it little by little..:)
.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
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
Thank you for this. I was needing something to update on of our photos page. This helped alot!
[…] Menurut Robert Nyman: […]
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! 😀
[…] Read more about the CSS display inline-block value: Why it rocks, and why it sucks. […]
[…] 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. […]
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? 🙂
[…] 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.) […]
[…] 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 […]
Great post ..i make css look like this for my dynamic div for wrodpress post
.posts
{
width:300px;
background-color:#66FFFF;
margin-left:15px;
margin-right:15px;
display:inline-block;
}
for wordpress post i want to display post dynamic in two collumn.
it’s good ?
[…] Display inline-block, why it rocks and why it sucks by Robert Nyman […]
Thanks for this post. Sure did help me out. A LOT!
[…] Display inline-block, why it rocks and why it sucks ?? ?????????? Robert Nyman […]
[…] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
[…] Display inline-block, why it rocks and why it sucks by Robert Nyman […]
[…] http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/ […]
Thanks for the article. Very good idea. I rewrote it using and collapsed margins/borders.
I also wanted to find a way to right aligned. I used the float:right which worked, but was wondering if there is a way of doing this without the float.
http://jsfiddle.net/JLt7s/
wyatt,
Thanks for sharing. Don’t know off the top of my head how to get that behavior.
Thanks, inline bloc has confused me for a long time and, you know, on the net it is often hard to find information that directly answers the problem.
Your sharing was the right place at the right time, thank you
Hank West
[…] Display inline-block, why it rocks and why it sucks ?? ?????????? Robert Nyman […]
[…] CSS display: inline-block: why it rocks, and why it sucks […]
[…] aunque cada vez más se utiliza la propiedad display:inline-block para hacerlo. Aquí tenéis una defensa del vs inline-block seguida de una interesante […]
i love you!
Thankyou.
last two renedered results look like a …. 😀
The first section, “Problems With float”, could sure use a rendered example. It is too hard to understand what you are saying there w/o an example and an image. thanks!
Why doesn’t anyone set “word-spacing: 0px” on the parent element? Then use margin’s etc, as normal. If you need word spacing back to normal just reset it in a child element.. It’s what I do.
Great article it helped me a lot!!!