tags:

views:

112

answers:

3

I have a list of items that I want to be laid out horizontally. It's basically a row of pictures with captions. Something like

picture1 picture2 picture3  
caption1 caption2 caption3

So I figured that was a unordered list with List elements styled with "display: inline". But the captions have to be block elements otherwise they are printed in line like this:

picture1 caption1 picture2 caption2 ...

But when they're block elements the list stays vertical:

picture1  
caption1  
picture2  
caption2  
picture3  
caption3

Do I need to use floated divs instead of a list in this case? What's the best way to make this work?

Below is a complete example using divs instead of images.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title>Blah</title>
<style type="text/css">

.myclass li{
  list-style: none;
  display: inline;
}
.item{
  display: inline;
}

</style>
</head>
<body>

<div class="myclass">
  <ul>
    <li><div class="item"><a href="http://google.com"&gt;one&lt;/a&gt;&lt;p&gt;uno&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;
    <li><div class="item"><a href="http://google.com"&gt;two&lt;/a&gt;&lt;p&gt;dos&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;
    <li><div class="item"><a href="http://google.com"&gt;three&lt;/a&gt;&lt;p&gt;tres&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;
  </ul>
</div>

</body>  
</html>
A: 

Whenever you have a block-level element, all the layout shifts down, even if it's container is displayed inline.

Floating your LIs is the way to go; that way you can have any content inside them.

Seb
A: 

You don't need the divs. Just set .myclass li to display: inline-block.

Ben Blank
I tried it and it seems to work in firefox, but not IE7. Good info though. I wasn't aware of that option.
dl__
Then I would guess that either your page is putting IE7 into quirks mode or I've forgotten something significant. ;-)
Ben Blank
A: 

The divs in the li elements are redundent. Here is the simplest way to do it:

The HTML:

<ul id="captions">
   <li><a href="#"><img /></a><p>Caption here</p></li>
   <li><a href="#"><img /></a><p>Caption here</p></li>
   <li><a href="#"><img /></a><p>Caption here</p></li>
   <li><a href="#"><img /></a><p>Caption here</p></li>
</ul>

The CSS:

#captions {
   list-style:none;
   margin:0;
   padding:0;
}
#captions li {
   list-style:none;
   margin:0;
   padding:0;
   width:200px;
   height:220px;
   float:left;
   text-align:center;
}
Matthew James Taylor