The HTML <textarea> element collects multi-line text such as comments, descriptions, messages or article content.
<label for="message">Message</label>
<textarea id="message" name="message" rows="4" cols="40"></textarea>
<input>, a textarea does not use a value attribute for its initial text. The initial value goes between the opening and closing <textarea> tags.<textarea name="txtArea" rows="3" cols="50">Textarea Data</textarea>
This creates a multi-line editing control with initial text of Textarea Data.
| Attribute | Purpose |
|---|---|
name | Defines the field name used when the form is submitted. |
id | Identifies the element and connects it to a label. |
rows | Sets the visible number of text lines. |
cols | Provides an approximate visible width in character columns. |
maxlength ![]() | Sets the maximum number of characters accepted. |
minlength ![]() | Sets the minimum number of characters for browser constraint validation. |
placeholder ![]() | Shows a short hint while the textarea has no value. |
autocomplete ![]() | Provides browser autofill guidance. |
autofocus ![]() | Requests focus when the page loads. |
readonly | Prevents editing while keeping the value available for normal form submission. |
disabled | Disables interaction and normally excludes the control from form submission. |
required ![]() | Requires a non-empty value for browser constraint validation. |
wrap | Controls how line wrapping is handled when text is submitted. |
form ![]() | Associates the textarea with a form by that form's id, even when the textarea is outside the form element. |
The name is used for submitted form data. The id uniquely identifies the control in the document and can connect it to a visible label.
<label for="bio">Short biography</label>
<textarea id="bio" name="bio" rows="5"></textarea>
name is not included in the submitted form data. A placeholder should not replace a visible label.Place the initial text between the opening and closing tags:
<textarea name="notes">Initial notes</textarea>
Do not write:
<!-- Incorrect for setting textarea content -->
<textarea name="notes" value="Initial notes"></textarea>
The value attribute does not set textarea content.
rows controls the visible height in text lines, while cols provides an approximate visible width in character columns.
<textarea name="description" rows="6" cols="50"></textarea>
Demo of textarea rows and cols
For responsive layout, CSS is usually better for width:
<textarea name="description" rows="6" style="width: 100%;"></textarea>
In reusable pages, place the style rule in a stylesheet rather than repeating inline CSS.
<label for="summary">Summary</label>
<textarea
id="summary"
name="summary"
minlength="20"
maxlength="500"
required
></textarea>
Demo of maxlength and minlength
<label for="feedback">Feedback</label>
<textarea id="feedback" name="feedback" placeholder="Tell us what you liked or what could be improved"></textarea>
autocomplete can be used to provide browser autofill guidance where an applicable autocomplete token exists.
<textarea name="address" autocomplete="street-address"></textarea>
Use meaningful autocomplete tokens rather than treating on and off as the only possibilities.
<textarea name="message" autofocus></textarea>
<textarea name="terms" readonly>These terms cannot be edited.</textarea>
A readonly textarea cannot be edited, but users can normally focus/select its text and its value is normally submitted with the form.
<textarea name="terms" disabled>This control is disabled.</textarea>
A disabled textarea is not normally focusable and is excluded from normal form submission.
<label for="comment">Comment</label>
<textarea id="comment" name="comment" required></textarea>
The server must still check required data because client-side validation can be bypassed.
The wrap attribute controls how text wrapping is represented when the form value is submitted.
| Value | Behavior |
|---|---|
soft | Text may wrap visually in the control, but the browser does not add line breaks merely because of that visual wrapping. |
hard | Visual wrapping is represented with line breaks in the submitted value; cols is required for this behavior. |
<textarea name="notes" rows="5" cols="40" wrap="hard"></textarea>
hard and soft do not mean "wrap" versus "do not wrap"; the important distinction is whether visual line wrapping is represented in the submitted value.A textarea can belong to a form even when it is outside that form's opening and closing tags.
<form id="feedback_form" action="save.php" method="post">
<button type="submit">Submit</button>
</form>
<label for="feedback2">Feedback</label>
<textarea id="feedback2" name="feedback" form="feedback_form"></textarea>
<form action="save-feedback.php" method="post">
<label for="feedback3">Your feedback</label>
<textarea
id="feedback3"
name="feedback"
rows="6"
maxlength="1000"
placeholder="Enter your feedback"
required
></textarea>
<button type="submit">Send feedback</button>
</form>
A textarea is submitted as a normal string value. Validate it on the server before storing or using it.
<?php
$feedback=$_POST['feedback'] ?? '';
$feedback=trim($feedback);
if($feedback === ''){
echo 'Feedback is required.';
}elseif(strlen($feedback) > 1000){
echo 'Feedback is too long.';
}else{
echo nl2br(
htmlspecialchars(
$feedback,
ENT_QUOTES,
'Windows-1252'
)
);
}
If an application puts stored or submitted text back inside a textarea, HTML-escape the dynamic value.
<?php
$value='Example <text> & notes';
?>
<textarea name="notes"><?= htmlspecialchars(
$value,
ENT_QUOTES,
'Windows-1252'
) ?></textarea>
This prevents dynamic content such as </textarea> from being interpreted as markup and prematurely closing the element.
Managing textarea content with jQuery
Put the initial text between the opening and closing textarea tags.
Use a visible label for the field name and placeholder only for a short hint.
A disabled textarea is normally excluded from normal form submission.
Readonly content is normally submitted; disabled content is normally not submitted.
The difference concerns whether visual wrapping is represented by line breaks in the submitted value.
Browser validation can be bypassed. Validate again on the server.
Escape dynamic values before placing them between textarea tags.
It is used for multi-line text input such as comments, messages, descriptions and article content.
Place the initial text between the opening and closing textarea tags. Do not use a value attribute.
Rows controls the visible number of text lines. Cols provides an approximate visible width in character columns.
Readonly prevents editing but the value is normally submitted. Disabled prevents normal interaction and the control is normally excluded from submission.
It causes visual wrapping to be represented with line breaks in the submitted value; cols is required for this behavior.
Yes. The form attribute can associate it with a form by that form's id.
No. Validate textarea length and content again on the server.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.