views:

289

answers:

4

I want to display some floating boxes (divs containing thumbnails) and the number of thumbnails depends on the current page width. For example:

<div class="container">
  <div class="box1" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
  <div class="box2" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
  <div class="box3" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
  <div class="box4" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
    .......... ETC
</div>

the problem is that for a given width it shows for example 4 boxes on each row, but they are all left aligned and there is some white space to the right, how can i center horizontally for each row??

Something like this: http://realworldstyle.com/thumbs%5F3.html but with boxes centered horizontally on the page...

thanks in advance,

+2  A: 

I think the only way to get the elements centered is to work not with float, but setting the images display: inline. That way, you can align them at will using the parent container's text-align property.

But that will give you additional issues with vertical margins and setting height. But as far as I know, it's the only reliable cross-browser way as long as inline-block is not widely supported.

Pekka
I think the question makes clear why your fix would not work.
Doug Neiner
Point taken, read too quickly. Will edit.
Pekka
I've edited my question adding an example for better understanding...
fidoboy
@Pekka, SO doesn't like me :) It won't let me take back my down vote. Is it possible you can perform one more edit ?
Doug Neiner
Done! :) ---------
Pekka
+1 For great answer, didn't think about that. The last row of thumbs would not stay in a grid pattern.. I wonder if that matters.
Doug Neiner
A: 

Sorry, but will not be able to do what you are wanting with straight CSS + HTML. (Take a look at @Pekka for an alternative, though if the row of thumbnails don't fill the whole row, they would be centered by themselves on the last row)

You would need to have a fixed width on the parent object .container with margin-left: auto and margin-right:auto which you cannot do since it is a fluid width page.

Here is how I would go about doing it, though it is sure to have some bugs you will need to work around:

  1. Bind a javascript event to the window.resize event
  2. Get the new document width and see how many thumbnails would fit on one row
  3. Set the width of the div.container to be that width plus the little bit of margin on the right. This div would always have a margin-left and margin-right of auto

This will center the thumbnails as best as possible. Depending on your visual display, you may need an additional wrapping div to provide the 100% width background.

Doug Neiner
Ok, think that this could be the simplest solution, i though that i could solve this only using CSS but it seems that i'll need to use javascript also...
fidoboy
Can the downvoter take the time to provide helpful feedback so I can make my answer better?
Doug Neiner
Fantastic! I've solved it with some help from jQuery and now it works... Many thanks dcneiner!!
fidoboy
No downvote from me, but just the same I would suggest css for the browsers that support it and use the javascript to just wrap the container in a table for IE. That way you don't have to bother with the resize stuff.
jeroen
@jeroen My only problem with the other solutions, is they would break the grid pattern. By centering everything `text-align: center` the final row would not line up in a grid with the previous rows. Since the OP mentioned "thumbnails" I assumed it was something to which a consistent grid pattern is important.
Doug Neiner
@dcneiner: That´s true if you apply the inline-block stuff to the boxes, not if you apply it to .container.
jeroen
Ah, of course! Didn't think about that. Good point.
Doug Neiner
Why is this downvoted?
Pekka
I hate downvotes without an explanation...
jeroen
@Pekka @jeroen thanks for the support guys. I am happy to learn if people take the time to say why they downvoted. Oh well.
Doug Neiner
A: 

Use an unordered list with inline list elements:

HTML:

<ul>
    <li><img src="image1.jpg" /></li>
    <li><img src="image2.jpg" /></li>
    <li><img src="image3.jpg" /></li>
    <li><img src="image4.jpg" /></li>
</ul>

CSS:

ul {
    width: 960px;
    text-align: center;
}

ul li {
    display: inline;
}

That'll do as long as you don't have block-level elements inside the LI elements. Works also if you have more than one row of images ;) You can also use div's but using a list is semantically much more nicer.

Tatu Ulmanen
This doesn't work, because you are using a fixed with (960px) in your example....
fidoboy
@fidoboy The width isn't needed for his example to work, but if there were 6 items per row, and 3 on the last. The last 3 would be centered and not in line with the previous rows.
Doug Neiner
+1  A: 

inline-block and auto margins on .container should do the trick for most browsers with perhaps text-align:center on body as well.

And if IE6 and IE7 don't play nice, you can always use javascript / jquery to wrap the whole thing in a table just for them.

Mind you, I wouldn't dare suggest a table solution for normal browsers, although it obviously works flawlessly ;-)

jeroen