Skip to main content

How to Add a Multi-Line Text Box with textarea in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

<textarea> is a text box for more than one line, for things such as comments, messages and addresses. Pressing Enter in it starts a new line instead of sending the form.

Syntax​

Syntax
<textarea name="<name>" rows="<lines>" cols="<characters>"><starting text></textarea>
PartWhat it does
name="<name>"The name its text is sent under when the form is sent.
rows="<lines>"How many lines of text tall the box is.
cols="<characters>"How many characters wide the box is.
<starting text>Optional text the box starts with, written between the two tags.
</textarea>The closing tag. A text area always needs it.

MDN's textarea page lists all its attributes.

The Example​

The example is a comments form: a text area 4 lines tall and 30 characters wide, with a label, and a Send button.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding a label Comments, a text area with the id and name comments, rows 4 and cols 30, and a Send button.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Comments</title>
</head>
<body>
<form>
<label for="comments">Comments</label>
<textarea id="comments" name="comments" rows="4" cols="30"></textarea>
<p><button type="submit">Send</button></p>
</form>
</body>
</html>

Step 2: Type Two Lines​

Double-click index.html to open it in your browser. Click in the box and type Great talk., press Enter, then type More demos, please.:

The page in Chromium: the label Comments next to a text area holding two lines, Great talk. and More demos, please., and a Send button under it.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

Step 3: Send the Form​

Click Send. The address now ends with index.html?comments=Great+talk.%0D%0AMore+demos%2C+please.

What Happened​

  • rows="4" and cols="30" set the box's size: Chromium drew it 263 pixels wide and 68 pixels tall.
  • Enter started a new line inside the box, instead of sending the form.
  • The form sent the text with its line break. In the address, spaces became +, the comma became %2C, and the line break became %0D%0A, the codes for the two characters that end a line.

Common Mistakes​

MistakeWhat happensDo this instead
Writing <textarea /> with no closing tagThe rest of the page ends up inside the box: the Send button disappeared, and the box showed the page's remaining HTML as textAlways write </textarea>
Setting the starting text with a value attributeIt's ignored: with value="Hello", the box stayed emptyPut the starting text between the tags

Troubleshooting​

SymptomLikely causeFix
Part of the page is missing, and the text box shows HTMLThe text area has no </textarea>Add the closing tag
The starting text doesn't showIt's in a value attributeMove it between the tags

FAQ​

Can people resize the box?​

Yes. In Chromium, they drag the handle in its bottom-right corner, which you can see in the screenshot. Dragging it 100 pixels right and 60 down made the box 363 by 128 pixels instead of 263 by 68.

How does a script read the text?​

From its value property. For the two lines typed above, value was "Great talk.\nMore demos, please.", where \n is the line break.

What's the difference between a textarea and an input?​

An input text box holds one line: Enter in it sends the form. A textarea holds as many lines as people need.

Conclusion​

textarea gives people room to write more than one line. Size it with rows and cols, put any starting text between its tags, and always close it with </textarea>.

Additional Resources​