I currently have a textarea
like this:
<textarea matInput rows="5" cols="40" placeholder="text"></textarea>
However, it is always the same size.
Any idea on how to change the size of the textarea
?
The size of a text area is specified by the cols and rows attributes (or with CSS). The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the text area will be submitted). The id attribute is needed to associate the text area with a label.
You can resize a textarea by clicking on the bottom right corner of the textarea and dragging the mouse.
Depending on what you've meant by:
However, it is always the same size.
There are two options.
rows
\ cols
):Currently, only rows
affects the Material textarea
height, cols
doesn't change its width.
Therefore for increasing width, we have to use the CSS width
property on a mat-form-field
containing our textarea
:
<mat-form-field style="width: 300px;"> <textarea matInput rows="5" cols="40" placeholder="text"></textarea> </mat-form-field>
textarea
content):In Material 6, CdkTextareaAutosize
directive was added.
From an official docs:
The cdkTextareaAutosize directive can be applied to any
<textarea>
to make it automatically resize to fit its content. The minimum and maximum number of rows to expand to can be set via thecdkAutosizeMinRows
andcdkAutosizeMaxRows
properties respectively.
And here's a simplified example from there:
<mat-form-field> <mat-label>Autosize textarea</mat-label> <textarea matInput cdkTextareaAutosize cdkAutosizeMinRows="1" cdkAutosizeMaxRows="5"> </textarea> </mat-form-field>
NOTE:matTextareaAutosize
mentioned in other answers is deprecated and will be removed in the next major release. The official docs already use cdkTextareaAutosize
instead.
Here's an example :
<mat-form-field> <mat-label>Description</mat-label> <textarea matInput formControlName="description" matTextareaAutosize matAutosizeMinRows=1 matAutosizeMaxRows=5></textarea> </mat-form-field>
Reference: https://material.angular.io/components/input/api
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With