👀 Live Preview
Textarea with cols="40" and rows="5":

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.
40, 50, 80.
Where cols applies.
Width and height.
Responsive option.
Dynamic cols.
Better text areas.
colsThe 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 controls visible width in character columns. rows controls visible height in text lines. Use both together for a balanced textarea size.
Add cols to a <textarea> element with a positive integer:
<textarea cols="40" rows="5">Enter text here...</textarea><textarea> elements.20, 40, 80).rows to control both dimensions of the text area.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.<textarea cols="20" rows="3">Short comment</textarea>
<textarea cols="40" rows="5">Standard message</textarea>
<textarea cols="80" rows="10">Long content</textarea>| Use Case | cols Value | Notes |
|---|---|---|
| Short comment | cols="20" | Compact field |
| Contact message | cols="40" | Common default |
| Article draft | cols="80" | Wide writing area |
| Height control | rows="5" | Pair with cols |
| Applicable element | <textarea> | Only textarea |
| Responsive width | CSS width: 100% | Combine with max-width |
| Element | Supported? | Notes |
|---|---|---|
<textarea> | Yes | Primary and only standard use |
<input> | No | Use CSS width on single-line inputs |
<table>, <col> | No | Use colspan for table columns |
<div> | No | cols is not a global width attribute |
Basic textarea width with cols and dynamic JavaScript adjustment.
Textarea with cols="40" and rows="5":
Let’s look at a simple example of how to use the cols attribute:
<textarea cols="40" rows="5">Enter text here...</textarea>In this example, the cols attribute is set to 40, indicating that the text area should be initially displayed with 40 visible columns.
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:
<textarea id="dynamicTextArea" rows="4"></textarea>
<script>
// Dynamically set cols for a text area
document.getElementById("dynamicTextArea").cols = 50;
</script>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.
<textarea> with a visible <label> or aria-label.resize: vertical lets users expand the field if needed.max-width: 100%.maxlength to limit input, separate from cols visual width.Define the number of visible character columns in HTML.
The user agent maps column count to an approximate pixel width.
User sees a multiline field with the expected horizontal size.
Users get a text area sized appropriately for the expected content.
The cols attribute is supported in all modern browsers on <textarea> elements.
All major browsers honor cols on textarea for default width.
Bottom line: Use cols confidently on textarea; add CSS for responsive layouts on smaller screens.
cols with rows for balanced dimensionsmax-width: 100% for mobile-friendly formscols on non-textarea elementscols with table colspancols attribute to define an appropriate width for your text areas, keeping in mind the expected content and overall design of your web page.cols attribute with the rows attribute to control both the width and height of your multiline text input fields effectively.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.
colsBookmark these before building your next textarea form.
Character columns.
Scope20, 40, 80.
ValuesWidth + height.
Layout.cols = 50
Scriptmax-width 100%.
Mobile<textarea> is the only standard element. Other elements ignore cols.cols controls horizontal width in columns. rows controls vertical height in lines of text.cols sizes textarea width. colspan merges table cells horizontally.textareaElement.cols = 50 or use setAttribute("cols", "50").Practice the cols attribute with width and JavaScript examples in the Try It editor.
2 people found this page helpful