7

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

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Would absolute positioning on the textarea work for you? `textarea { position:absolute; }` seems to do the trick – j08691 May 03 '18 at 17:35

4 Answers4

2

This will work. or just add display:block to textarea in your fiddle.

html, body {
  margin: 0;
  padding: 0;
  border: 0;
}
* {
box-sizing: border-box;
}

textarea {
  width: 100%;
  height: 100vh;
  display: block;
 }
<textarea placeholder="message"></textarea>
patelarpan
  • 7,188
  • 2
  • 20
  • 25
2

The issue is the common white space issue of inline-block/inline element due to vertical alignment. If you check dev tools of google you will see this:

enter image description here

So to fix it you simply need to adjust vertical alignment or make the textarea a block element (like provided in the other answers):

html, body, textarea {
  margin: 0;
  padding: 0;
  border: 0;
  width: 100%;
  height: 100%;
}
textarea {
 vertical-align:top;
}
<textarea>Text goes here</textarea>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

html, body {
  margin: 0;
  padding: 0;
  border: 0;
  width: 100%;
  height: 100%;
}
textarea {
  margin: 0;
  padding: 0;
  border: 0;
  width: 100%;
  height: 100%;
  display:block;
  resize:none;/*Add this if you dont want users to resize */
}
<textarea>Text goes here</textarea>
Gautam Naik
  • 8,990
  • 3
  • 27
  • 42
-1

Just remove width and height for html and body tag.

Thank me later

kanny im
  • 1
  • 3