What is the best way to line up a label and a textarea?

Here is how my code currently looks:

             XXXXX
             XXXXX
Description: XXXXX

My desired output is:

             XXXXX
Description: XXXXX
             XXXXX

Sometimes the "Description" field can span multiple lines.

Code snippet:

<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 styling:

.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;
}

Answer №1

To achieve the desired alignment, it is necessary to place both elements within a container and then apply the alignment properties to the container.

Here's an example:

.formfield {
  display: flex;
  align-items: center;
}
<div class="formfield">
  <label for="textarea">Label for textarea</label>
  <textarea id="textarea" rows="5">Textarea</textarea>
</div>

Answer №2

To make your textareas look great, just remember to enclose them within a label and adjust the textarea style with the following code:

vertical-align: middle;

By applying this simple CSS magic to all textareas on your page, you can enhance their appearance significantly.

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

<label>Add some Description here: <textarea>Type text here</textarea></label>

Answer №3

  1. Make sure the label's height matches the height of the multiline textbox.
  2. Add the cssClass

    .alignTop{vertical-align: middle;}
    to align the label control at the top.

    <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>
    

Answer №4

Make sure the text area box is positioned aligned with the label, instead of aligning the label to the text area.

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

textarea{
    vertical-align: middle;
}

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

Answer №5

To align items vertically in your CSS, you can use the property vertical-align:middle.

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

Answer №6

Give this a shot:

textarea { vertical-align: top; }​ 

Answer №7

Boost the appearance of your table by adding a height to your td elements.

vertical-align: middle; 

With this CSS property, the element targeted will be centered vertically within its parent element. Keep in mind that the height of the td could be restricted by the amount of text it contains.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encounter problem while attempting to download file through Update Panel

I have ensured that my browser is not blocking any popups, but I am encountering an error in my code that I can't seem to identify. protected void btnDownload_Click(object sender, EventArgs e) { var context = new HMangInternationalCon ...

Inaccurate sizing of popup dialogs on SharePoint 2010

Currently, I am in the process of revamping a SharePoint 2007 web application to SharePoint 2010. I was surprised to find that the popup dialog boxes for new items, edit, etc. have inline styles for fixed width and height. Below is an example where div.ms ...

Achieving Vertical Alignment of Two Divs with CSS

I've come across several solutions to my issue, but none of them seem to work in my current situation. I have a banner at the top of my site with two floated columns, and suspect that the navigation menu in the right column may be causing the problem. ...

Identifying activity on a handheld device

I am currently working on a website and I have noticed that it doesn't work as well on mobile devices as it does on desktop. There are performance issues that need to be addressed. I've seen other websites redirecting users to a different page wh ...

"Rearrange elements on a webpage using CSS and HTML to position one

Can we utilize CSS/HTML to visually move a full width element that is positioned below another one so that it appears above without altering the markup order? <div id="first">first</div> <div id="second">second</div> #first {…} ...

Is it possible to enable tab navigation for a button located within a div when the div is in focus?

I have a component set up like this: (Check out the Code Sandbox example here: https://codesandbox.io/s/boring-platform-1ry6b2?file=/src/App.js) https://i.sstatic.net/ZuxdL.png The section highlighted in green is a div. Here is the code snippet: import { ...

Can you provide me with suggestions on how to indicate progress to the user while retrieving data through a sequence of web service calls in asp.net?

I have an ASP.NET control (.NET 3.5) that interacts with a webservice to retrieve data from a database, which is then displayed in gridviews. To prevent timeouts, I make multiple calls to the webservice instead of retrieving all data at once as each query ...

Django is looking for a primary key that I do not possess

Encountering an issue: invalid literal for int() with base 10: 'ljrh' This error occurs when attempting to add a new entry in a model: day = itemize(value, getday(strip(key))) add = Reoccurring(request.user.username, strip(day.Day), strip(day. ...

Upgrade to iOS 8 and experience the new Full Screen Mode feature. When you tap on a text field for the first time, the onscreen keyboard will flash briefly

Recently, I encountered a strange issue with my web app (AngularJS HTML5) after upgrading my iPad to iOS8. The onscreen keyboard started acting strangely - when I tapped a text field once, the keyboard would very briefly flash and then disappear. It was so ...

In search of a dropline menu with a comparable style

My search for a menu like the one found on www.ultragraph.co.uk has been exhaustive. I have only come across simple dropline menus, but I am seeking one with hidden sub-menus that reveal upon mouseover similar to ultragraph.co.uk. If anyone has any sugge ...

Add a fading transition feature to images that are loaded at a later time

I used a clever technique to create a blur effect by loading a small, lightweight image first. Once the main background image is loaded, it swaps out the 'data-src' with the actual image. However, I am facing an issue with the abrupt transition, ...

Cross-origin resource sharing problem arises when JavaScript is loaded asynchronously using script tags created dynamically

By dynamically creating a script as shown below, the JavaScript source is downloaded asynchronously. let newScript = document.createElement('script'); newScript.src = srcUrl; let firstScript = document.getElementsByTagName('script')[0] ...

How can I preloaded self-hosted fonts in Next.js without receiving a console warning for not using the resource within a few seconds?

I am working on a project in Next.js and I want to preload my self-hosted fonts. In my code, I have the following setup: Inside my _app.js file: <Head> <link rel="preload" href="/fonts/leira/Leira-Lite.t ...

Issues regarding innerHTML

I have a collapsed table with a link that collapses its content. My goal is to change the link (such as from "+" to "-" and vice versa) using JavaScript. I was able to achieve this, but now my table no longer collapses. Here is the code snippet: <div c ...

Using jQuery's innerHTML method inside an EJS template

Can someone assist me in passing a dynamic value (ejs) from one page to another? I'm attempting to store the value in a variable and then console log it, but it's not working as expected. Below is the code snippet: <%- include('partials/h ...

What methods can I use to identify my current page location and update it on my navigation bar accordingly?

My latest project involves a fixed sidebar navigation with 3 divs designed to resemble dots, each corresponding to a different section of my webpage. These sections are set to occupy the full height of the viewport. Now, I'm facing the challenge of de ...

Animating a webpage with JQuery: Fade out and fade in effects

Within the scope of a Master page, there exists a logout button, several web pages, and a login page that does not utilize this master layout. The objective is as follows: Upon clicking the logout button, the current page should smoothly transition into t ...

What is the best approach to obtain an inventory of inputs from a particular viewpoint?

I am facing an issue where I have a list of products that require the quantities to be returned along with the reason for return. However, I cannot modify the referenced model as it has an impact on multiple values. Can someone guide me on how to retrieve ...

Ensuring a consistently positioned footer at the bottom

I understand that this might not seem like a significant issue, but I'm encountering some difficulties. In my main.html file, there are three divs. The first div contains the navigation bar, the second div is an "empty" div that uses JQuery/Javascript ...

Placing a list item at the beginning of an unordered list in EJS with MongoDB and Node.js using Express

What I've done: I already have knowledge on how to add an LI to UL, but it always goes to the bottom. What I'm trying to achieve: Add an LI to the top so that when my div.todos-wrapper (which has y-oveflow: hidden) hides overflow, the todos you a ...