每次我开发一个包含文本区域的新表单时,当我需要指定它的尺寸时,我都有以下困境:

使用CSS或使用文本区域的属性cols和行?

每种方法的优缺点是什么?

使用这些属性的语义是什么?

通常是怎么做的?


当前回答

我通常不指定高度,但会指定宽度:……还有一排排和cols。

通常,在我的情况下,只需要宽度和行,textarea看起来很好,相对于其他elems。(如果有人不使用CSS, cols是一个备用方案,正如在其他答案中解释的那样。)

((同时指定行和高度感觉有点像复制数据吗?))

其他回答

文本区域 { 高度: 自动; } <行的文本=“10”></textarea>

这将触发浏览器将textarea的高度EXACTLY设置为行数加上它周围的填充。将CSS高度设置为精确的像素数量会留下任意的空白。

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).

HTML行和cols没有响应!

我用CSS定义了大小。提示:如果你为手机定义了一个小尺寸,可以考虑使用textarea:focus {};

在这里添加一些额外的空间,它只会在用户真正想要写一些东西的时候展开

根据w3c, cols和rows都是textarea的必需属性。行和Cols是将适合文本区域的字符数,而不是像素或其他潜在的任意值。顺着row /cols走。

如果你不是每次都使用line-height:'..';属性控制textarea的高度,width属性控制textarea的宽度。

或者你可以通过下面的css来使用font-size:

#sbr {
  font-size: 16px;
  line-height:1.4;
  width:100%;
}