Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

cols: suggest a textarea’s character width

Control the default display width without setting a text-length limit.

Version: HTML Living Standard

Syntax

<textarea cols="30"></textarea>

The cols attribute gives a textarea’s expected number of characters per line. It takes a positive integer; if absent or invalid the character width defaults to 20. Browsers can use it to size the control, but actual pixel width depends on fonts, borders and styles. It is not a guarantee that every line contains exactly that many visible characters.

CSS width can override the display size, and this attribute does not limit the total text length. That is a separate concern handled by maxlength and application validation. With wrap="hard", a column count is required and submission can include browser-inserted line breaks; the default soft wrapping does not insert those breaks merely because a line wraps visually.

The example compares two controls using the same font but different column counts. It stores forty characters in the narrower field and then gives both fields the same CSS width. The measured result confirms an initial width difference, no truncation of the assigned value and equality after the explicit style override.

Examples

<!doctype html><meta charset="utf-8">
<style>textarea { font: 16px monospace; box-sizing: border-box; }</style>
<label>Narrow <textarea id="narrow" cols="10"></textarea></label>
<label>Wide <textarea id="wide" cols="30"></textarea></label><output id="result"></output>
<script>
const narrow = document.querySelector('#narrow'), wide = document.querySelector('#wide');
narrow.value = 'x'.repeat(40);
const wider = wide.getBoundingClientRect().width > narrow.getBoundingClientRect().width;
narrow.style.width = wide.style.width = '100px';
document.querySelector('#result').textContent = 'wider=' + wider + '; valueLength=' + narrow.value.length + '; equalAfterCSS=' + (wide.getBoundingClientRect().width === narrow.getBoundingClientRect().width);
</script>

Result

wider=true; valueLength=40; equalAfterCSS=true

The test compares dimensions instead of claiming a fixed pixel-per-character ratio across systems.

Sources