假设我有以下内容
div{
padding: 20px;
}
<div style="display:flex; background: gold; flex-direction: column;">
<textarea>Do NOT expand this</textarea>
<textarea class="expand">Expand this baby</textarea>
</div>
我希望当文本区域“expand”上的内容创建垂直溢出以扩展父 div,不创建垂直滚动。
最佳答案
我认为这不能仅用 CSS 来完成,并且由于您不需要 jQuery,所以这里是一个纯 JS 解决方案。这个想法是在每次更新内容时计算文本区域的高度(调整大小)
var tex = document.querySelector('textarea.expand');
tex.addEventListener('keydown', resize);
function resize() {
setTimeout(function() {
tex.style.height = 'auto'; //needed when you remove content so we reduce the height
tex.style.height = tex.scrollHeight + 'px';
}, 0);
}
div {
padding: 20px;
}
<div style="display:flex; background: gold; flex-direction: column;">
<textarea>Do NOT expand this</textarea>
<textarea class="expand">Expand this baby</textarea>
</div>
关于html - css Flex div 随着子文本区域的增长而增长(无jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971723/