The HTML cols attribute
The HTML cols attribute sets how wide a <textarea> is, counted in average character widths, as in cols="40". The default is 20. A CSS width overrides it, and wrap="hard" uses it to break lines when the form is submitted.
Overview
The HTML cols attribute sets the visible width of a <textarea>, measured in average character widths. It takes a whole number greater than zero and defaults to 20. Invalid values such as 0 or -3 fall back to 20 as well.
Because the unit is an average character, the real width depends on the font. In our Chrome test with 16px Arial, a textarea with cols="10" had a 95px text area, while ten capital W letters measured about 151px and ten lowercase i letters about 36px. Wide letters wrap sooner and narrow ones later.
A CSS width overrides cols whenever both are present, so a rule like width: 100% makes the attribute irrelevant. Large values can also overflow a phone screen. In Chrome's default textarea font, cols="80" came out 608px wide, so add max-width 100% as a guard.
The attribute also shapes submission. For wrap="hard", the HTML standard has the browser insert line breaks into the submitted value so no line is longer than cols characters, and it requires cols whenever wrap is hard.
Syntax
<label for="notes">Notes</label>
<textarea id="notes" name="notes" cols="50" rows="4"></textarea>
Values
| Value |
|---|
| A positive integer (default 20). |
Example
<p><label for="narrow">cols="20", the default</label><br>
<textarea id="narrow" rows="2">Twenty average characters wide</textarea></p>
<p><label for="wide">cols="36"</label><br>
<textarea id="wide" cols="36" rows="2">Thirty-six average characters wide</textarea></p>
Best practices
- Treat
colsas a fallback width and set the real layout with CSSwidthormax-width. - Add
max-width: 100%so a largecolsvalue can't overflow a small screen. - Expect wide letters to wrap sooner than
colssuggests in proportional fonts, since it counts average characters. - Set
colswhenever you use wrap with the valuehard, as the HTML standard requires.
Frequently asked questions
How wide does the cols attribute make a textarea?
What is the default cols value of a textarea?
Why doesn't cols change my textarea's width?
width: 100% replace the width that cols asks for, and MDN notes that cols has no effect under field-sizing: content.How do I make a textarea full width?
width: 100% in CSS and keep cols as a fallback. Add box-sizing: border-box as well, so the padding doesn't push the textarea past its container.Does cols matter for wrap=hard?
wrap=hard is set. Under that rule, the submitted text is broken into lines no longer than cols characters.