如何使文本区域自动扩展到最大高度?

huangapple go评论72阅读模式
英文:

How can I make a textarea expand automatically to a maximum height?

问题

Sure, here's the translated CSS code:

.outerdiv {
  height: auto;
  display: flex;
}

.outerdiv textarea {
  height: 100%;
  flex-grow: 1;
}

Please note that code translation is provided as requested, without any additional information or responses.

英文:

I am learning CSS. I have a div which contains a flexible textarea. Now I want the textarea and outer div to auto expand height when the user inputs multiline content in the textarea, and when the textarea is more than 3 rows, stop the expand and scroll the textarea. How should I write the CSS?

Rendered Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.outerdiv {
  height: auto;
  display: flex;
}

.outerdiv textarea {
  height: 100%;
  flex-grow: 1;
}

<!-- language: lang-html -->

&lt;div class=&quot;outerdiv&quot;&gt;
  &lt;textarea&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

我不认为有一种 CSS 解决方案。

然而,这段 JavaScript 代码实现了你想要的功能:

function updateTextarea() {
   var textarea = document.getElementById("textarea");
    // 获取文本区域的行高
   var lht = parseInt($('textarea').css('lineHeight'), 10);
    // 获取行数
   var scrlht = textarea.scrollHeight;
   var lines = Math.floor(scrlht / lht);
   
   if (lines < 4) {
    textarea.style.height = '15px';
    textarea.style.height = (lines * lht) + 'px';
   }
}

CSS 部分如下:

textarea {
line-height: 15px;
height: 15px;
}

HTML 部分如下:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <textarea id='textarea' oninput="updateTextarea()"></textarea>
</div>

它在删除文本时不会使文本区域变小,但在输入时会增长。

英文:

I don't think there is a css solution.

However this JS kinda does what you want:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function updateTextarea() {
   var textarea = document.getElementById(&quot;textarea&quot;);
    // Get line height of the textarea
   var lht = parseInt($(&#39;textarea&#39;).css(&#39;lineHeight&#39;),10);
    // Get lines 
   var scrlht = textarea.scrollHeight;
   var lines = Math.floor(scrlht / lht);
   
   if (lines &lt; 4) {
    textarea.style.height = &#39;15px&#39;
    textarea.style.height = (lines * lht) + &#39;px&#39;
   }
 }

<!-- language: lang-css -->

textarea {
line-height: 15px;
height: 15px;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div&gt;
  &lt;textarea id=&#39;textarea&#39; oninput=&quot;updateTextarea()&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

It does not make the texarea smaller when removing text. But it does grow when you type.

答案2

得分: 2

You could adapt this absolute gem of an answer as follows:

<!-- 开始代码段: 不隐藏,显示控制台,不使用Babel编译 -->

<!-- 语言: CSS -->

textarea {
  height: 1rem;
}

<!-- 语言: HTML -->

<textarea 
  data-max-height="48" 
  name="text" 
  oninput='this.style.height = "";this.style.height = Math.min(this.scrollHeight, this.getAttribute("data-max-height")) + "px"'
>
</textarea>

<!-- 结束代码段 -->

[1]: https://stackoverflow.com/a/48460773/12571484
英文:

You could adapt this absolute gem of an answer as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

textarea {
  height: 1rem;
}

<!-- language: lang-html -->

&lt;textarea 
  data-max-height = &quot;48&quot; 
  name=&quot;text&quot; 
  oninput=&#39;this.style.height = &quot;&quot;;this.style.height = Math.min(this.scrollHeight, this.getAttribute(&quot;data-max-height&quot;)) + &quot;px&quot;&#39;
  &gt;
&lt;/textarea&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 22:33:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定