583

I have this text input in a form:

<input type="text"
       cols="40" 
       rows="5" 
       style="width:200px; height:50px;" 
       name="Text1" 
       id="Text1" 
       value="" />

I am trying to get it to take multiple lines of input. The width and height make the box to be bigger, but the user can enter text all (s)he wants yet it fills one line only.

How do I make the input more like a textarea?

0

10 Answers 10

935

You need to use a textarea to get multiline handling.

<textarea name="Text1" cols="40" rows="5"></textarea>

5
  • 11
    Yes, but, textarea doesn't support the pattern attribute. So darn.
    – toddmo
    Commented Oct 12, 2015 at 19:17
  • 1
    hmmmm, what. Im looking for multiple insert for type text. why this people ask this ques. Commented Dec 23, 2016 at 3:41
  • @AlexH your comment is very important and helpful indeed, thanks. Commented Jan 24, 2017 at 13:22
  • 2
    what I don't like about this is that in JQuery, you can't set textarea value using val(). You have to append to it. :( Commented Jul 28, 2017 at 1:25
  • @AlexH: You could remove your comment and add it again if you want to alter anything. Commented Nov 28, 2018 at 13:59
79

You can't. At the time of writing, the only HTML form element that's designed to be multi-line is <textarea>.

0
68

It is possible to make a text-input multi-line by giving it the word-break: break-word; attribute. (Only tested this in Chrome)

3
  • 1
    Thanks! I noticed that Chrome was allowing multiple lines for inputs, which I totally didn't want to, and the reason was word-break which was inherited from the body element
    – rap1ds
    Commented Nov 12, 2014 at 14:12
  • 7
    Looks good in Chrome 39 and Safari 8.0.2, but not Firefox 34 in my brief testing. :( jsfiddle.net/msybs9g7 Commented Dec 18, 2014 at 22:47
  • 29
    This solution is outdated and not supported in most browsers anymore. Use <textarea> instead
    – Dror Bar
    Commented Jul 6, 2020 at 13:54
63

Use the textarea

<textarea name="textarea" style="width:250px;height:150px;"></textarea>

don't leave any space between the opening and closing tags Or Else This will leave some empty lines or spaces.

1
  • Thanks, I was wondering if the height would work instead of rows.
    – ideaztech
    Commented Jan 10, 2022 at 15:11
45

If you need Multiline Textarea with Auto Height Increase feature, You can use following simple javascript,

function auto_height(elem) {  /* javascript */
    elem.style.height = '1px';
    elem.style.height = `${elem.scrollHeight}px`;
}
.auto_height { /* CSS */
  width: 100%;
}
<textarea rows="1" class="auto_height" onInput="auto_height(this)"></textarea>

8
  • 1
    Thanks. I modified this and used it in my react textarea
    – Cuado
    Commented Oct 8, 2021 at 10:46
  • 2
    P.S you want to add overflow: hidden to the textarea, or else it will add the extra height few letters before the actual needed one.
    – Art3mix
    Commented Jan 26, 2022 at 17:35
  • 2
    For me this caused the textarea to slightly increase in height when you start typing. A workaround is to set padding to 0 and restore it after setting the height: codepen.io/mrexodia/pen/LYdrBqM
    – mrexodia
    Commented Aug 8, 2022 at 20:37
  • Probably nered a ResizeObserver too for if the window is size changed Commented Mar 5, 2023 at 2:51
  • 1
    in react.js I applied it and it worked.
    – KD.S.T.
    Commented Aug 25, 2023 at 19:21
21

Check this:

The TEXTAREA element creates a multi-line text input control

15

You should use textarea to support multiple-line inputs.

<textarea rows="4" cols="50">
Here you can write some text to display in the textarea as the default text
</textarea>
9

Use <div contenteditable="true"> (supported well) with storing to <input type="hidden">.

HTML:

<div id="multilineinput" contenteditable="true"></div>
<input type="hidden" id="detailsfield" name="detailsfield">

jQuery:

$("#multilineinput").on('keyup',function(e) {   
    $("#detailsfield").val($(this).text()); //store content to input[type=hidden]
});
//optional - one line but wrap it
$("#multilineinput").on('keypress',function(e) {    
    if(e.which == 13) { //on enter
        e.preventDefault(); //disallow newlines     
        // here comes your code to submit
    }
});
-7

If you're using React, you could use the 3rd-party UI library MUI. It has a custom proprietary <Input> element with a multiline option.

As LSE commented, ultimately it gets rendered as <textarea>.

  <FormControl>
    <InputLabel htmlFor="textContract">{`textContract`}</InputLabel>
    <Input
      id="textContract"
      multiline
      rows="30"
      type="text"
      value={props.textContract}
      onChange={() => {}}
    />
  </FormControl>

https://material-ui.com/components/text-fields/#multiline

1
  • 2
    From the documentation : The multiline prop transforms the text field into a <textarea> element.. So even though it looks like an <input>, it's rendering a <textarea>.
    – LSE
    Commented Nov 11, 2021 at 1:05
-11

If you don't need to edit it, you can use a

<span>text here</span>

It will expand with the text.

1
  • 1
    This would remove the input element completely; doesn't help at all.
    – Kalnode
    Commented Feb 13, 2022 at 15:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.