391

Every time I develop a new form that includes a textarea I have the following dilemma when I need to specify its dimensions:

Use CSS or use the textarea's attributes cols and rows?

What are the pros and cons of each method?

What are the semantics of using these attributes?

How is it usually done?

alex
  • 479,566
  • 201
  • 878
  • 984
Pherrymason
  • 7,835
  • 8
  • 39
  • 57

13 Answers13

338

I recommend to use both. Rows and cols are required and useful if the client does not support CSS. But as a designer I overwrite them to get exactly the size I wish.

The recommended way to do it is via an external stylesheet e.g.

textarea {
  width: 300px;
  height: 150px;
}
<textarea> </textarea>
tremendows
  • 4,262
  • 3
  • 34
  • 51
kogakure
  • 3,777
  • 1
  • 16
  • 6
  • 4
    This is okay, but you have to realize that whatever arbitrary space you are setting the size with is going to waste and is really just for show. – Explosion Pills Oct 09 '10 at 08:27
  • 5
    @tandu: What do you mean by "is going to waste and is really just for show"? – BoltClock Oct 09 '10 at 08:48
  • 4
    rows/cols is based on the character size of the user. So if you have a css-defined width/height that cannot be divided by the pixles of a character in the textarea, there is going to be that much whitepsace left over vertically and horizontally. Probably no one will ever notice, but just sayin. – Explosion Pills Oct 09 '10 at 08:50
  • 34
    You could use "em" as measurement, instead of pixels. 1em is the width of the "M" in any font and size. But cols are only relevant if used with monospace fonts, which is in most designs not the case. To get a perfect fitting in height use a multiple of `line-height` for the height of your textarea. – kogakure Oct 09 '10 at 09:07
  • 1
    -1 The `rows` and `cols` attributes are not required for the `textarea` element to pass HTML5 validation, nor are they listed as required by W3C. – Leo Galleguillos Jan 06 '16 at 05:28
  • 7
    @LeoGalleguillos “Required and useful _if the client does not support CSS_”. Nobody said anything about validation. – Janus Bahs Jacquet Aug 13 '16 at 16:03
  • 1
    This solution is highly questionable. Using px units in CSS and also relying on rows (which will depend on the actual font-face used, which is not 100% controllable, since you don't know what browser/OS the user has) will almost certainly cause mismatches. I am extremely uncomfortable with too many upvotes on answers like this. I want to give it -167! – Octopus Jul 20 '17 at 16:53
  • It says on MDN that the default value of cols is 20 (-1) – Marvin Jun 02 '19 at 16:17
141

textarea { height: auto; }
<textarea rows="10"></textarea>

This will trigger the browser to set the height of the textarea EXACTLY to the amount of rows plus the paddings around it. Setting the CSS height to an exact amount of pixels leaves arbitrary whitespaces.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Jan Werkhoven
  • 2,656
  • 1
  • 22
  • 31
  • 4
    Unfortunatelly, "Height of textarea does not match the rows in Firefox" - it's "rows+1" http://stackoverflow.com/q/7695945/1266880 – apurkrt Mar 21 '15 at 21:09
  • 6
    Not sure if things have changed since '13 or whether there's something super non-obvious about my particular situation, but setting `height: auto;` in CSS doesn't seem to affect my textarea at all. – clozach Apr 01 '17 at 03:44
  • To counterbalance the other comments saying that this doesn't work, this answer solved my issue with using `rows` for a `textarea` within [bootstrap's `form-floating` CSS class](https://getbootstrap.com/docs/5.2/forms/floating-labels/#textareas). – somethingRandom Aug 17 '22 at 10:37
12

According to the w3c, cols and rows are both required attributes for textareas. Rows and Cols are the number of characters that are going to fit in the textarea rather than pixels or some other potentially arbitrary value. Go with the rows/cols.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 9
    w3c schools states that you can set it through the rows and cols attribute but that setting height and width is better. There is no mention of rows and cols being a required attribute for textarea on w3c.org. http://www.w3schools.com/tags/tag_textarea.asp http://www.w3.org/wiki/HTML/Elements/textarea – Michael Stramel Aug 13 '14 at 12:53
  • 20
    @MichaelStramel my answer is four years old :) -- by the way it's *not* w3c schools. They are not affiliated. I'm not sure that HTML5 has a concept of *required* attributes anymore, but I could be wrong about that. I still don't see why setting width/height would be better than rows/cols though – Explosion Pills Aug 13 '14 at 13:51
  • 3
    I realized that after I commented but still felt it was relevant for anyone reading this post. Also, with responsive designs, the rows and cols can cause issues unless they are set through JavaScript. That IMO makes setting it through CSS a much better way. – Michael Stramel Aug 13 '14 at 14:06
10

For text area we can use below css to fix size

    <textarea  class="form-control" style=" min-width:500px; max-width:100%;min-height:50px;height:100%;width:100%;" ></textarea>

Tested in angularjs and angular7

