How can I make a <textarea> that fits within the width of the current viewport?
Asked Answered
N

6

19

How can I make it so that my <textarea> doesn't go over the border of small displays? I want that my <textarea> gets the width of the current display.

<textarea></textarea>
Natalienatalina answered 21/8, 2016 at 19:32 Comment(0)
I
42

You need to change its default box-sizing to a different value.

textarea {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;

    width: 100%;
}
Isacco answered 21/8, 2016 at 19:38 Comment(0)
M
12

Set a max-width on the element.

textarea {
  max-width: 100%;
}
<textarea></textarea>
Mariannamarianne answered 21/8, 2016 at 19:35 Comment(0)
Z
7

I agree with all of the above answers but one issue not addressed is that if you don't specify the number of columns the textarea will not actually fill the width of the div or element. So I use the following:

<textarea cols="120" style="max-width:100%;">TEXT GOES HERE</textarea>

I set the number of columns to slightly greater that the width of the div on a large screen and as the screen gets smaller it acts responsive to the screen size.

Ziegler answered 8/7, 2019 at 21:27 Comment(1)
I like this comment, it make sense and seems to be the easiest solutionHarms
E
6

Try textarea {max-width:95%;} - it will always fit your display.

Economy answered 21/8, 2016 at 19:36 Comment(0)
H
3

you can disable text area resize by:

textarea {
   resize: none;
}

and set max-width to the width of the container div or table

Hodosh answered 21/8, 2016 at 19:37 Comment(1)
Could you edit your answer and go into a bit more detail about how to set the max width. (also, I accidentally down voted your answer and once you edit it I can give it the up vote it deserves.) Thanks!Tuatara
W
1

You can do the following:

div textarea {
  box-sizing: border-box;
  max-width: 50%;
}
<div>
  <textarea cols="100" rows="7"></textarea>
</div>

Setting both max-width and box-sizing ensures the textarea respects its defined width even when the number of cols exceeds the set max-width.

Wil answered 8/11, 2019 at 3:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.