更改文本区域的大小 [英] Change size of textarea
问题描述
我目前有一个像这样的 textarea
:
I currently have a textarea
like this:
<textarea matInput rows="5" cols="40" placeholder="text"></textarea>
然而,它总是相同的大小.
However, it is always the same size.
知道如何改变 textarea
的大小吗?
Any idea on how to change the size of the textarea
?
推荐答案
取决于您的意思:
然而,它总是相同的大小.
However, it is always the same size.
有两种选择.
目前,只有 rows
影响材质 textarea
高度,cols
不会改变其宽度.
Currently, only rows
affects the Material textarea
height, cols
doesn't change its width.
因此,为了增加宽度,我们必须在包含我们的 textarea
的 mat-form-field
上使用 CSS 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>
选项 2(适合 textarea
内容的动态大小):
在材料 6 中,添加了 CdkTextareaAutosize
指令.
OPTION 2 (DYNAMIC size to fit textarea
content):
In Material 6, CdkTextareaAutosize
directive was added.
来自官方文档:
cdkTextareaAutosize 指令可以应用于任何 使其自动调整大小以适合其内容.最低和可以通过设置扩展到的最大行数
cdkAutosizeMinRows
和 cdkAutosizeMaxRows
属性分别.
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>
注意:
其他答案中提到的 matTextareaAutosize
已弃用,将在下一个主要版本中删除.官方文档 已经使用 cdkTextareaAutosize
代替.
NOTE:
matTextareaAutosize
mentioned in other answers is deprecated and will be removed in the next major release. The official docs already use cdkTextareaAutosize
instead.
这篇关于更改文本区域的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!