HTML cols Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Forms & Input

Introduction

The cols attribute is a valuable feature in HTML that is specifically used with the <textarea> element to define the visible width of a text area. This attribute allows developers to specify the number of visible columns in the text area, providing control over the layout and presentation of multiline text input.

What You’ll Learn

01

Character Columns

40, 50, 80.

02

textarea Only

Where cols applies.

03

With rows

Width and height.

04

CSS width

Responsive option.

05

JavaScript

Dynamic cols.

06

Form Design

Better text areas.

Purpose of cols

The primary purpose of the cols attribute is to define the visible width of the text area, representing the number of columns that should be visible to the user. It helps in controlling the presentation of multiline text input fields, ensuring a consistent and visually appealing layout.

💡
cols vs rows

cols controls visible width in character columns. rows controls visible height in text lines. Use both together for a balanced textarea size.

📝 Syntax

Add cols to a <textarea> element with a positive integer:

cols.html
<textarea cols="40" rows="5">Enter text here...</textarea>

Syntax Rules

  • Only valid on <textarea> elements.
  • Value must be a positive integer (e.g. 20, 40, 80).
  • Represents average character width, not exact pixels.
  • Pair with rows to control both dimensions of the text area.

💎 Values

The cols attribute accepts a numerical value representing the number of columns in the text area. The value should be a positive integer.

  • cols="20" — Narrow text area, suitable for short comments.
  • cols="40" — Common default for general message fields.
  • cols="80" — Wide text area for longer content or code snippets.
cols-values.html
<textarea cols="20" rows="3">Short comment</textarea>
<textarea cols="40" rows="5">Standard message</textarea>
<textarea cols="80" rows="10">Long content</textarea>

⚡ Quick Reference

Use Casecols ValueNotes
Short commentcols="20"Compact field
Contact messagecols="40"Common default
Article draftcols="80"Wide writing area
Height controlrows="5"Pair with cols
Applicable element<textarea>Only textarea
Responsive widthCSS width: 100%Combine with max-width

Applicable Elements

ElementSupported?Notes
<textarea>YesPrimary and only standard use
<input>NoUse CSS width on single-line inputs
<table>, <col>NoUse colspan for table columns
<div>Nocols is not a global width attribute

Examples Gallery

Basic textarea width with cols and dynamic JavaScript adjustment.

👀 Live Preview

Textarea with cols="40" and rows="5":

Example

Let’s look at a simple example of how to use the cols attribute:

cols.html
<textarea cols="40" rows="5">Enter text here...</textarea>
Try It Yourself

How It Works

In this example, the cols attribute is set to 40, indicating that the text area should be initially displayed with 40 visible columns.

Dynamic Values with JavaScript

Just like other HTML attributes, you can dynamically set the cols attribute using JavaScript. This can be particularly useful when you want to adjust the visible width of a text area based on user interactions or certain conditions. Here’s a brief example:

dynamic-cols.html
<textarea id="dynamicTextArea" rows="4"></textarea>

<script>
  // Dynamically set cols for a text area
  document.getElementById("dynamicTextArea").cols = 50;
</script>
Try It Yourself

How It Works

In this script, the cols attribute is set to 50 for a text area with the id dynamicTextArea. This dynamic approach allows you to adapt the visible width of the text area programmatically.

♿ Accessibility

  • Always use labels — Pair every <textarea> with a visible <label> or aria-label.
  • Do not rely on size alone — cols sets visual width; assistive tech does not communicate column count as a constraint.
  • Allow resizing when helpful — CSS resize: vertical lets users expand the field if needed.
  • Test on mobile — Fixed column widths may overflow small screens; use CSS max-width: 100%.
  • Provide maxlength if needed — Use maxlength to limit input, separate from cols visual width.

🧠 How cols Works

1

Author sets cols on textarea

Define the number of visible character columns in HTML.

Markup
2

Browser calculates width

The user agent maps column count to an approximate pixel width.

Layout
3

Text area is rendered

User sees a multiline field with the expected horizontal size.

Display
=

Better multiline input UX

Users get a text area sized appropriately for the expected content.

Browser Support

The cols attribute is supported in all modern browsers on <textarea> elements.

HTML5 · Fully supported

Universal textarea sizing

All major browsers honor cols on textarea for default width.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
cols attribute 99% supported

Bottom line: Use cols confidently on textarea; add CSS for responsive layouts on smaller screens.

💡 Best Practices

✅ Do

  • Define an appropriate width for your text areas based on expected content
  • Combine cols with rows for balanced dimensions
  • Test layout across different screen sizes for responsiveness
  • Add CSS max-width: 100% for mobile-friendly forms
  • Label every textarea clearly for accessibility

❌ Don’t

  • Use cols on non-textarea elements
  • Confuse cols with table colspan
  • Rely on cols alone for strict pixel-perfect layouts
  • Make text areas too narrow for the content you expect
  • Forget to test with real user input and long text
  • Use the cols attribute to define an appropriate width for your text areas, keeping in mind the expected content and overall design of your web page.
  • Test the text area layout across different screen sizes to ensure a responsive and user-friendly experience.
  • Combine the cols attribute with the rows attribute to control both the width and height of your multiline text input fields effectively.

Conclusion

The cols attribute is a useful tool for customizing the visible width of text areas in HTML forms. By understanding and leveraging this attribute, you can create more user-friendly and aesthetically pleasing text input fields in your web applications.

For responsive designs, combine cols with CSS width rules so text areas scale gracefully on phones and tablets while keeping a sensible default on desktop.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about cols

Bookmark these before building your next textarea form.

5
Core concepts
🔢 02

Integer Value

20, 40, 80.

Values
📏 03

Pair with rows

Width + height.

Layout
⚙️ 04

Dynamic JS

.cols = 50

Script
📱 05

Responsive CSS

max-width 100%.

Mobile

❓ Frequently Asked Questions

It sets the visible width of a textarea in average character columns.
<textarea> is the only standard element. Other elements ignore cols.
cols controls horizontal width in columns. rows controls vertical height in lines of text.
No. cols sizes textarea width. colspan merges table cells horizontally.
Yes. Assign textareaElement.cols = 50 or use setAttribute("cols", "50").
Yes, on textarea in all modern browsers. Add CSS for responsive width on small screens.

Build better textarea forms

Practice the cols attribute with width and JavaScript examples in the Try It editor.

Try textarea example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

2 people found this page helpful