views:

145

answers:

5

I'm trying to have two or more columns worth of DIVs for input elements in a form. It's a very complex form, and some elements will be hidden and shown depending on some answers.

The problem is I can't get the DIVs to space accordingly in IE6 while having an effective hide/show. This is what mostly works:

.first_column
{
  float:left;
  clear:both;
}
.second_column
{
  float:left;
}

And some HTML...

<div id="question1" class="first_column">
first row, column 1 <input type="text" id="asdf">
</div>

<br style="clear:both;" />

<div id="question2" class="first_column">
second row, column 1 <input type="text" id="asdf2">
</div>

<div id="question3" class="second_column">
second row, column 2 <input type="text" id="asdf3">
</div>

<br style="clear:both;" />

This works as expected. The problem is the show/hide. If I hide #question1, the line break remains. This isn't so bad for this small example, but if there are many questions depending on a show/hide, large gaps start to appear between rows of questions.

How can I achieve this without that line break?

+1  A: 

Use margin-bottom on your divs instead of br

Gregoire
Works in firefox but it doesn't look like that works in IE6. The 2nd column of questions will start in the first row, even if it's supposed to start in the 1st row.
macca1
Are these divs all nested in a parent div? Try float:left on the parent.
Ben
@Ben: Yes, they are all nested in a parent div but float:left doesn't seem to have an effect on IE6.
macca1
+1  A: 

I suggest wrapping your complete rows in another div, and give it an id like row_1, row_2. This would include all questions plus the br. Then when you hide the row, the br hides too.

Ray
Thanks for the suggestion. It's also worth noting that this is a small widget in a large application. There are already a significant number of DOM elements and the jquery document.ready performance is already pretty poor because of this. I'm trying to condense the number of elements used in this question widget.
macca1
It sux to have to add an extra dom element, but I don't see any way around it, unless you can get the idea from Diodeus to work. You could use a table, which would allow you to lose the br tags, but it is probably not enough of a benefit to make it worth trying.
Ray
A: 

Use this pattern. Better semantics, simple code. Wrap these in DIVS for hide/show.

this.next('.question').toggle() - uses Prototype library. It finds the next element with the given class name and will hide/show that element.

<a href="javascript:;//open question" onclick="this.next('.question').toggle()">open</a>
<div class='question'>
<label>First Name</label>
<input .... />
<div class="formClear"></div>
</div>

...and this CSS

.label {
width:120px;
float:left;
margin-right:15px
}

.input {
float:left
}

.formClear {
clear:left;
height:15px;
}
Diodeus
But then you lose the ability to easily show/hide a question by referencing it's parent DIV. In this case you would need to keep track of and hide/show a label, a tag, and a div instead of just a div. correct?
macca1
Wrap it in a DIV with a class of something like 'question'. If you use a framework like jQuery (I use Prototype) you can make show/hide easy:<a href="javascript:;// show question" onclick="this.next('.question').toggle()">open question</a>
Diodeus
A: 

For this problem, in my websites I use this:

In the CSS:

.spacer
{clear: both; visibility: hidden;}

In the HTML:

<div class="spacer"></div>

DIV is betther than BR (or HR) for this type of things because it do not move a pixel so you are free to apply margins to other content DIVs in order to have the perfect layout that you want. This work in all browsers, also in IE6.

This will fix also most of the floating problems. I do not find other solutions without this that are cross browser.

Davmuz
A: 

Unfortunately IE6 is unwavering in it's resolve to render HTML differently from every other browser. Unfortunately I wasn't able to come up with a simple enough solution with using the br's with clear. Oh well... time to fight with IE6 elsewhere.

Thanks for the suggestions.

macca1