tags:

views:

184

answers:

4

I'm creating a tiled grid of images much like an image gallery with a grid of thumbnails, and I need the images to wrap onto the next row after 3 images. So I'm floating a bunch of divs that each contain an image and then clearing the float manually after three images.

The problem is that I'm working within a rather convoluted existing template that already makes use of float to arrange everything. Clearing the float in my grid scrambles the entire page, presumably because it's clearing every float in page so far. What can I do?

I'm clearing the float using by inserting a blank div. ie:

<div style='clear:right'>

Might one of the other methods for clearing floats work better?

A: 

If your markup is like so:

div
 img
 img
 img
 row break
 img
 img
 img
 ...

Then you need to add this after every three blocks:

<br class="clear" />

But if your markup is like this:

div
 div
  img
  img
  img
 div
  img
  img
  img
 ...

..then you just need to apply the following .clear class to your inner DIVs.

Either way, add this to your stylesheet:

.clear:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
* html .clear { height: 1%; }
*:first-child+html .clear { min-height: 1px; }

You can use this class for all other elements that contain floats.

Nimbuz
That doesn't seem to help. My code is laid out the same way as your first example, except that there is then one big div around all that -- namely that belonging the main content div of the template that I'm using. The problem is that this div itself is being floated, and it seems like any kind of clearing I do clears the floats across the whole template, not just within the grid I'm making.
humble coffee
A: 

If IE >= 8 support is fine for you, you might want to consider using display: table instead of floats. Since you want to display a grid, this the more appropriate way of doing it.

http://www.quirksmode.org/css/display.html#table

Heinzi
A: 

I'd try to use display: inline-block; to style the elements containing each image. Example of HTML code for one container:

<style>
.figure {
    display: inline-block;
}
</style>

<div class="figure">
    <img src="littlepony.jpg" alt="My cute little pony" width="13" height="37" />
</div>

Now there a few pitfalls using this with IE6, IE7 and Firefox 2:

  • IE 6 and 7 will only style inline elements that have hasLayout triggered, I mean you'll see inline-block behavior if you do this:
<!--[if lte IE 7]>
.figure {
    display: inline;
    zoom: 1; /* triggering hasLayout */
}
<![endif]-->
  • Firefox 2 doesn't understand display: inline-block; so you'll have to precede the latter by another display instruction:
.figure {
    display: -moz-inline-stack;
    display: inline-block;
}
  • now Firefox 2 is going to annoy you a bit. First, you must have only one child element in your .figure element, otherwise the children would ... stack. So if you have a legend under your image, insert a div between div.figure and img+p

    <div>
        <img src="littlepony.jpg" alt="Photo of my cute little pony" width="13" height="37" />
        <p>Second child of .figure>div and not .figure</div>
    </div>
</div>

Second (still only in Fx2), you'll notice from time to time that you can't anymore neither select text inside the -moz-inline-stack'ed element nor click on links it could contain. The fix is to position the added div relatively:

    .figure div {
        position: relative;
    }

Of course the conditional comment for IE6/7 must occur after regular CSS, otherwise it'll be of little help.

And finally, if no elegant solution works for you, use a TABLE. A simple table with only td and no th. If it occurs that:

  • IE6 and 7 don't like display: table-sth
  • your CMS causes problems to what would otherwise work fine on another site
  • Firefox 3 support for inline-block is of no help

than yes it's better for everybody that you use a table and no half-solution causing problems to half your users ;)

Felipe Alsacreations
no offense, but for web development, if the answer starts with "Now there a few pitfalls using this with IE6, IE7 and Firefox 2", that solution should be scrapped at that point, especially when easier alternatives(such as simple floating) can be used. very technical answer, but just not practical
jaywon
+3  A: 
  1. Create a suitable container div (if you don't already have one)
  2. Set a restrictive width on the container div - equalling the same that 3 images takes up.
  3. Allow all images to float - they'll automatically wrap.
  4. Set the container div with 'overflow: hidden', which will clear the floats for you.

A simplified version might look like this:

<style>

div#container {  
  overflow: hidden; 
  width: 300px; 
}

div#container img { 
  float: left; 
  width: 100px; 
}

</style>

<div id="container"> 

  <img src="" />
  <img src="" />
  <img src="" />


  <img src="" />
  <img src="" />
  <img src="" />


  <img src="" />
  <img src="" />
  <img src="" />

</div>
codeinthehole
For some reason this works, but making a div and giving it identical style using style="..." did not work for me.
scott77777
If you chose to use inline styles, you'd also need to apply the relevant style individually to each img tag. Inline styles are rarely a good idea.
codeinthehole