Rohit Parte
  • 3,365
  • 26
  • 26
7

The answer is "yes". That is, you should use both. Without rows and cols (and there are default values even if you don't use them explicitly) the textarea is unusably small if CSS is disabled or overriden by a user stylesheet. Always keep accessibility concerns in mind. That being said, if your stylesheet is allowed to control the appearance of the textarea, you will generally wind up with something that looks a whole lot better, fits into the overall page design well, and that can resize to keep up with user input (within the limits of good taste, of course).

Stan Rogers
  • 2,180
  • 11
  • 9
7
 <textarea style="width:300px; height:150px;" ></textarea>
Rahul
  • 1,181
  • 1
  • 11
  • 20
user470962
  • 155
  • 1
  • 20
    @user470962 Ad hominem. If you correct the syntax error to ' – TAAPSogeking Oct 08 '13 at 02:37
  • 1
    @TAAPSogeking in fairness, this is the same answer, 4 years later. -1 – Rambatino Oct 29 '16 at 11:24
5

HTML rows and cols are not responsive!

So I define the size in CSS. As a tip: if you define a small size for mobiles think about using textarea:focus {};

Add some extra space here, which will only unfold the moment a user wants to actually write something

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
someone
  • 53
  • 1
  • 4
  • using the :focus pseudo class is very clever! How come I didn't think of that. Combine with transition-duration! `````` – cmarangu Apr 10 '22 at 21:00
4

The size of a textarea can be specified by the cols and rows attributes, or even better; through CSS' height and width properties. The cols attribute is supported in all major browsers. One main difference is that <TEXTAREA ...> is a container tag: it has a start tag ().

knittl
  • 246,190
  • 53
  • 318
  • 364
AsifQadri
  • 2,388
  • 1
  • 20
  • 30
3

A major feature of textareas is that they are expandable. On a web page this can result in scroll bars appearing on the text area if the text length overfills the space you set (be that using rows, or be that using CSS. That can be a problem when a user decides to print, particularly with 'printing' to PDF - so set a comfortably large min-height for printed textareas with a conditional CSS rule:

@media print { 
textarea {
min-height: 900px;  
}
}
Geoff Kendall
  • 1,307
  • 12
  • 13
3

if you dont use every time use line-height:'..'; property its control the height of textarea and width property for width of textarea.

or you can make use of font-size by following css:

#sbr {
  font-size: 16px;
  line-height:1.4;
  width:100%;
}
Nurealam Sabbir
  • 365
  • 3
  • 4
3

I usually don't specify height, but do specify width: ... and rows and cols.

Usually, in my cases, only width and rows are needed, for the textarea to look nice in relation to other elems. (And cols is a fallback if someone doesn't use CSS, as explained in the other answers.)

((Specifying both rows and height feels a little bit like duplicating data I think?))

KajMagnus
  • 11,308
  • 15
  • 79
  • 127
1

YES!....always style textarea using CSS and avoid the attributes, unless you need to support some very old agent that does not support style sheets. Otherwise, you have full power to use CSS. Below is my default CSS formatting for textarea that looks beautiful in any website. Customize it as you like. Comments are included below so you can see why I chose those CSS properties and values:

textarea {
    display: inline-block;
    margin: 0;
    padding: .2em;
    width: auto;
    min-width: 30em;
    /* The max-width "100%" value fixes a weird issue where width is too wide by default and extends beyond 100% of the parent in some agents. */
    max-width: 100%;
    /* Height "auto" will allow the text area to expand vertically in size with a horizontal scrollbar if pre-existing content is added to the box before rendering. Remove this if you want a pre-set height. Use "em" to match the font size set in the website. */
    height: auto;
    /* Use "em" to define the height based on the text size set in your website and the text rows in the box, not a static pixel value. */
    min-height: 10em;
    /* Do not use "border" in textareas unless you want to remove the 3D box most browsers assign and flatten the box design. */
    /*border: 1px solid black;*/
    cursor: text;
    /* Some textareas have a light gray background by default anyway. */
    background-color: #eee;
    /* Overflow "auto" allows the box to start with no scrollbars but add them as content fills the box. */
    overflow: auto;
    /* Resize creates a tab in the lower right corner of textarea for most modern browsers and allows users to resize the box manually. Note: Resize isn't supported by most older agents and IE. */
    resize: both;
}

In my "reset" element style sheet I set these values as defaults for "textarea" by default, which give all your textareas a nice look and feel with scrolling when detected, a resizing tab (non-IE browsers), and fixes for dimensions, including a height that allows the box to size itself based on existing content you put in it for the user and a width that does not break out beyond its parent containers limitations.

Stokely
  • 12,444
  • 2
  • 35
  • 23
-9

CSS


input
{
    width: 300px;
    height: 40px;
} 

HTML


<textarea rows="4" cols="50">HELLO</textarea>
Community
  • 1
  • 1
ASHU
  • 94
  • 7