What is the HTML <textarea> element?

Learn about the HTML <textarea> element for multi-line text input. Syntax, attributes, practical examples and best practices explained.
9 min read
Believemy logo

Introduction to the <textarea> element

The <textarea> element is a fundamental HTML form component that allows users to enter text across multiple lines. Unlike the <input> element which is limited to a single line of text, theΒ <textarea> provides an expandable input area, perfect for comments, messages, long descriptions, or any textual content requiring more space.

If you want to master HTML forms and create professional user interfaces, our comprehensive HTML course will guide you step by step through learning these essential concepts. You'll discover how to create interactive and accessible forms that meet modern web standards.

Good to know

πŸ’‘ The <textarea> element is essential for any contact form, comment system, or long content input interface!

In this article, we will explore in depth how the <textarea> element works, its many attributes, practical use cases, and best practices for creating accessible and user-friendly text fields.

Β 

Definition and basic syntax

Fundamental structure

The <textarea> element is an HTML form element that creates a multi-line text input area. Unlike most form elements, it requires both an opening and closing tag, and the default text is placed between these tags.

HTML
<textarea>Your default text here</textarea>

The basic syntax with the most common attributes looks like this:

HTML
<textarea name="message" rows="5" cols="40" placeholder="Write your message..."></textarea>

Here are the key elements of this syntax:

  • name: identifies the field when submitting the form
  • rows: defines the number of visible lines
  • cols: defines the number of visible columns (characters)
  • placeholder: displays grayed-out hint text

Β 

Difference from the input element

It's important to understand the fundamental difference between <textarea> and <input type="text">:

Feature<textarea><input type="text">
Text linesMultiple linesSingle line
ResizingPossible (by default)No
Default valueBetween tagsvalue attribute
Line breaksPreservedNot allowed

Β 

Association with a label

For better accessibility, you should always associate your <textarea> with a label element. This allows screen readers to correctly identify the field and improves user experience:

HTML
<label for="comment">Your comment:</label>
<textarea id="comment" name="comment" rows="4" cols="50"></textarea>
Warning

Never forget to associate a label with your textarea! It's essential for accessibility and your form's SEO.

Β 

Textarea element attributes

Sizing attributes

The rows and cols attributes define the visible size of the text field. These values represent the number of lines and character width respectively:

HTML
<!-- Small comment field -->
<textarea rows="3" cols="30"></textarea>

<!-- Large field for long content -->
<textarea rows="10" cols="80"></textarea>

However, we recommend using CSS to manage dimensions, as it offers more flexibility and better responsive control:

CSS
.textarea-believemy {
  width: 100%;
  min-height: 150px;
  max-height: 400px;
  resize: vertical;
}

Β 

Behavior attributes

Several attributes control the behavior of the <textarea>:

AttributeDescriptionValues
disabledCompletely disables the fieldBoolean
readonlyRead-only (not modifiable)Boolean
requiredRequired fieldBoolean
autofocusAutomatic focus on page loadBoolean
wrapLine wrapping managementsoft, hard, off

Here's an example combining several of these attributes:

HTML
<form action="/submit" method="post">
  <label for="description">Believemy project description:</label>
  <textarea 
    id="description" 
    name="description" 
    required 
    autofocus
    rows="6"
    placeholder="Describe your training project..."
  ></textarea>
  <button type="submit">Submit</button>
</form>

Β 

Validation attributes

HTML5 introduced native validation attributes for <textarea> that allow client-side content verification before form submission:

HTML
<textarea 
  name="review" 
  minlength="20" 
  maxlength="500"
  required
  placeholder="Your review must contain between 20 and 500 characters"
></textarea>
  • minlength: minimum number of required characters
  • maxlength: maximum number of allowed characters
  • required: makes the field mandatory

Β 

The wrap attribute explained

The wrap attribute controls how text is formatted when the form is submitted:

  • soft (default): text appears with visual line breaks, but they are not included when submitted
  • hard: line breaks are included in the submitted data (requires the cols attribute)
  • off: no automatic line wrapping, text scrolls horizontally
HTML
<!-- The submitted text will contain line breaks -->
<textarea wrap="hard" cols="40" name="formatted_text"></textarea>

