tags:

views:

78

answers:

1

Here's the dilemma.

<div id="lists-container>
<ul id="list-one"></ul>
<ul id="list-two"></ul>
</div>

I need content in list-one to expand 100% of parent div (list-container), if list-two has content, I want both lists to be 50% widths -floating side by side.

What happens is that list-two ends up clearing list-one, rather than floating to the right. Can anyone please help? This is what I have:

#list-container{width:600px}
ul#list-one{display:inline; float:left}
ul#list-two{display:inline; float:right; width:50%}
A: 

Use display:table. Also put in some hidden Internet Explorer markup. Some of it is run together because IE will add white space if you don't close the tags right after content.

<div id="lists-container" style="display:table; width:100%">
  <!--[if lt IE 8]>
  <table border=0 cellspacing=0 cellpadding=0>
    <tbody>
  <![endif]-->
  <div style="display:table-row">
    <!--[if lt IE 8]>
      <tr>
        <td width="50%">
    <![endif]-->
    <div style="display:table-cell; width:50%">
      <ul id="list-one"></ul>
    </div><!--[if lt IE 8]></td><td width="50%"><![endif]-->
    <div style="display:table-cell; width:50%">
      <ul id="list-two"></ul>
    </div><!--if lt IE 8]></td></tr><!endif]-->
  </div><!--if lt IE 8]></tbody></table><!endif]-->
</div>

This gives you nice, predictable, table-like behavior but still uses semantic markup. It works in IE 6+ and EOMB.

Note: I read about this somewhere online some time ago. I think it was a blog. If anybody can find and credit the source, I'd appreciate it. I can't find it!

If you don't want to use display:table, here is some code that will get you close. The applied width is 50% of the parent container + border + padding + margin, so this will behave differently at different widths, but this kind of works.

<style type="text/css">
  #list-container {
    width:600px
  }
  ul#list-one {
    border:1px solid red;
    display:inline;
    float:left;
    width:49%;
    margin:0;
    padding:0;
  }
  ul#list-two {
    width:49%;
    padding:0;
    margin:0;
    border:1px solid blue;
    display:inline;
    float:left;
  }
</style>

<div id="lists-container">
  <ul id="list-one">
    <li>item 1</li>
  </ul>
  <ul id="list-two">
    <li>item 2</li>
  </ul>
</div>
sidewaysmilk
I completely misread this question >_<. I'm leaving this up because it's still a neat trick.
sidewaysmilk
David
It's very tricky floating things and specifying their widths as percentages. The element's margin, padding and border are not considered when setting the object's width, so it's (50% of parent) + border + padding + margin. Use FireBug to peek at how the box model comes together.Anyway, to get these things to layout the way that you're describing, I would suggest using `display:table`. Then you can specify in percentages.
sidewaysmilk
Thanks sidewaysmilk. When I get a chance later I'll set up with your suggestion. As for the width in percentages, I shouldn't be so mis-leading. The width is less important. I can easily state a fixed width in pixels for list-two, but clearing still occurs.I'm just baffled as to why (when active) <ul id="list-two"> doesn't float inline -next to <ul id="list-one">.I thank you for your time, I'll let you know if your suggestion solves my problem.
David
Ah. Fixed widths makes it easier. The other solution that I posted (I updated my original answer) definitely floats them next to each other. If you want them touching, you can float both left. You can also change the second float to right.
sidewaysmilk