Back to HTML

The textarea Element for Multi-line Input

Coding Tutorials Team 28 June 2026

For anything longer than a single line, the textarea is the control. It differs from an input in one structural way that produces a distinctive class of bug.

The value goes between the tags

Unlike every other form control, a textarea has no value attribute. Its initial content is whatever sits between the opening and closing tags.

<label for="message">Your message</label> <textarea id="message" name="message" rows="6"></textarea>

Note that the tags are adjacent with nothing between them. Any whitespace there becomes part of the value, so a textarea written across several lines for readability starts life containing a newline and some spaces. The field then looks empty while a validation check for emptiness fails, which is a genuinely confusing bug to encounter.

The same applies when repopulating a form after a failed submission. The previous value must be placed between the tags rather than in an attribute, and it must be escaped, since anything resembling markup will otherwise be parsed as part of the document.

Sizing it

The rows attribute sets the visible height in lines and the cols attribute sets the width in characters. Both have defaults, so a textarea with neither still renders at a usable size.

In practice, width is better controlled in CSS, since a character based measurement does not correspond to anything in a modern layout. Rows remains useful because it sets the initial height in a way that reserves space before the stylesheet arrives.

Set rows to something appropriate for the expected answer. A single line box invites a single line, and a large one signals that detail is welcome. It is one of the more effective ways of influencing what people write.

Resizing

Browsers allow users to drag a textarea larger by default, which is a genuinely useful affordance. The CSS resize property controls it.

textarea { resize: vertical; min-height: 8rem; }

Restricting to vertical is the common compromise, since horizontal resizing usually breaks a layout while vertical rarely does. Disabling resizing entirely is worth avoiding, because someone writing a long response has no other way to see what they have written.

Automatic growth

A textarea that expands as the user types is a common enhancement and requires a small amount of scripting, since there is no native mechanism. The usual technique resets the height and then sets it to the content height on each input event.

Keep a sensible minimum and maximum. A field that grows without limit pushes the submit button off the screen, which is worse than a scrollbar.

Character limits

The maxlength attribute caps the number of characters and prevents further typing once reached. It is enforced by the browser and, like all client side constraints, must be repeated on the server.

Silently refusing further input is a poor experience, so a visible counter is close to mandatory when a limit exists. Announce it politely to assistive technology using a live region, and update it as the user types rather than only on submission.

Consider whether the limit is necessary at all. Many exist because of an arbitrary database column size rather than any editorial reason, and a limit that cuts off a reasonable answer is a limit worth revisiting.

Wrapping and line breaks

The wrap attribute controls how the submitted value treats line breaks. The default soft wraps text visually and submits only the breaks the user actually typed. A value of hard inserts real line breaks at every wrap point, which requires the cols attribute to be set and is rarely what anyone wants.

Line breaks in the submitted value are preserved as characters. Displaying that content later requires either converting them to break elements or applying a CSS white space value that preserves them, or the text will render as one continuous paragraph.

Spellcheck and autocomplete

Spell checking is on by default for a textarea, which is usually right for prose and wrong for code or reference numbers. The spellcheck attribute switches it off where the red underlines would be noise.

Browsers may also restore a previous value when a user navigates back, which is helpful and occasionally surprising if you rely on the field being empty on load.

It is not a rich text editor

A textarea holds plain text only. Formatting requires either a separate syntax such as Markdown, which keeps the control simple and the storage safe, or a rich editor built on a contenteditable region, which is considerably more complex and brings a set of sanitising obligations with it.