shubendrak shubendrak - 1 month ago 7
CSS Question

Issues in creating css image gallery

Here is my JsFiddle

I am new to css. I am trying to create an image gallery. I designed the layout using

table
tr
td
tag.

Here is my HTML code:

<div class="gallery">
<table>
<tr>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
</tr>
<tr>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="normalimage" src=""> </td>
<td> <img class="wideimage" src=""> </td>
</tr>
</table>
</div>


Here is my css:

.gallery {
background-color: #abcdef;
width:355px;
height: 265px;
overflow-x: scroll;
}
.gallery img {
display: block;
margin-left: 20px;
margin-top: 20px;
}
.normalimage {
height: 80px;
width: 50px;
border: 5px solid black;
}
.wideimage {
height: 80px;
width: 130px;
border: 5px solid black;
}


Question: In the 2nd row I want my third image to appear just after second image with 20px distance.

Answer

You don't need a table here. Just wrap the images in one <div> per row like this:

<div class="gallery">
  <div class="row">
    <img class="normalimage" src="">
    <!-- more images -->
  </div>
  <div class="row">
    <img class="normalimage" src="">
    <!-- more images -->
   </div>
</div>

And adjust your CSS like this:

.gallery .row {
    white-space: nowrap;
}
.gallery img {
    display: inline-block;
    /* other properties */
}

Example Fiddle