How can I resize the text area so that it fits the content width and set the minimum and maximum width?
P粉277824378
2023-09-05 11:21:30
<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>
Is it similar to this?
Note: As mentioned in the comments, this technique uses the
contenteditable
attribute