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
Using JavaScript to Compare Two Input Numbers in HTML
Phantomjs Page.Content Isn't Retrieving the Page Content
Attribute Onclick="Function()" Not Functioning as Intended
Good Beginners Tutorial to Socket.Io
Choosing Mobile Web HTML5 Framework
How to Change the Playing Speed of Videos in HTML5
Infinite Scrolling with React Js
Impossible to Hide Navigation Bars in Safari iOS 7 for Iphone/Ipod Touch
JavaScript Loading Screen While Page Loads
Angularjs - Placeholder for Empty Result from Filter
How to Host Material Icons Offline
Disable Zoom on Input Focus in Android Webpage
What Cross-Browser Issues Have You Faced
Twitter's Typeahead.Js Suggestions Are Not Styled (Have No Border, Transparent Background, etc.)
How to Automatically Close Alerts Using Twitter Bootstrap
Bootstrap 3: How to Make Head of Dropdown Link Clickable in Navbar