Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complicated form table vs div

I looked online for examples of implementation of the form using DIVs and all I see is pretty simple one column forms. I have some pretty complicated forms, here is a picture of one:

http://img835.imageshack.us/img835/8292/formn.jpg

It is easy to make it work with table (which I do), the only problem I have is that sometimes I need to not display some of the choices and move the values up one row to avoid gaps.

I started making some forms using divs, but they fall apart when I change the browser window size and it is not easy to align things.

This topic is helpful: Is it bad design to use table tags when displaying forms in html? but it doesn't address some of the concerns I have.

What would you propose as a solution? I can dynamically delete/insert values in the table or try to do the DIVs.

like image 553
Roman Goyenko Avatar asked Aug 24 '10 22:08

Roman Goyenko


1 Answers

I would go the div route. As long as you're careful of your widths when you apply floats, it's actually pretty straightforward to get the layouts to work properly in different screen resolutions.

Here are a few rules:

  1. Set a max width on your form or your form's wrapper element. If you want to float elements on one row make sure their width's added together does not exceed this width.
  2. If you are adding horizontal padding/margins to your floated elements remember that these add to the total width of the element.
  3. Avoid mixing percentage widths with pixel padding and margins. Apply the percentage width to a parent element and the pixel padding/margins to a child element.
  4. Use clearing elements between your rows of elements to keep everything in line.

As to the markup, you can use the form elements along with CSS to create a semantic structure:

<fieldset>
    <legend>Fieldset Title</legend>

    <label for="input1">Input 1:</label>
    <span><input type="text" id="input1" name="input1"/></span>
    <label for="input2">Input 2:</label>
    <span><input type="text" id="input2" name="input2"/></span>

    <br/>

    <label for="input3">Input 3:</label>
    <span><input type="text" id="input3" name="input3"/></span>
    <label for="input4">Input 4:</label>
    <span><input type="text" id="input4" name="input4"/></span>
</fieldset>

And the CSS:

fieldset {
    padding: 20px 0;
    width: 600px;
    margin: 0;
    border: 0;
}

legend {
    display: block;
    width: 100%;
    background: black;
    color: white;
}

label, span{
    float: left;
    width: 150px;
}

input {
    width: 120px;  
}

br {
    clear: both;  
}

You can see the result here.

like image 92
Pat Avatar answered Sep 27 '22 23:09

Pat