One of the css properties that I’ve been using recently is display: inline-block for elements that are going to be sitting next to one another i.e. thumbnail images or anything similiar. Traditionally floats would achieve the same effect however, it wouldn’t be suitable to use it on elements which may have variable heights. So taking the example of thumbnails images which were 60×60 and one thumbnail which was in the middle and is 60×200, then any smaller images following the large image would start to stack up on one another. This is actually the expected behaviour of floats and is described in the w3c spec.
Any floated box becomes a block box that is shifted to the left or right until its outer edge touches the containing block edge or the outer edge of another float. The top of the floated box is aligned with the top of the current line box (or bottom of the preceding block box if no line box exists). If there isn’t enough horizontal room on the current line for the float, it is shifted downward, line by line, until a line has room for it.
By using display: inline-block you prevent the above behaviour from occurring entirely and force the next element not to position itself where room is available. There’s an article on sitepoint which describes this in much greater depth and illustrates the behaviour visually.
Usage
To use this property lets say we have the following markup:
<ul>
<li><img src="small.gif" alt="small" /></li>
<li><img src="large.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
<li><img src="small.gif" alt="small" /></li>
</ul>
with the following css:
* {margin:0; padding:0;}
ul {width: 500px;}
ul li {list-style: none; display: inline-block; vertical-align: top;}
The results would be a page where no elements are stacking and document layout is maintained. With that being said there is a caveat, display: inline-block doesn’t work in Firefox 2 but fortunately there is a workaround to get Firefox 2 to render correctly, though it ain’t pretty. I also recommend readers visit ppks’ site which illlustrates what each display value is, its browser support and illustration of the rendering.
I have set up a demo page which compares floats vs inline-block, without the Firefox 2 workaround, and I certainly hope that designers and developers start to use this more often. I welcome any comments, criticism or suggestions on improving the code.
This entry was posted on Saturday, December 27th, 2008 at 9:27 am and is filed under article, development, web. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Neat trick!
I noticed something similar on our templates but it used classes a bit more…and I sort of prefer that (even though it isn’t as semantic).
Reason being that whilst we can use conditional comments to include / exclude HTML or CSS hacks for IE there’s no way to target a specific Firefox, and what’s the score with Firefox 3 and the -moz prefix? I’d expect it to work in the same way but is it a hack for a non-problem?
We can always do that server side and to be honest I don’t know why it was a problem for that site. I will ask why it was not allowed, presumably for performances reasons but I can’t work out what that would be. Will have to ask.
Will definitely consider it for future sites though. It’s a solution for a common problem and CSS problems should be solved with CSS not by chucking more classes at an element
I for one wasn’t aware of this until Firefox 3 was released and I honestly didn’t bother that much keeping up with specs. Since then I’ve been keeping a close eye on the css3 spec and testing any new css3 features on the nightly releases for firefox and safari.
I’ve used
display: inline-blockon personal projects for form labels, buttons and just this morning applied the style to fix a bug. Without thedisplay: inline-blockI would’ve had to write 2-3 lines of css code and we all know less is better!Firefox 2 was a great browser in it’s time but people have to start upgrading if they want a better web experience but the problem is that they don’t know they can have that unless someone tells them. For that I reason I strongly believe that we, designers and developers’ have to take the initiative and have some sort of display message on page load telling users to upgrade – much like the one running on this site.