Auto Resize Textarea JavaScript

Auto Resize Textarea JavaScript
Code Snippet:Auto-expanding Text Area
Author: Steve Graw
Published: January 20, 2024
Last Updated: January 22, 2024
Downloads: 532
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code provides auto-resizing functionality for textareas. It dynamically adjusts the textarea’s height as you type. This feature enhances user experience when entering text.

You can use this code in web forms, chat applications, or any text input areas. It ensures a more user-friendly experience by automatically resizing textareas, reducing the need for manual adjustments and improving the overall usability of your website or application.

How to Create Auto Resize Textarea Javascript

1. Begin by creating an HTML file for your web page. Make sure to include the required Normalize.css link in your HTML’s head section, like this:

<link rel="stylesheet" href="">

2. Insert a <textarea></textarea> element within your HTML body where you want the auto-resizing textarea to appear. You can customize the width and initial height as per your design preferences.


3. Style the textarea using the following CSS styles: (Optional)

body {
  padding: 40px;
  text-align: center;

textarea {
	min-height: 5em;
	max-height: 50vh;
	width: 50%;

4. Copy the JavaScript code into a separate JavaScript file or include it within a <script> tag in your HTML. This code contains the logic for auto-expanding the textarea as users type.

var autoExpand = function (field) {

	// Reset field height = 'inherit';

	// Get the computed styles for the element
	var computed = window.getComputedStyle(field);

	// Calculate the height
	var height = parseInt(computed.getPropertyValue('border-top-width'), 10)
	             + parseInt(computed.getPropertyValue('padding-top'), 10)
	             + field.scrollHeight
	             + parseInt(computed.getPropertyValue('padding-bottom'), 10)
	             + parseInt(computed.getPropertyValue('border-bottom-width'), 10); = height + 'px';


document.addEventListener('input', function (event) {
	if ( !== 'textarea') return;
}, false);

Feel free to customize the CSS to match your design. Implement this code where text input areas need dynamic resizing for an enhanced user interface.

That’s all! hopefully, you have successfully created functionality to auto-resize textarea using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link