tags:

views:

738

answers:

4

ends up like

             XXXXX
             XXXXX
Description: XXXXX

I want

             XXXXX
Description: XXXXX
             XXXXX

"Description" sometimes spans multiple lines.

Code:

<p class="DataForm">

<label>Blah blah blah Description:</label>

<asp:TextBox ID="txtBlahblahblahDescription" runat="server" TextMode="MultiLine"  Rows="8" Columns="50"></asp:TextBox><br />

</p>

CSS:

.DataForm
{

}

.DataForm label
{
    display: inline-block;
    font-size:small;
    margin-left:5px;
    width:5%;
    min-width:60px;
}

.DataForm input
{
    margin-right:9px;
    display: inline-block;
    width:21%;
    min-width:30px;
}
+3  A: 

Use vertical-align:middle in your CSS.

<table>
    <tr>
       <td style="vertical-align:middle">Description:</td>
       <td><textarea></textarea></td>
    </tr>
</table>
Peter Stuifzand
I'm looking for an elegant solution without tables if possible.
SLC
You should write stuff like that in your question.
Peter Stuifzand
Who is using tables for forms nowadays?
BalusC
Sorry, thought it was obvious :(
SLC
I use the following html for parts of a form: `<p><label>Description:<br><textarea></textarea></label></p>`. This way, when you click the `label` element it will focus the `textarea` element. It's not optimal yet, but I'm getting some nice results with this.
Peter Stuifzand
Cool, I wanted to keep the text areas on the same line ;)
SLC
Tables are perfectly valid for forms. It *is* tabular data, albeit with only one data row (or column).
DisgruntledGoat
@Peter Stuifzand, out of curiosity, is there a reason you've opted to use `<p>` to contain the label/description pairs, rather than -just for example- `<li>`s contained in a list?
David Thomas
The biggest reason that I use `<p>` is that it without styling already shows the way I want it. With `<li>` (and other) I need to have extra styling.
Peter Stuifzand
A: 

Try setting a height on your td elements.

vertical-align: middle;

means the element the style is applied to will be aligned within the parent element. The height of the td may be only as high as the text inside.

h4xnoodle
What `td` elements?
David Thomas
+2  A: 

You need to put them both in some container element and then apply the alignment on it.

For example:

<p class="formfield">
    <label for="textarea">Label for textarea</label>
    <textarea id="textarea" rows="5">Textarea</textarea>
</p>

with

.formfield * {
    vertical-align: middle;
}
BalusC
Hmm, I tried putting that as an inline style on my p tag, but it didnt work
SLC
In your case, apply it on `.DataForm * {}`.
BalusC
Wouldn't it be inherited? I tried it anyway, but it did not work :(
SLC
My bad, it does work with dataform *, although the way it centers it is kind of odd, it works, so thanks
SLC
The oddness is probably caused by (implicit) top/bottom margins/paddings. Set those of both label and textarea equal to get a "perfect" center.
BalusC
A better solution would just be to put `vertical-align: middle;` in each of your rules above, `.DataForm label` and `.DataForm textarea`. Using the * selector can sometimes have side effects.
DisgruntledGoat
You may be right, but if one would `vertical-align: middle` something, one would usually rather apply it on the complete block.
BalusC