CSS Magic: Align Form Fields Without Tables

I’ve seen many a source code that is neat and XHTML-compliant everywhere except—for some mysterious reason—forms. There is some unspoken rule that tables are the only way to align form fields into two even columns. That rule is wrong. Without further ado, here is how to rid your pages of the final vestiges of layout tables (tested in Firefox 3 and IE6, the best and worst of all possible worlds):

HTML:
<div class=”field_container”><label>First Name</label><input type=”text”></div>
<div class=”field_container”><label>Last Name</label><input type=”text”></div>

CSS:
label {
width:150px;   
/*Or however much space you need for the form’s labels*/
    float:left;
}

I’m serious, that’s it. So if I see another table, YOU’RE FIRED. :)

This entry was posted in Angry Development Tips, Design, Technology. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

10 Comments

  1. Posted June 9, 2008 at 6:01 am | Permalink

    No, no, no, NO! WRONG! BZZZZT!

    You have hard-coded a maximum width. What happens when somebody changes the label names, or translates them into a language that is more verbose than English, or resizes their fonts? Using em widths instead of pixels is better, but still not good enough.

    As punishment, you have to keep working on OsCommerce! Oh, wait—that’s the unpleasant cash cow you talked about in a previous post.

  2. Erkki
    Posted June 9, 2008 at 8:41 am | Permalink

    We’ve been using a similar technique with width set using percentages (so with 3 columns around 33%), YMMV.

    How would you implement something like this?

    http://drop.io/xtmb7ws/asset/screen

    I had some ideas but they turned out to be too complicated so I just used tables, shame on me.

    The problem is that the right-hand content has dynamic height and there can be misc other content between some rows (also with no pre-defined height).

    One could float two divs per row but getting the heights to match without using javascript was beyond me.

  3. Posted June 9, 2008 at 12:16 pm | Permalink

    Jim, you have a good point. I guess I’m going to use percentages like Erkki suggested. Meanwhile, I’ve been foiled again. ;-)

  4. Posted June 17, 2008 at 2:55 pm | Permalink

    When to use tables for layout

    http://olav.dk/articles/tables.html

  5. Matvey
    Posted June 22, 2008 at 8:43 pm | Permalink

    the Label element should have a for attribute!!! else it’s not accessible.

  6. Clark Birkelund
    Posted May 16, 2009 at 5:36 am | Permalink

    i did this…

    making it float, it will adapt for the longest text… the css is not seperate, but just a quick sollution :

    Navn og efternavn:*

    Evt. firmanavn:

    Adresse:*

    Postnummer:*

    By:*

    E-Mail:*

    Telefon/mobil:*

    Land:

    Bemærkning/Hvordan fandt du os?   

    <input type=”text” value=”" name=”ContactName” />

    <input type=”text” value=”" name=”CompanyName” />

    <input type=”text” value=”" name=”Address” />

    <input type=”text” value=”" onBlur=”findBy();” id=”ZipCode” name=”ZipCode” style=”width:80px;” />

    <input type=”text” value=”" name=”City” id=”City” />

    <input type=”text” value=”" name=”Email” />

    <input type=”text” value=”" name=”Phone” style=”width:80px; margin-left:0px; padding-left:0px;” />

    Danmark
    Norge
    Sverige

  7. Clark Birkelund
    Posted May 16, 2009 at 5:37 am | Permalink

    Hmmm was not able to post the content…

    have a look at http://www.superstar.dk/delivery.asp

  8. WordPresser
    Posted May 12, 2010 at 2:14 pm | Permalink

    Thanks mariya for this useful technique, and yes as Erkki suggested use percentage.

    Jim, you have control over your forms and CSS whether multilingual or not. They are not changing everyday, and you can actually make the text aligned right for that.

    so you can have this in your CSS,

    label {
    width:30%;
    float:left;
    text-align:right;
    }

    Good-bye tables :)

    Thanks again mariya.

  9. ojiwa
    Posted June 1, 2010 at 4:30 am | Permalink

    very nice am fired tho’ :-)

  10. Dana
    Posted August 25, 2010 at 1:23 pm | Permalink

    Thank you so much, that was very helpful! I had tried several things and only frustrated myself more each time.

    Quick.
    Easy.
    Pretty.

    I like it.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>