英文:
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 -->
<div class="outerdiv">
<textarea></textarea>
</div>
<!-- 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("textarea");
// Get line height of the textarea
var lht = parseInt($('textarea').css('lineHeight'),10);
// Get lines
var scrlht = textarea.scrollHeight;
var lines = Math.floor(scrlht / lht);
if (lines < 4) {
textarea.style.height = '15px'
textarea.style.height = (lines * lht) + 'px'
}
}
<!-- language: lang-css -->
textarea {
line-height: 15px;
height: 15px;
}
<!-- language: lang-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>
<!-- 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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论