tags:

views:

482

answers:

5

If anyone can give any insight here it would be very much appreciated. I want to create output that shows reservations in fitness classes on various dates. The idea is to have three across and however many rows are needed to cover all of the dates. To do this, I'd like to use CSS rather than resorting to tables.

Ok, so I have the following CSS definitions:

.ListSetContainer 
{ 
  background-color:Red;
  border:1px solid #999999;
  text-align:center;
}

.ListSet
{
  margin: 2px 2px 2px 16px;
  float: left;
  clear:none;
}

.ListSet ul 
{
  background-color: #EEEEEE;
  border: 1px solid #999;
  margin: 2px 2px 16px 2px;
  padding: 6px 6px 0px 6px;
}

And the following HTML:

  <div style="clear:both;">
    <h4>Class Dates, Counts and Participants</h4>
    <div class='ListSetContainer'>
      <div class='ListSet'>
        <ul>...{Class information here...}
        </ul>
      </div>
      <div class='ListSet'>
        <ul>...{Class information here...}
        </ul>
      </div>
      <div class='ListSet'>
        <ul>...{Class information here...}
        </ul>
      </div>
    </div>  -- End of First ListSetContainer

    <div class='ListSetContainer'>
      <div class='ListSet'>
        <ul>...{Class information here...}
        </ul>
      </div>
      <div class='ListSet'>
        <ul>...{Class information here...}
        </ul>
      </div>
    </div>  -- End of Second ListSetContainer
  </div>  -- End of surrounding Div

But rather than getting three divs arranged horizontally, followed by a second row with two divs, I'm getting this:

Linked Image

A few notes. First, notice that the hierarchy in the HTML (ListSetContainer divs contain the ListSet divs) is not reflected in the output. Second, the ListSetContainer div is only one pixel high - there is no background shown! (I'm using a Red background just to be sure I don't miss it). The whole enclosing div is just squashed down and the inner divs are floating out on their own. Setting the height manually doesn't work because the inner div with the list is variable height (and it looks odd, to boot). If I remove the float:left; from the inner divs, they expand to be the full width of the screen and thus I cannot get three in a row. So...I'm at a loss.

Again, thanks for any help you can offer!

+4  A: 

Your ListSetContainer needs to contain some non-floated content for it to hava a height/width. Floating an element takes it out of the normal layout hierarchy and thus the container, containing only the ListSets, doesn't contain anything that gets rendered normally. Add a non-breaking space to the container, then you should be able to set its width/height as there will be something in it to render.

I would also have your ListSetContainer use clear: both. This should make it start a new "row" of ListSets.

tvanfosson
+1 - Thanks for the insight. Man, you are everywhere all the time!
Mark Brittingham
I do a lot of compiling: http://xkcd.com/303/ :-)
tvanfosson
I ended up using a Width of 100% as per Richard Grundy's advice and that took care of having three ListSets per row. Oddly enough, it also took care of the need for non-floating content. I'm not sure why the height just came along correctly but it did. CSS sometimes seems like a crap shoot...
Mark Brittingham
Once you stopped floating the inner content that put it back in the normal rendering flow. Since the outer div now has content in the normal flow, it will get height and width. Glad you got things to work out.
tvanfosson
+1  A: 

Use clear:both for the .ListSetContainer block.

But you still can improve this using just one list:

<style type="text/css">
    .ListSetContainer {
     list-style: none;
     padding: 0;
    }
    .ListSetContainer li {
     width: 33.333%;
     float: left;
    }
    .ListSetContainer li li {
     width: auto;
     float: none;
    }
</style>
<ul class="ListSetContainer">
    <li><ul><li>...{Class information here...}</li></ul></li>
    <li><ul><li>...{Class information here...}</li></ul></li>
    <li><ul><li>...{Class information here...}</li></ul></li>
    <li><ul><li>...{Class information here...}</li></ul></li>
    <li><ul><li>...{Class information here...}</li></ul></li>
</ul>
Gumbo
+1 - Thanks for the alternate approach. Your CSS is definitely cleaner than mine so I'll give it a go.
Mark Brittingham
Plus you can adjust it to how many elements you want: just alter the width.
Gumbo
But to account for rounding problems and general weirdness in IE6, I suggest changing the width to 30% or so and / or setting margin and padding to 0.
Traingamer
+1  A: 

The problem lies in the fact that your ListSet class needs "display: block;" adding or "float:left;" removing and if adding the block then add "display: block; width:100%" to the ListSetContainer class.

Richard
+1 - Thank you for offering the display:block approach. I'll take some time to play with that.
Mark Brittingham
It turns out that the key was setting the ListSetContainer to use a width of 100%. For whatever reason, using display:block; didn't help at all if float:left; was still used. If float:left; wasn't used, I didn't need display:block; either but, without the width:100% things were stacked vertically.
Mark Brittingham
+1  A: 

It's because the ListSetContainer doesn't have a set width. i expect that they'd all fit on one row on a widescreen monitor.

You could try:

.ListSetContainer {
    width: 300px;
}

.ListSet {
    clear: none;
    float: left;
    width: 100px;
}

And style as appropriate.

Ross
Turns out that the width item on ListSetContainer was key although I took Gumbo's advice and used percentages on both the ListSetContainer and ListSet.
Mark Brittingham
+1  A: 

You are presenting tabular data with a fixed set of columns--one per month. Sounds like what tables were supposed to be used for. Now is your chance to use tables and not feel guilty about doing so!

Now, if you aren't going to use tables, you should be fixing the size of each element, but not the number of elements per row. Let the number of columns be determined by the size of the browser window.

Failing all that, I think you need a clear: both between each row.

Cory R. King
This is one of those "on principle" things. I'm generating this from code and, while I knew I could do it in a table, I was just getting stubborn about understanding why the CSS/Div approach wasn't working. Anyway, +1 for your help!
Mark Brittingham
heh--when you think about it, if you were going to be W3C Peanut Gallery Approved(tm) [grin], they'd tell you to use a table in this case.
Cory R. King
Clear:both; helped (things were laid out correctly) but the enclosing ListSetContainer still wasn't the correct height (as tvanfossan predicted). However, setting Width:100% took care of everything.
Mark Brittingham