Β 

Practical examples and use cases

Complete contact form

Here's an example of a professional contact form integrating a <textarea> for the message:

HTML
<form action="/contact" method="post" class="contact-form">
  <div class="field">
    <label for="name">Your name:</label>
    <input type="text" id="name" name="name" required>
  </div>
  
  <div class="field">
    <label for="email">Your email:</label>
    <input type="email" id="email" name="email" required>
  </div>
  
  <div class="field">
    <label for="subject">Message subject:</label>
    <input type="text" id="subject" name="subject" required>
  </div>
  
  <div class="field">
    <label for="message">Your message:</label>
    <textarea 
      id="message" 
      name="message" 
      rows="8" 
      required
      minlength="50"
      maxlength="2000"
      placeholder="Describe your request in detail (minimum 50 characters)..."
    ></textarea>
  </div>
  
  <button type="submit">Send message</button>
</form>

Β 

Comment system

For a comment system on a site like Believemy, we can create a styled and functional <textarea>:

HTML
<div class="comments-section">
  <h3>Leave a comment on this course</h3>
  
  <form action="/comments/add" method="post">
    <input type="hidden" name="course_id" value="html-css">
    
    <div class="form-group">
      <label for="comment">Your comment:</label>
      <textarea 
        id="comment" 
        name="content"
        rows="5"
        required
        minlength="10"
        maxlength="1000"
        placeholder="Share your experience with this Believemy course..."
      ></textarea>
      <span class="character-counter">0 / 1000</span>
    </div>
    
    <button type="submit" class="btn-comment">Post comment</button>
  </form>
</div>

Β 

Simple content editor

For a content editing interface, the <textarea> can serve as the base for a more complex editor:

HTML
<div class="content-editor">
  <div class="toolbar">
    <button type="button" data-action="bold"><strong>B</strong></button>
    <button type="button" data-action="italic"><em>I</em></button>
    <button type="button" data-action="link">πŸ”—</button>
  </div>
  
  <textarea 
    id="editor" 
    name="article_content"
    rows="20"
    placeholder="Write your article here..."
    spellcheck="true"
  ></textarea>
  
  <div class="preview" id="preview"></div>
</div>

Β 

CSS styling for textarea

Basic styles

The <textarea> can be fully customized with CSS. Here's an example of modern styles:

CSS
/* Basic textarea style */
.textarea-believemy {
  width: 100%;
  min-height: 120px;
  padding: 12px 16px;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.5;
  color: #333;
  background-color: #fff;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  resize: vertical;
}

/* Focus state */
.textarea-believemy:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

/* Disabled state */
.textarea-believemy:disabled {
  background-color: #f5f5f5;
  color: #999;
  cursor: not-allowed;
}

/* Invalid state */
.textarea-believemy:invalid {
  border-color: #e53e3e;
}

/* Placeholder */
.textarea-believemy::placeholder {
  color: #999;
  opacity: 1;
}

Β 

Resize control

By default, browsers allow users to resize the <textarea>. You can control this behavior with the CSS resize property:

CSS
/* Allow only vertical resizing */
.textarea-vertical {
  resize: vertical;
}

/* Allow only horizontal resizing */
.textarea-horizontal {
  resize: horizontal;
}

/* Allow both directions (default) */
.textarea-both {
  resize: both;
}

/* Prevent all resizing */
.textarea-fixed {
  resize: none;
}
Good to know

Tip: use resize: vertical for most use cases. This allows users to enlarge the field if needed while preserving the layout.

Β 

Responsive design

For a <textarea> that adapts to all screens:

CSS
.textarea-responsive {
  width: 100%;
  min-height: 100px;
  max-width: 100%;
  box-sizing: border-box;
}

@media (min-width: 768px) {
  .textarea-responsive {
    min-height: 150px;
  }
}

@media (min-width: 1024px) {
  .textarea-responsive {
    min-height: 200px;
  }
}

Β 

Best practices and accessibility

Textarea accessibility

