Textarea & Select Elements

Textarea & Select Elements

Master multi-line text inputs and dropdown selections. Learn textarea sizing, select options, optgroups, multiple selections, and advanced form control techniques.

How do textarea and select work in HTML?

<textarea> is a multi-line text field: its default value goes between the tags, and rows sets its height. <select> creates a dropdown from <option> elements; the selected option's value is submitted. Add multiple to a select to allow several choices, and use <optgroup> to group options.

<label for="bio">Bio</label>
<textarea id="bio" name="bio" rows="4" maxlength="500">Default text</textarea>

<label for="lang">Language</label>
<select id="lang" name="lang">
  <option value="">Choose…</option>
  <optgroup label="Frontend">
    <option value="js" selected>JavaScript</option>
    <option value="ts">TypeScript</option>
  </optgroup>
</select>

Textarea & Select Overview

Textarea and select elements provide essential form controls for multi-line text input and option selection. These elements offer rich functionality for collecting user data beyond simple text inputs.

Key Elements:

Textarea Element:

  • Multi-line text input
  • Resizable by users
  • Character counting

Select Element:

  • Dropdown selections
  • Option grouping
  • Multiple selections

Textarea Element Guide

The textarea element provides a multi-line text input control, perfect for comments, messages, descriptions, and any content that requires more than a single line.

Key Attributes

rows:Visible text lines (default: 2)
cols:Visible character width (default: 20)
maxlength:Maximum character limit
minlength:Minimum character requirement
wrap:Text wrapping (soft/hard/off)

CSS Properties

resize:none | both | horizontal | vertical
overflow:Control scrollbar behavior
white-space:Text formatting control
line-height:Spacing between lines

Pro Tips

  • • Use placeholder for helpful instructions
  • • Implement character counting with JavaScript
  • • Consider auto-resize functionality for better UX
  • • Use required attribute for validation
  • • Set appropriate rows to avoid scrolling
  • • Use spellcheck="true" for text content

Select Element Mastery

Select elements provide dropdown menus and list boxes for option selection. They support grouping, multiple selections, and rich data attributes for dynamic interactions.

Select Types & Variations

Basic Dropdown

<select name="choice">
 <option value="1">One</option>
</select>

Multiple Select

<select name="choices" multiple>
 <option value="1">One</option>
</select>

With Size

<select size="4">
 <option value="1">One</option>
</select>

Option Groups (optgroup)

Organize related options into groups for better user experience and navigation:

Benefits:

  • Logical grouping of options
  • Improved accessibility
  • Better visual organization
  • Easier navigation in large lists

Use Cases:

  • • Product categories
  • • Geographic regions
  • • Skills by expertise level
  • • Menu categories

Important Considerations

Accessibility:

  • • Always provide labels
  • • Use meaningful option text
  • • Consider keyboard navigation
  • • Test with screen readers

User Experience:

  • • Avoid very long option lists
  • • Provide search for large datasets
  • • Use clear, concise option text
  • • Consider mobile usability

Interactive Demo

Explore textarea and select elements with this comprehensive interactive demo featuring character counting, option groups, multiple selections, and dynamic content.

Code Editor

<label for="bio">Bio</label>
<textarea id="bio" rows="3" maxlength="100">I'm learning HTML.</textarea>
<small id="count"></small>

<label for="lang">Favourite language</label>
<select id="lang">
  <option value="">Choose…</option>
  <optgroup label="Frontend"><option>HTML</option><option selected>CSS</option></optgroup>
  <optgroup label="Programming"><option>JavaScript</option></optgroup>
</select>

Preview

Best Practices & Tips

Textarea Best Practices

  • • Set appropriate rows for content length
  • • Use maxlength with character counter
  • • Enable spellcheck for text content
  • • Consider auto-resize functionality
  • • Provide clear placeholder text
  • • Test resize behavior across browsers

Common Mistakes

  • • Making textareas too small
  • • Forgetting to provide labels
  • • Using too many options in select
  • • Not testing on mobile devices
  • • Ignoring keyboard navigation

Select Best Practices

  • • Use optgroups for logical grouping
  • • Provide meaningful default options
  • • Consider search for large lists
  • • Use data attributes for metadata
  • • Test multiple selection UX
  • • Ensure mobile accessibility

Performance Tips

  • • Limit options in large datasets
  • • Use lazy loading for dynamic content
  • • Optimize option rendering
  • • Consider virtual scrolling
  • • Implement efficient search

Textarea and Select — step by step

1 / 4
1

Use textarea for multi-line input

The textarea element creates a multi-line text input field. Unlike input, it has a closing tag and its default value goes between the tags. Use rows and cols attributes or CSS to control its size.

1 of 4
💡Use optgroup to Organize Long Dropdowns
When a select element has many options, group related ones using the optgroup element with a label attribute. This creates visual groupings in the dropdown and improves usability without affecting the submitted values.

🎯 Quick Check

How do you allow users to select multiple options in a select element?

HTML Textarea and Select: summary

Key points

  • Textarea has no value attribute in HTML; put the default text between the tags.
  • resize: vertical in CSS stops users from resizing the textarea horizontally.
  • A first option with an empty value plus required forces the user to make a choice.

Frequently asked questions

›How do I set a default selected option?

Add the selected attribute to that <option>.

›How do I limit the characters in a textarea?

Use maxlength="500". Show a live counter with JavaScript if users need to see how many are left.