CSS
What Is the Correct HTML for Making a textarea?
When you want users to enter multi-line text—like comments, messages, or notes—the HTML <textarea>
element is the perfect solution. Unlike a single-line input field, a <textarea>
lets users type in longer blocks of text.
In this blog post, you’ll learn:
- ✅ The correct HTML syntax for creating a
<textarea>
- 🧪 Real examples with common attributes
- 🎨 How to control its size
- 🧠 Best practices and tips
✅ Basic Syntax of <textarea>
The correct HTML structure looks like this:
<textarea name="message"></textarea>
Unlike other form elements like <input>
, the <textarea>
requires both opening and closing tags. The text inside the tags becomes the default (pre-filled) value.
🧪 Example: A Simple Textarea
<form>
<label for="comments">Your Comments:</label><br>
<textarea id="comments" name="comments" rows="4" cols="50">
Enter your feedback here...
</textarea>
</form>
✅ This creates a textarea that is:
- 4 lines tall (
rows="4"
) - 50 characters wide (
cols="50"
) - Pre-filled with the default text
🎯 Common <textarea>
Attributes
Attribute | Description |
---|---|
name | The key used when the form is submitted |
id | Links to the <label> for accessibility |
rows | Number of visible text lines |
cols | Number of visible character widths |
placeholder | Shows hint text (modern alternative to default content) |
maxlength | Limits the number of characters a user can enter |
Example With placeholder
and maxlength
<textarea name="bio" placeholder="Write your bio..." maxlength="150"></textarea>
✅ This encourages users to enter a brief bio, limited to 150 characters.
💡 Notes on Styling
While rows
and cols
define the default size, you can also style <textarea>
with CSS:
<textarea style="width: 100%; height: 100px;"></textarea>
Or use classes for more reusable styles:
<textarea class="large-textarea"></textarea>
❌ Common Mistakes to Avoid
Mistake | Why It’s a Problem |
---|---|
Using <textarea/> self-closing tag | ❌ Not allowed in HTML—needs a closing tag |
Forgetting name | Form won’t send this field’s data |
Using value="" | Doesn’t work—use content between tags |
🧠 Summary
Feature | Correct HTML |
---|---|
Basic textarea | <textarea name="field"></textarea> |
With size | rows="4" and cols="50" |
Placeholder text | placeholder="Enter here..." |
Pre-filled text | Text between the tags |
✅ Final Thoughts
The <textarea>
tag is essential when you need multi-line input from users. Just remember to include both opening and closing tags, and set helpful attributes like rows
, cols
, or placeholder
for a better user experience.
For modern layouts, use CSS for more precise control over size and styling.