Open In App

HTML cols Attribute

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The cols attribute in HTML is used to tell the browser how many average-width characters should fit on a single line i.e. the number of columns to display.

Note: This attribute is only applicable on <textarea> and <frameset>.

Syntax: 

<textarea cols = "value">Text content...</textarea>

Attribute Values: It has a default value which is 20. It contains a numeric value that represents the width of the text area element. 

Example: In this example, we use the cols attribute in a <textarea> to set its width to fit 25 characters per line, with a predefined height of 4 rows.

<!DOCTYPE html>
<html>

<head>
    <title>HTML cols Attribute</title>
    <style>
        h1,
        h2 {
            text-align: center;
        }
    </style>
</head>

<body>
    <center>
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>

        <h2>
            HTML cols Attribute
        </h2>

        <!-- Below textarea is assigned a cols value 25 
            That is, 25 characters will fit in a line -->
        <textarea rows="4" cols="25">
            A computer science portal for geeks.
        </textarea>
    </center>
</body>

</html>

Output: 

cols

Supported Browsers: The browser supported by cols attribute are listed below: 

HTML cols Attribute – FAQs

What is the default value of the cols attribute in HTML?

The default value of the cols attribute is typically 20 characters if not specified, but this can vary depending on the browser.

Is the cols attribute required for a <textarea> element?

No, the cols attribute is not required, but it is recommended to specify it or use CSS to define the width of the <textarea> for consistent display across browsers.

Can the cols attribute be used with any HTML element?

No, the cols attribute is specifically used with the <textarea> element to define its width.

How do I set both the width and height of a <textarea> in HTML?

You can set the width using the cols attribute and the height using the rows attribute, like this: <textarea cols=”40″ rows=”10″></textarea>.

Can I use CSS instead of the cols attribute to set the width of a <textarea>?

Yes, you can use CSS to set the width of a <textarea> with the width property, like this: <textarea style=”width: 300px;”></textarea>. Using CSS provides more flexibility and control over styling.


Next Article

Similar Reads

three90RightbarBannerImg