tags:

views:

41

answers:

4

I have a situation similar to the following

<div class="col1">
  <h2>title</h2>
  <div><ul><li>Content</li><li>Another Item</li></ul></div>
</div>
<div class="col1">
  <h2>Another title</h2>
  <div><ul><li>More Content</li></ul></div>
</div>
<div class="col1">
  <h2>...</h2>
  <div><ul><li>More Content</li><li>Still more</li></ul></div>
</div>
....
<div class="col2">
  <h2>title</h2>
  <div><ul><li>Content</li></ul></div>
</div>
<div class="col2">
  <h2>Another title</h2>
  <div><ul><li>More Content</li><li>And More</li><li>...And More</li></ul></div>
</div>
<div class="col2">
  <h2>...</h2>
  <div><ul><li>More Content</li><li>Still more</li></ul></div>
</div>
....

I realize this would be much easier if I just had the div's of class="col1" all within one div and the div's of class="col2" all within another but is there any way I can get this displaying in 2 columns without doing that? Thanks in advance.

+1  A: 

You may try something like this:

<style type="text/css">
  .col1 {
    display:block;
    float: left;
    background: #ff0;
    clear: left;
    width: 200px;
  }
</style>

background is just set to see what is happening and width may be adjusted.

Joe
That will push the first "col2" div to the right of the last "col1" div but it doesn't push the right column to the top. Any suggestions?
Ben
hey Ben, in my environment (ff3) and the example code I used then col2s are aligned right of the col1s. Do you have any floating active on the col2? If yes just give it try and remove the floating ;)
Joe
I was able to get it (partially working) but now if the left column ends up being shorter than the right, the remaining items on the right get pushed back into the left column.
Ben
yep, but I'm afraid you won't be able to avoid this unless you don't wrap the col2s in a container. Alternativly you may try to float them right, as mentioned above, and align them together by putting a wrapper around all cols. I'll update my post with an example in a minute..
Joe
+1  A: 
<div class="col1 left" >
  <h2>title</h2>
  <div><ul><li>Content</li><li>Another Item</li></ul></div>
</div>

<div class="col2 right" >
  <h2>title</h2>
  <div><ul><li>Content</li><li>Another Item</li></ul></div>
</div>

<div class="clear"></div> 

where .left {float:left} .right{float:right}

so for each "row" y will have a left and a right.

Must clear floats for each "row" y begin

.clear {clear:both}

ntan
+1  A: 
.col1 {
clear:left;
float:left;
width:200px;
}

Seems to work, but obviously brings its own set of issues. This kind of question is a bit difficult to answer without working within the framework of the whole layout.

Damien_The_Unbeliever
+1  A: 

Paste this into your head tag.

p.s: the syntax highlighter hates me :) <style type="text/css" >

.col1, .col2 { display: inline-block; } .col1 { float: left; background: #ff0; clear: left; width: 200px; } .col2 { float: right; background: #f0f; clear: right; width: 200px; position: relative; top:-300px; right:0; } </style>

questzen
This seems to be working. Where does the top: -300px; come from?
Ben
Unfortunately -300px is the messier one, this needs to be derived using javascript, essentially it is the height offset from the top of the parent. Your top container needs to have postion: relative; (in this case body tag). I have manually fiddled to give you an idea.
questzen