CSS Textarea That Expands as You Type Text

CSS Textarea that expands as you type text

This cannot be done with CSS alone. try the autogrow jquery plugin.
https://github.com/jaz303/jquery-grab-bag/blob/master/javascripts/jquery.autogrow-textarea.js

You can also see autogrow demo here http://onehackoranother.com/projects/jquery/jquery-grab-bag/autogrow-textarea.html

It's lightweight and easy to use. Here's how it's done. Define your textarea id. Include the jquery js file before </body>. Then between script tags, issue the jquery command $("#txtInput").autoGrow();

<body>
<textarea id="txtInput"></textarea>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>

<script>
$("#txtInput").autogrow();
</script>
</body>

Creating a textarea with auto-resize

This works for me (Firefox 3.6/4.0 and Chrome 10/11):

var observe;if (window.attachEvent) {    observe = function (element, event, handler) {        element.attachEvent('on'+event, handler);    };}else {    observe = function (element, event, handler) {        element.addEventListener(event, handler, false);    };}function init () {    var text = document.getElementById('text');    function resize () {        text.style.height = 'auto';        text.style.height = text.scrollHeight+'px';    }    /* 0-timeout to get the already changed text */    function delayedResize () {        window.setTimeout(resize, 0);    }    observe(text, 'change',  resize);    observe(text, 'cut',     delayedResize);    observe(text, 'paste',   delayedResize);    observe(text, 'drop',    delayedResize);    observe(text, 'keydown', delayedResize);
text.focus(); text.select(); resize();}
textarea {    border: 0 none white;    overflow: hidden;    padding: 0;    outline: none;    background-color: #D0D0D0;}
<body onload="init();"><textarea rows="1" style="height:1em;" id="text"></textarea></body>

Textarea that expands as you type and pushes down content below it

You could try this method to resize your textarea. If this does not work you should probably set some properties to the container of the textarea.

<script language="javascript">
function increaseHeight(e){
e.style.height = 'auto';
var newHeight = (e.scrollHeight > 32 ? e.scrollHeight : 32);
e.style.height = newHeight.toString() + 'px';
}
</script>

<textarea onkeyup="increaseHeight(this);"></textarea>

It is possible to expand a textarea only with CSS?

Instead of textarea , you can use div with contentEditable attribute:

div {  display: inline-block;  border: solid 1px #000;  min-height: 200px;  width: 300px;}
<div contentEditable="true"></div>

How to expand textarea to fit text vertically and horizontally?

Thanks everyone for their advices and responses, but finally I came with my own, a little bit workaround, solution.

So I used this Textarea to resize based on content length as script expanding the height of textarea.

To expand the width of textarea I created a hidden div, whom I copied the text from textarea and recursively I assigned the width of div to textarea. Of course you have to set same styling of text for both elements.

Also as I wanted the textarea to be able to inflate itself "to infinite and beyond", thats why Im changing the width of html and body also.

function textAreaAdjust(o) {
o.style.height = "1px";
o.style.height = (90+o.scrollHeight)+"px";
x = $('textarea').val().replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/ /g, ' ')
.replace(/\n/g, '<br>');
$('div').html(x);
$('html').css('width',$('div').width()+50);
$('body').css('width',$('html').width());
$('textarea').css('width',$('html').width());
}

Auto-expanding textarea

Reset the height before Using scrollHeight to expand/shrink the textarea correctly. Math.min() can be used to set a limit on the textarea's height.

Code:

var textarea = document.getElementById("textarea");
var heightLimit = 200; /* Maximum height: 200px */

textarea.oninput = function() {
textarea.style.height = ""; /* Reset the height*/
textarea.style.height = Math.min(textarea.scrollHeight, heightLimit) + "px";
};

Fiddle: http://jsfiddle.net/gjqWy/155

Note: The input event is not supported by IE8 and earlier. Use keydown or keyup with onpaste and/or oncut if you want to support this ancient browser as well.



Related Topics



Leave a reply



Submit