Inline form fields with labels placed on top

Posted by rcourtna on Stack Overflow See other posts from Stack Overflow or by rcourtna
Published on 2010-06-14T23:21:35Z Indexed on 2010/06/14 23:32 UTC
Read the original article Hit count: 190

Filed under:
|
|

I can't believe I'm having to ask this, but I'm at my wit's end.

I'm trying to display 2 form fields inline, but with the label for each field on the top. In ascii art:

Label 1      Label 2
---------    ---------
|       |    |       |
---------    ---------

Should be pretty simple.

<label for=foo>Label 1</label>
<input type=text name=foo id=foo />

<label for=bar>Label 2</label>
<input type=text name=bar id=bar />

This will get me:

        ---------           ---------
Label 1 |       |   Label 2 |       |
        ---------           ---------

To get the labels on top of the boxes, I add display=block:

<label for=foo style="display:block">Label 1</label>
<input type=text name=foo id=foo />

<label for=bar  style="display:block">Label 2</label>
<input type=text name=bar id=bar />

After I do this, the labels are where I want them, but the form fields are no longer inline:

Label 1  
---------
|       |
---------

Label 2  
---------
|       |
---------

I've been unable to find a way to wrap my html so the fields display inline. Can anyone help?

© Stack Overflow or respective owner

Related posts about html

Related posts about css