tags:

views:

115

answers:

4

I'm trying to display a grid of items, with each item having a photo on the left and a description on the right, something like this:

----------------------------
| photo | item description |
----------------------------

I want to display these items in a 3x3 grid on the page. I have the grid part worked out, what I'm having trouble with is alignment of the photo and description. When the height of the description exceeds the height of the photo, I don't want the text to wrap under the photo. I essentially want to maintain two separate columns.

I have tried this:

.item{
  padding-left: 60px; // size of photo + 5px margin
  background-position: 5px 0px;
}

<div class="item" style="background-image: url('/img/photo123.jpg');">
  Here is the item description
</div>

That has worked very well. the markup is clean and I don't have to mess around with absolute/relative, however, now I can't add a border to the image. Can anyone suggest a workaround or alternative?

+4  A: 

IMHO that is not clean. Those are obviously content relevant images, so they shouldn't be background images.

It usually very simple with floating, but there are several other ways.

CSS:

.item img {
  float: left;
}

.item p {
  margin-left: 60px; // size of photo + 5px margin
}

HTML:

<div class="item">
    <img src='/img/photo123.jpg'> <!-- Add width/height and alt text -->
    <p>Here is the item description</p>
    <div style="clear:left"></div>
    <!-- or any other clearing solution, for example, "clearfix" -->
</div>
RoToRa
@Eric: Hmm, thanks, I guess...
RoToRa
thanks, this works perfectly.
Evan
reisio
A: 

Have a look here. You just need to apply min-height on your div.

Eric
How should he know the height of the text?
RoToRa
No, the height of the image
Eric
But the problem case is when the text is longer than the image.
RoToRa
Is there? http://www.jsfiddle.net/Ut6Qg/2/
Eric
You are right. I somehow was confused :-)
RoToRa
A: 

Why don't you want to use a table? This seems to me to be tabular data (admittedly with the image being a data element), so wouldn't a table would be the obvious choice?

ZombieSheep
I'll be displaying 9 items per page in a 3x3 grid, I don't want the markup of 9 mini tables or even one giant table with 3 items / 6 columns per row
Evan
A: 

you could use list-elements like so

<ul>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
    <li>Description</li>
    <li class="image">Image</li>
</ul>

and the CSS

ul {width: 960px; font-family: Arial; font-size: 12px; float: left;}
ul li {width: 80px; height: 180px; padding: 10px; background: #444444; list-style: none; float: left; color: #cccccc; }
ul li.image {width: 180px; height: 180px; padding: 10px; background: #cccccc; margin: 0 20px 20px 0; color: #444444; }
pixeltocode
The image and the description are two parts of the same item, not separate.
nikc