To make your <textarea> elements accessible to all users, follow these essential recommendations:

  • Always use a label: associate each textarea with a label using the for and id attributes
  • Provide clear instructions: use the aria-describedby attribute to link additional instructions
  • Indicate required fields: use aria-required="true" in addition to the required attribute
  • Handle errors: use aria-invalid and aria-errormessage to signal issues
HTML
<div class="form-group">
  <label for="bio">Your biography <span class="required">*</span></label>
  <span id="bio-instructions" class="instructions">
    Describe yourself in a few sentences (50-500 characters)
  </span>
  <textarea 
    id="bio" 
    name="biography"
    aria-describedby="bio-instructions bio-error"
    aria-required="true"
    minlength="50"
    maxlength="500"
    required
  ></textarea>
  <span id="bio-error" class="error" role="alert"></span>
</div>

Β 

Performance optimization

For <textarea> elements that may contain a lot of text, consider these optimizations:

  • Debounce events: limit the frequency of processing on input or keyup events
  • Lazy load content: for very long texts, load content progressively
  • Limit maximum size: use maxlength to prevent abuse
HTML
<!-- Optimized textarea with performance attributes -->
<textarea 
  id="course-content"
  name="content"
  maxlength="10000"
  spellcheck="false"
  autocomplete="off"
  autocapitalize="sentences"
></textarea>

Β 

Security and validation

Never forget that client-side validation is only a user convenience. You MUST always validate and sanitize data server-side:

Warning

HTML attributes like maxlength or required can be bypassed. Always validate data on your server before any processing!

Β 

Common mistakes to avoid

Mistake 1: Forgetting the closing tag

Unlike <input>, the <textarea> requires a closing tag:

HTML
<!-- ❌ INCORRECT -->
<textarea name="message" />

<!-- βœ… CORRECT -->
<textarea name="message"></textarea>

Β 

Mistake 2: Using the value attribute

The <textarea> doesn't use the value attribute. Default content goes between the tags:

HTML
<!-- ❌ INCORRECT -->
<textarea name="message" value="Default text"></textarea>

<!-- βœ… CORRECT -->
<textarea name="message">Default text</textarea>

Β 

Mistake 3: Unwanted whitespace

Be careful with spaces and line breaks between tags, they will be included as content:

HTML
<!-- ❌ INCORRECT - contains whitespace -->
<textarea name="message">
  
</textarea>

<!-- βœ… CORRECT - no extra whitespace -->
<textarea name="message"></textarea>

Β 

Mistake 4: Neglecting accessibility

Never forget to associate a label with your <textarea>:

HTML
<!-- ❌ INCORRECT -->
<textarea name="comment" placeholder="Your comment"></textarea>

<!-- βœ… CORRECT -->
<label for="comment">Your comment:</label>
<textarea id="comment" name="comment"></textarea>

Β 

Browser compatibility

The <textarea> element enjoys excellent compatibility with all modern and legacy browsers:

BrowserMinimum versionSupport
Chrome1.0βœ… Full
Firefox1.0βœ… Full
Safari1.0βœ… Full
Edge12βœ… Full
Opera1.0βœ… Full
Internet Explorer5.5βœ… Full

HTML5 attributes like placeholder, minlength, maxlength, and required have been supported by all modern browsers for several years.

Β 

Frequently asked questions about textarea

Question

What's the difference between rows/cols and CSS dimensions?

The rows and cols attributes define dimensions in terms of characters and lines, while CSS allows precise control in pixels, percentages, or other units. We recommend using CSS for layout, as it offers more flexibility and better adaptation to different screen sizes.

Β 

Question

How do I prevent textarea resizing?

Use the CSS property resize: none; to completely block resizing, or resize: vertical; to allow only vertical resizing, which is generally the best compromise between usability and layout control.

Β 

Question

How do I create a character counter for a textarea?

You can use JavaScript to listen to the input event on the <textarea> and update a counter displayed next to the field. Combine this with the maxlength attribute to automatically limit input.

Β 

Question

How can I learn HTML and master forms?

To perfectly master HTML forms and all elements like <textarea>, we recommend following our course to learn HTML. You'll discover all the techniques to create professional, accessible forms optimized for SEO.

Related terms

Discover our hTML and CSS glossary

Browse the terms and definitions most commonly used in HTML and CSS development.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.