How do I align a label and a textarea?

前端 未结 7 1748
傲寒
傲寒 2020-12-04 22:57

My code ends up like:

             XXXXX
             XXXXX
Description: XXXXX

I want:

             XXXXX
Description: XXXX         


        
相关标签:
7条回答
  • 2020-12-04 23:27

    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>
    
    0 讨论(0)
  • 2020-12-04 23:28

    Use vertical-align:middle in your CSS.

    <table>
        <tr>
           <td style="vertical-align:middle">Description:</td>
           <td><textarea></textarea></td>
        </tr>
    </table>
    
    0 讨论(0)
  • 2020-12-04 23:33

    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>
    
    0 讨论(0)
  • 2020-12-04 23:40
    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>
      

    0 讨论(0)
  • 2020-12-04 23:42

    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>

    0 讨论(0)
  • 2020-12-04 23:44

    Try this:

    textarea { vertical-align: top; }​ 
    
    0 讨论(0)
提交回复
热议问题