Creating an Auto Resizing HTML Textarea using CSS and JavaScript Tutorial

In this tutorial, you can learn how to create an Auto Resizing HTML Textarea using CSS and JS. The tutorial aims to provide students and beginners with a reference for learning some useful CSS and JS tricks for their own website or web application projects. Here, I will be providing a web page script that demonstrates the creation of Auto Resizing textarea.

What is an Auto Resizing Textarea?

Auto Resizing Textarea is an HTML textarea element or multi-line text editing element that automatically resize its height each time the line height of the text is greater or lower than the element. It means that the textarea height is automatically resized according to the content line height while a normal textarea shows a scrollbar when the content exceeds the height of the element.

How to create an Auto Resizing Textarea?

The Auto Resizing Textarea can be easily achieved using CSS and JavaScript. Using the CSS script, we can set the default size or height of the textarea and its design. Then, JavaScript will detect the changes in the textarea content. Check out the sample web page script that I created provided below to have a better idea about creating an Auto Resizing Textarea.

Sample Web Page

The below scripts result in a simple web application that contains an HTML Textarea that automatically resizes its height when the content exceeds or is lower than the current height of the textarea.

Page Interface

The script below is the HTML file script named index.html. It contains the elements of the page layout and the text area element.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>CSS/JS - Auto Resize Textarea</title>
  7.     <link rel="preconnect" href="https://fonts.googleapis.com">
  8.     <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9.     <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  10.     <link rel="stylesheet" href="style.css">
  11.  
  12. </head>
  13.     <div id="wrapper">
  14.         <div class="page-title">Auto Resize Textarea using JavaScript</div>
  15.         <hr style="width:25px">
  16.         <div id="content-wrapper">
  17.             <div>
  18.                 <textarea spellcheck="false" name="message" id="message" placeholder="Write your message here..."></textarea>
  19.             </div>
  20.         </div>
  21.     </div>
  22.     <script src="script.js"></script>
  23.  
  24. </body>
  25. </html>

Stylesheet

Here's the CSS file script that contains the style codes for the page layout and textarea design. The file script is known as style.css and it is loaded on the index page file script.

  1. @import url('https://fonts.googleapis.com/css2?family=Rubik&display=swap');
  2. @import url('https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet');
  3. :root{
  4.     --secular-font: 'Secular One', sans-serif;
  5.     --satisfy-font: 'Satisfy', cursive;
  6. }
  7. *{
  8.     box-sizing: border-box;
  9. }
  10. body *{
  11.     font-family: 'Rubik', sans-serif;
  12. }
  13. /**
  14. Page Design
  15. */
  16. body,
  17. html{
  18.     height: 100%;
  19.     width: 100%;
  20.     margin: 0;
  21.     padding: 0;
  22. }
  23. body{
  24.     background-color: #789395;
  25. }
  26. /* Page Wrapper */
  27. #wrapper{
  28.     width: 100%;
  29.     height: 100%;
  30.     display: flex;
  31.     flex-direction: column;
  32.     align-items: center;
  33.     justify-content: center;
  34. }
  35. .page-title{
  36.     font-size: 35px;
  37.     font-family: var(--secular-font);
  38.     letter-spacing: 2px;
  39.     text-align: center;
  40.     color:#fff;
  41.     text-shadow: 0px 0px 5px #7070709c;
  42. }
  43. /* Content Wrapper */
  44. #content-wrapper{
  45.     width: 550px;
  46.     background: #fff;
  47.     border: 1px solid #cfcfcf;
  48.     box-shadow: 1px 1px 5px #282828c4;
  49.     padding:2em 1.5em;
  50.     overflow: auto;
  51. }
  52. @media (max-width:550px){
  53.     #content-wrapper{
  54.         width: 100%;
  55.     }
  56. }
  57. textarea#message {
  58.     width: 100%;
  59.     height: 42px;
  60.     outline: none;
  61.     border: 1px solid #b7b7b7;
  62.     box-shadow: 1px 1px 5px #c1c1c1;
  63.     resize: none;
  64.     padding: 1em .5em;
  65.     overflow: hidden;
  66.    
  67. }
  68. textarea#message:focus {
  69.     border-color: #52ace1;
  70.     box-shadow: 1px 1px 3px #2aa7f19e;
  71. }    

JavaScript

Lastly, the file script below is the JavaScript file named script.js. It contains the codes for updating or resizing the height of the textarea when the user updates its content and the current element's height is greater or less than the content line height or scroll height.

  1. const textAreaEL = document.getElementById('message')
  2.  
  3. const AutoResizeTextarea = function(el){
  4.     // Update Textarea Height to lowest temporarily
  5.     el.style.height = '0px'
  6.     // Update Textarea Height same as the the scrollHeight
  7.     el.style.height= `${el.scrollHeight}px`
  8. }
  9. //When textare is updated
  10. textAreaEL.addEventListener('input', AutoResizeTextarea.bind(null, textAreaEL))
  11. //When textare has change
  12. textAreaEL.addEventListener('change', AutoResizeTextarea.bind(null, textAreaEL))
  13. //When textare is resized
  14. window.addEventListener('resize', AutoResizeTextarea.bind(null, textAreaEL))

Result

Here are the snapshots of the web page as a result of the scripts I provided above.

Auto Resizing Textarea using CSS and JS

Auto Resizing Textarea using CSS and JS

There you go! I have also provided the source code zip file on this website and it is free to download. Feel free to download and modify the sample script. The download button is located below this tutorial's content.

That's it! I hope this Creating an Auto Resizing HTML Textarea using CSS and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment