« Previous

Tableless Image Gallery - An Experiment of Fluid Grid

Posted on October 2, 2007
Filed Under CSS, HTML | 5 Comments
Tags: , ,

Today I want to build a tableless image gallery. The motivation came from a web project I am currently doing. In one of the web pages I am coding, I need to make a grid layout and put image thumbnails in it. I started off by using table to layout the grid. Although it turned out nicely, the gallery itself is not very friendly to DOM manipulation. For instance, when removing a table cell during a delete operation using Ajax, a hole remains in the gallery.

I told myself that there must be a better way to do this. So I started experimenting tableless grid layout. It took me a while, but I am quite satisfied by the result. You can take a look how the finish product looks like or download sample layout files. For illustration of deletion using Ajax, I used jQuery to perform a simple DOM removal operation, which removes a clicked cell off the grid. While you are testing the online example, look closely how the gap is being filled when a cell is removed. I've tested it in IE 6 and Firefox 2.0.0.7, and both worked like a charm.

The Grid Layout Markup

Below is a basic skeleton of the grid in HTML. The outer DIV functions as a container, which contains two cells (a cell is denoted by CSS class imageSingle):

<div class="album">
    <div class="imageSingle">
        <div class="image"><img src="1-th.jpg"/></div>
        <div class="footer">Caption</div>
    </div>
    <div class="imageSingle">
        <div class="image"><img src="1-th.jpg"/></div>
        <div class="footer">Caption</div>
    </div>
</div>

I added two DIVs to each image cells. The first DIV is used to hold image thumbnail, and the other is used to hold image caption.

The Style Sheet

We now have the structure for our layout, it is time to make it looks like the way we want it. I created the following style sheet, the same style sheet I used for the online demonstration of this article:

.album{
  width:100%;
  background-color:#000000;
  padding: 10px;
  float:left
}
.album .imageSingle {
  float: left;
  margin: 5px;
  width:156px;
  text-align:center;
  border:solid 1px #333
}
.album .imageSingle .image {
  width:150px;
  height:150px;
  margin:3px;
}
.album .footer {
  text-align:center;
  width: 156px;
  border-top:solid 1px #333;
  color:#999;
  font-weight:bold;
  padding-top: 7px;
  padding-bottom:7px;
  margin-top: 10px;
}

Among styles above, the most essential one is float:left in albumSingle. By setting floating to left, album cells will magically align side-by-side with its sibling. The beauty of floating layout is that it can well adapt to variety of width without creating a horizontal scrollbar.

You can easily create your own theme by changing around the background or border color, font style, width and height of the image cell.

One last thing: don't forget to clear the float since the whole grid layout is floating to the left. You can clear the float by adding this line to the end of the grid layout:

<div style="clear:both"></div>

Related Reading

Download Sample Files

Tableless Image Grid Files

Go to Top

More interesting posts ...

  • No Related Post
Comments
5 Comments so far
  1. roman December 28, 2007 2:27 am

    i'll try it

  2. Scott Harvey February 23, 2008 3:06 pm

    The one major limitation with this type of grid is if you have a caption that takes a couple of lines or a tall image the layout will break.

    This has been the main reason why I have stuck with using table for this type of thing, do you have a solution for this?

  3. Deborah Whipp March 22, 2008 9:30 pm

    I love this and am using it on a site I'm working on. However, one of my images has a 3-line caption while the rest have 2 and this does result in a gap.

    Here's the css I'm using:

    .album{ width:100%; background-color:#ffffff; padding:0px; float:left }
    .album .imageSingle { float:left; margin:5px; width:135px; text-align:left; }
    .album .imageSingle .image { width:88px; height:113px; margin:1px; text-align:left; }
    .album .footer { font-family:verdana,arial,sans-serif; font-size:11px; text-align:left; width:120px; color:#3d3e5d; font-weight:normal; padding-top:0px; padding-bottom:5px; margin-top:0px; }

    Have you had any luck with this issue?

  4. Sirus Coder May 29, 2008 8:34 am

    This looks great and will work well for my website, so thank you for writing it and making it look good.

    To Scott and Deborah, I think the idea here is that all the images will be of the same size (created as thumbnails) and all the captions will be constrained to a set number of lines. I am going to stick to a single line of caption and if it appears to extend over the single line, I plan on truncating the text. For example:
    Caption: The quick brown fox jumps over the lazy dog
    I would display this as: The quick brown fox j...

    The thinking is that these boxes would serve as quick identifiers of what you want to click on and not entire packages of information:)

    Just my thoughts. Again good work!

    Sirus

  5. Sanj June 16, 2008 5:34 am

    Hi interesting stuff! I was wondering, can you not create a grid without borders, so that the image edges actually touch each other. (For example, like the 'Friends Grid' in Facebook). Have you got a code for that?

    All the best.
    Sanj

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

Attention
If you want to post source code, please wrap it with <pre> and </pre>

Categories

Polls

  • Which browser are you using to browser this site?

    View Results

    Loading ... Loading ...