Forum Moderators: not2easy

Message Too Old, No Replies

CSS display: inline-block; is usable now

and some overdue thanks..

         

SuzyUK

11:57 am on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



just an update to an older post [webmasterworld.com], and some overdue thanks to a reader for letting me know about it :)

That code is probably one of the better CSS solutions for a gallery style layout which has multiple boxes/images of different heights and widths which need to be displayed in a fluid width layout and still wrap nicely (floats don't cut it unless all the same height!)

however at the end of that code we had decided that FF couldn't cope without a nested div with a width that matched the parent, as it would align elements inside these "boxes" horizontally instead of vertically which ruled out using the unknown width" scenario.

Thanks, to a kindly reader letting me know about another Mozilla Property : -moz-box-orient [developer.mozilla.org] it is now perfectly possible to use this code for unknown widths too.

With Opera, IE and Safari already supporting inline-block; (albeit the IE trickery is still required for <=IE7) and FF3 now offering support too the code should work fine going forward too :)

so here's the updated code, width is now optional, if you want to see what difference that

-moz-box-orient: vertical;
is making, comment out the rule and take a look in FF<3, you'll see that in the last box the text overruns and the list is aligned horizontally with the text.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Centered Inline-Blocks - Gallery Style Listing</title>
<style type="text/css" media="screen">html, body {padding: 0; margin: 0; border: 0; background: #00f;}
#container {
background: #fff;
margin: 0 150px;
border: 1px solid #f00;
padding: 5px;
text-align: center; /* IE 5.x center */
}

.box {
background: #eee;
margin: 5px;
border: 1px solid #000;

display: -moz-inline-box; /* For FF<3 */
-moz-box-orient: vertical; /* For FF<3 */
display: inline-block; /* IE <8 needs this tripped back to display: inline; to make it work on block elements - see conditional below */
width: 150px; /* now optional */
vertical-align: middle; /* explicitly declare your preference as Moz inline-box auto aligns to top */

text-align: left; /* to left align text within divs when container has been set to center */
}

.box p {margin: 0.5em;}
</style>

<!--[if lt IE 8]>
<style type="text/css" media="screen">
.box {display: inline;} /* this is to make inline-block work on block elements versions <IE8 */
</style>
<![endif]-->

</head>
<body>
<div id="container">
<div class="box"><p>test<br>test.</p></div>
<div class="box"><p>test</p></div>
<div class="box"><p>test</p></div>
<div class="box"><p>test.<br />test.<br>test.<br>test.<br>test.</p></div>
<div class="box"><p>test</p></div>
<div class="box"><p>test</p></div>
<div class="box"><p>test</p></div>
<div class="box"><p>test</p></div>
<div class="box">
<p>You no longer have to have the content in these boxes nested<br>inside a nested block elements with a width = to box width for FF.</p>
<ul>
<li>test list alignment</li>
<li>list alignment</li>
<li>list alignment</li>
</ul>

</div>
</div>
</body>
</html>

makes sense really, vertical/horizontal, but I hadn't known about that particular rule before kindly reader let me know.. so enjoy!

Setek

3:01 am on May 15, 2008 (gmt 0)

10+ Year Member



That's... really awesome.

Are there any known bugs/problems?

And how many browsers back does it work for? Like, Safari 2 as well or just Safari 3? Firefox 1.5 too or 2 onwards?

Xapti

4:29 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Firefox 1.5 does support it

SuzyUK

10:21 am on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



setek afaik, there are no known bugs/problems except for that FF<3 quirk. but I haven't stress tested yet - so although it's acting like an inline-box with those 2 x properties added it may well not match specs like other browsers do.

I did try a sample drop-menu with positioned drops and I couldn't quite get the drops to position correctly in FF2, but for galleries or "centered floats" it certainly seems to work fine!

I set up a complex test page a while back where the boxes contained images, or lists of links or plain text, or a combination of all of the above ;) support was perfectly fine even back then (2005) - which would've been around FF1.0, Xapti? - and with the change it still tests fine in FF2 for me now. FF3 also works with the second (correct) display property and I should've said that allowing for it that the order of those display rules is important. For FF3 the cascade picks up the second display rule after the inital -moz one for compliance in FF!

A small glitch I found (and have reported) is that Opera 9.5beta suddenly can't cope with link hover effects if they're inside inline boxes.. O9 was OK, so I presume this bug will not get through 9.5B

Safari 3.1/Win is OK and I'm sure it was prior to that too but will welcome any checks, I'm sure they also were amongst the first to support inline-block which probably means Konqueror is OK.. but again input welcome.