首页 > 解决方案 > How to set textarea to 100% width and height?

问题描述

How can i set a <textarea> to consume 100% width and height of the browser window?

For example, the following does not work:

html, body, textarea {
  margin: 0;
  padding: 0;
  border: 0;
  width: 100%;
  height: 100%;
}
<textarea>Text goes here</textarea>

jsFiddle

Because it consumes slightly over 100% of the window, causing a scrollbar to appear:

enter image description here

How do i make a <textarea> consume 100% of the space?

Bonus Reading

标签: htmlcsstextarea

解决方案


问题是由于垂直对齐导致的/元素的常见空白问题。如果你检查谷歌的开发工具,你会看到:inline-blockinline

在此处输入图像描述

因此,要修复它,您只需调整垂直对齐或使 textarea 成为块元素(如其他答案中提供的那样):

html, body, textarea {
  margin: 0;
  padding: 0;
  border: 0;
  width: 100%;
  height: 100%;
}
textarea {
 vertical-align:top;
}
<textarea>Text goes here</textarea>


推荐阅读