Is there a way to get a textarea to stretch to fit its content without using PHP or JavaScript?

后端 未结 5 1178
再見小時候
再見小時候 2020-12-08 03:25

I am filling a textarea with content for the user to edit.

Is it possible to make it stretch to fit content with CSS (like overflow:show for a div)?

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 03:59

    Here is a function that works with jQuery (for height only, not width):

    function setHeight(jq_in){
        jq_in.each(function(index, elem){
            // This line will work with pure Javascript (taken from NicB's answer):
            elem.style.height = elem.scrollHeight+'px'; 
        });
    }
    setHeight($(''));
    

    Note: The op asked for a solution that does not use Javascript, however this should be helpful to many people who come across this question.

提交回复
热议问题