tags:

views:

1995

answers:

3

I have a bunch of float: left elements and some are SLIGHTLY bigger than others. I want the newline to break and have the images float all the way to the left instead of getting stuck on a bigger element.

Here is the page I'm talking about : http://www.stdicon.com/gartoon/

If they are all the same size if works beautifully : http://www.stdicon.com/crystal/

Thanks! (I'd rather not get into javascript or server side scripting if I don't have to)

+1  A: 

You can wrap them in a div and give the div a set width (the width of the widest image + margin maybe?) and then float the divs. Then, set the images to the center of their containing divs. Your margins between images won't be consistent for the differently sized images but it'll lay out much more nicely on the page.

Evernoob
How will forcing a width stop them from sticking, isn't it a height problem? And how can I calculate the height of the images without coding?
Paul Tarjan
you can also apply a height to the containing div. Just get the height in pixels of the longest image and make that the height. You can get the height in pixels by opening your image in photoshop or fireworks or whatever image utility you prefer. In the link you provided, the longest container seems to be 140px; anything smaller than that is sticking.
Evernoob
hmm, all of my images are dynamically generated, so the heights will vary. I'm guess I'm down to doing javascript...
Paul Tarjan
no, not the images, the height of the div that contains them. Put this height in your CSS.
Evernoob
right, but how do I calculate that height? I have many "sets" that all have different images with different heights : http://www.stdicon.com/
Paul Tarjan
you don't need to calculate the height. Wrap each image in a div and set the height of that div to 140px; - if 140px still causes images to get stuck then try increasing it a bit.
Evernoob
A: 

Add to .icons div {width:160px; height:130px;} will work out very nicely

Hope it will help

Dom
Sadly all of my pages have different sized images, like that will make http://www.stdicon.com/tango/ look quite ugly :(
Paul Tarjan
A: 

Well, you've got two options:

  1. Use clear: left on the leftmost items - the con is that you'll have a fixed number of columns
  2. Make the items equal in height - either by script or by hard-coding the height in the CSS

Both are limiting - so it's your call.

Alexander Gyoshev