[英]How to increase height of textarea based font size
I have a textarea,where user will input some text,and also have a input range,where user can increase the font size of text,I want that the width of textarea still same,but height increase depend font size to show all text,and also I want textarea don't have a scroll and resize I did that using div,and I want the same result using textarea我有一个 textarea,用户将在其中输入一些文本,还有一个输入范围,用户可以在其中增加文本的字体大小,我希望 textarea 的宽度仍然相同,但高度增加取决于字体大小以显示所有文本,而且我希望 textarea 没有滚动和调整大小,我使用 div 做到了这一点,并且我希望使用 textarea 获得相同的结果
I did that using div https://jsfiddle.net/Meline222/fgpedL0z/1/我使用 div https://jsfiddle.net/Meline222/fgpedL0z/1/
I want the same result but using textarea https://jsfiddle.net/Meline222/fgpedL0z/3/ bt when i use textarea all text can't see我想要相同的结果,但使用 textarea https://jsfiddle.net/Meline222/fgpedL0z/3/ bt 当我使用 textarea 时,所有文本都看不到
I tried did but all text don't show textarea我试过了,但所有文本都不显示 textarea
this work for div这项工作为 div
#inputText {
width: 150px;
height: auto;
border: 1px solid;
word-wrap: break-word;
overflow:hidden;
resize: none;
}
If you want only the height to be adjusted along font size, use em
unit measure.如果您只想沿字体大小调整高度,请使用em
单位度量。
width: 150px;
height: 2em;
Documentation about the em
unit says关于em
单位的文档说
em: 1em is the same as the font-size of the current element. em: 1em 与当前元素的字体大小相同。
JSFiddle Demo: https://jsfiddle.net/craigiswayne/qkn8rdxp/20/ JSFiddle 演示: https ://jsfiddle.net/craigiswayne/qkn8rdxp/20/
function adjustSize(i){ var o = document.getElementById('inputText'); o.setAttribute("style","height: auto;"); var val = i.value; o.style.fontSize = val + 'px'; var fontSize = o.style.fontSize; o.setAttribute("style","font-size: " + fontSize + "; height: " + o.scrollHeight + "px;"); }
#inputText { width: 150px; height: auto; border: 1px solid; word-wrap: break-word; }
<textarea name="" id="inputText">kyb u uuhhhkjh kj</textarea> <input id="input" type="range" min="12" max="72" oninput="adjustSize(this);">
So the solution I chose to set the height
of the textarea
to it's scrollHeight
所以我选择将textarea
的height
设置为它的scrollHeight
的解决方案
scrollHeight
is defined by MDN docs as scrollHeight
由 MDN 文档定义为
The scrollHeight value is equal to the minimum height the element would require in order to fit all the content in the viewport without using a vertical scrollbar scrollHeight 值等于元素需要的最小高度,以便在不使用垂直滚动条的情况下适应视口中的所有内容
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight
see also this post for an explanation of scrollHeight
, clientHeight
and offsetHeight
有关scrollHeight
、 clientHeight
和offsetHeight
的解释, scrollHeight
见这篇文章
What is offsetHeight, clientHeight, scrollHeight? 什么是 offsetHeight、clientHeight、scrollHeight?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.