tags:

views:

2279

answers:

6

I am looking for a way to create a <ul> of items that I can put within a <div> and have them show up side-by-side and wrap to the next line as the browser window is resized.

For example, if we have 10 items in the list that currently show 5 items on the first row and 5 items on the second row, as the user makes the browser window wider, it turns into 6 items on the first row and 4 items on the second row, etc.

I am looking for similar functionality to what Windows Explorer does when in tiles/icons/thumbnails view. I am able to create the <li>'s I want as far as the size, color, content, etc. I am just having trouble with the wrapping/clearing/etc. part.

+5  A: 

give the LI float: left (or right)

They will all be in the same line until there will be no more room in the container (your case, a ul). (forgot): If you have a block element after the floating elements, he will also stick to them, unless you give him a clear:both, OR put an empty div before it with clear:both

Itay Moav
That seems to work except the next element on the page (an unrelated DIV after the LI) seems to also on the same line, which I don't want.
Brian David Berman
@Brian: give the div the css rule "clear: both;"
Chad Birch
@Chad: So the only way to accomplish this is to assign a clear:both to the next element in the page? I want to make sure there isn't something I can do to the UL, or the DIV that contains the UL before I start changing unrelated elements on the page.
Brian David Berman
If the div below can't be targeted for some reason as it or another with the same class needs to wrap around something else, you can add css to the ul to make the the ul clear itself. width:auto;overflow:hidden;
Steve Perks
+2  A: 

Here's a good resource for wrapping columned lists. http://www.communitymx.com/content/article.cfm?cid=27f87

ttony21
That actually is a pretty good article.
Walt Gordon Jones
+1  A: 

This can be a pure CSS solution. Given:

<ul class="tileMe">
    <li>item 1<li>
    <li>item 2<li>
    <li>item 3<li>
</ul>

The CSS would be:

.tileMe li {
    display: inline;
    float: left;
}

Now, since you've changed the display mode from 'block' (implied) to 'inline', any padding, margin, width, or height styles you applied to li elements will not work. You need to nest a block-level element inside the li:

<li><a class="tile" href="home">item 1</a></li>

and add the following CSS:

.tile a {
    display: block;
    padding: 10px;
    border: 1px solid red;
    margin-right: 5px;
}

The key concept behind this solution is that you are changing the display style of the li to 'inline', and nesting a block-level element inside to achieve the consistent tiling effect.

johnvey
A: 

Use the following code to float the items and make sure they are displayed inline.

ul.myclass li{float:left;display:inline}

Also make sure that the container you put them in is floated with width 100% or some other technique to ensure the floated items are contained within it and following items below it.

mattphp
A: 

Thanks! This helped a lot!

A: 

Thanks, this helped me as well!

trazek