How do I align a label and a textarea?

HtmlCssasp.net

Html Problem Overview


My code 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;
}

Html Solutions


Solution 1 - Html

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

For example:

.formfield * {
  vertical-align: middle;
}

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

Solution 2 - Html

Just wrap the textarea with the label and set the textarea style to

vertical-align: middle;

Here is some magic for all textareas on the page:)

<style>
    label textarea{
        vertical-align: middle;
    }
</style>

<label>Blah blah blah Description: <textarea>dura bura</textarea></label>

Solution 3 - Html

  1. Set the height of your label to the same height as the multiline textbox.

  2. Add the cssClass .alignTop{vertical-align: middle;} for the label control.

    <p>
        <asp:Label ID="DescriptionLabel" runat="server" Text="Description: " Width="70px" Height="200px" CssClass="alignTop"></asp:Label>
        <asp:Textbox id="DescriptionTextbox" runat="server" Width="400px" Height="200px" TextMode="MultiLine"></asp:Textbox>
        <asp:RequiredFieldValidator id="DescriptionRequiredFieldValidator" runat="server" ForeColor="Red"
        ControlToValidate="DescriptionTextbox" ErrorMessage="Description is a required field.">    
    </asp:RequiredFieldValidator>
    

Solution 4 - Html

Use vertical-align:middle in your CSS.

<table>
    <tr>
       <td style="vertical-align:middle">Description:</td>
       <td><textarea></textarea></td>
    </tr>
</table>

Solution 5 - Html

Align the text area box to the label, not the label to the text area,

label {
    width: 180px;
    display: inline-block;
}

textarea{
    vertical-align: middle;
}

<label for="myfield">Label text</label><textarea id="myfield" rows="5" cols="30"></textarea>

Solution 6 - Html

Try this:

textarea { vertical-align: top; }​ 

Solution 7 - Html

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNibblyPigView Question on Stackoverflow
Solution 1 - HtmlBalusCView Answer on Stackoverflow
Solution 2 - HtmlRadoView Answer on Stackoverflow
Solution 3 - HtmlCharlieView Answer on Stackoverflow
Solution 4 - HtmlPeter StuifzandView Answer on Stackoverflow
Solution 5 - HtmlMark BView Answer on Stackoverflow
Solution 6 - Htmlvaibhav jainView Answer on Stackoverflow
Solution 7 - Htmlh4xnoodleView Answer on Stackoverflow