search

Home  >  Q&A  >  body text

How can I resize the text area so that it fits the content width and set the minimum and maximum width?

<p>So, I'm having trouble now with the text area, I can't resize it to the content width... I found a long and detailed answer about content height and it works! But there is no content width above...</p> <p>This is that article: Creating a text area with automatic resizing</p> <p>However, my problem is not solved, I need to adjust the width too. Consider maximum and minimum widths. </p> <p>This is sample code:</p> <p> <pre class="brush:js;toolbar:false;">$("textarea").each(function () { this.setAttribute("style", "height:" (this.scrollHeight) "px;"); }).on("input", function () { this.style.height = 0; this.style.height = (this.scrollHeight) "px"; }); $("textarea").trigger("input");</pre> <pre class="brush:css;toolbar:false;">textarea{ border: 1px solid black; min-width: 50px; max-width: 300px; width: auto; }</pre> <pre class="brush:html;toolbar:false;"><script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.3.min.js">< ;/script> <textarea>this is my normal content, and it has to consider this content to be as long as the max-width</textarea> <textarea>this is my normal content, should be considered</textarea> <textarea>this is my min content</textarea></pre> </p> <p>Also, this is what I want them to be:</p>
P粉277824378P粉277824378532 days ago639

reply all(1)I'll reply

  • P粉322918729

    P粉3229187292023-09-06 10:02:16

    Is it similar to this?

    Note: As mentioned in the comments, this technique uses the contenteditable attribute

    #myInput {
      min-height: 20px;
      max-height: 100px;
      overflow: auto;
      border: 1px solid #000;
      border-radius: 4px;
      max-width: 80%;
      min-width: 40px;
      display: inline-block;
      padding: 7px;
      resize: vertical;
    }
    <div contenteditable id="myInput"></div>

    reply
    0
  • Cancelreply