Facebook Instagram Youtube
  • Home
  • Blog
  • HTML & CSS
    • Login Forms
    • Website Designs
    • Navigation Bars
    • Sidebar Menu
    • Card Designs
    • More
      • CSS Buttons
      • Glowing Effects
      • Social Media Buttons
      • Preloader or Loaders
      • Neumorphism Designs
  • JavaScript
    • Form Validation
    • Image Sliders
    • API Projects
    • JavaScript Games
    • Canvas Projects
  • PHP
  • Contact us
Search
CodingNepal CodingNepal
  • Home
  • Blog
    • 10 Easy JavaScript Games for Beginners with Source Code

      10 Easy JavaScript Games for Beginners with Source Code

      10 Best JavaScript Projects with Free Source Code JavaScript Projects for Beginners

      Top 10 JavaScript Projects for Beginners with Source Code

      Top 10 Profile Card Template Designs in HTML & CSS

      Top 10 Profile Card Template Designs in HTML & CSS

      Top 10 Website Templates Design in HTML CSS & JavaScript

      10+ Website Templates Design in HTML CSS and JavaScript

      Top 5 Sidebar Menu Templates in HTML CSS & JavaScript

      Top 15 Sidebar Menu Templates in HTML CSS & JavaScript

  • HTML & CSS
    • Login Forms
    • Website Designs
    • Navigation Bars
    • Sidebar Menu
    • Card Designs
    • More
      • CSS Buttons
      • Glowing Effects
      • Social Media Buttons
      • Preloader or Loaders
      • Neumorphism Designs
  • JavaScript
    • Form Validation
    • Image Sliders
    • API Projects
    • JavaScript Games
    • Canvas Projects
  • PHP
  • Contact us
Home Javascript Auto Resize Textarea in HTML CSS & JavaScript

Auto Resize Textarea in HTML CSS & JavaScript

By
CodingNepal
-
July 2, 2021

Auto Resize Textarea in HTML CSS & JavaScript

Hey friends, today in this blog you’ll learn how to Auto Resize Textarea using only HTML CSS & JavaScript. In the earlier blog, I have shared how to create Login Form Validation in HTML CSS & JavaScript, and now it’s time to create an auto resizable textarea using only a few lines of JavaScript codes.

Auto Resize Textarea means the height of the textarea automatically resizes according to its content. I have also set max-height for this Textarea, so it’ll start to scroll after the specified max-height. You may have noticed this type of auto resizable textarea in different messaging apps.

In this auto-resize textarea, as you can see in the preview image, there are shown two states of the textarea. The first one is the initial state and the second one is when you put some content in the textarea. When you start to type some characters in the textarea, it will automatically resize the height to fit its content.

Video Tutorial of Auto Resize Textarea in JavaScript

 
In the video, you have seen the demo of an Auto Resizable Textarea and knew how I created it using HTML CSS & few lines of JavaScript codes. I hope you’ve understood the codes and concepts behind creating this auto-resize textarea.

In the JavaScript codes, inside on keyup event, first I got the scrollHeight of the textarea. scrollHeight read-only property returns the entire height of an element in pixels. After I got the scrollHeight value, I passed this value as textarea height. So the textarea height will be the textarea scrollHeight.

I have only explained only main things on this blog but in the video, I have explained each JavaScript line with written comments. If you want to understand which line of code is doing what then please watch the above video.

You might like this:

  • AdBlock Detector in JavaScript
  • Contact Form using HTML & CSS
  • Detect User Browser in JavaScript
  • Login Form with Validation in HTML

Auto Resize Textarea in JavaScript [Source Codes]

To create Auto Resize Textarea in JavaScript. First, you need to create two Files: HTML & CSS files. After creating these files just paste the following codes into your file. You can also download the source code files of this auto resize textarea from the given download button.

First, create an HTML file with the name of index.html and paste the given codes into your HTML file. Remember, you’ve to create a file with .html extension.

<!DOCTYPE html>
<!-- Coding By CodingNepal - youtube.com/codingnepal -->
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Auto Resize Textarea in JavaScript | CodingNepal</title>
    <link rel="stylesheet" href="style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <div class="wrapper">
      <h2>Auto Resize Textarea</h2>
      <textarea spellcheck="false" placeholder="Type something here..." required></textarea>
    </div>
    <script>
      const textarea = document.querySelector("textarea");
      textarea.addEventListener("keyup", e =>{
        textarea.style.height = "63px";
        let scHeight = e.target.scrollHeight;
        textarea.style.height = `${scHeight}px`;
      });
    </script>

  </body>
</html>

Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.

/* Import Google font - Poppins */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body{
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(#4671EA, #AC34E7);
}
::selection{
  color: #fff;
  background: #4671EA;
}
.wrapper{
  width: 470px;
  background: #fff;
  border-radius: 5px;
  padding: 25px 25px 30px;
  box-shadow: 8px 8px 10px rgba(0,0,0,0.06);
}
.wrapper h2{
  color: #4671EA;
  font-size: 28px;
  text-align: center;
}
.wrapper textarea{
  width: 100%;
  resize: none;
  height: 59px;
  outline: none;
  padding: 15px;
  font-size: 16px;
  margin-top: 20px;
  border-radius: 5px;
  max-height: 330px;
  caret-color: #4671EA;
  border: 1px solid #bfbfbf;
}
textarea::placeholder{
  color: #b3b3b3;
}
textarea:is(:focus, :valid){
  padding: 14px;
  border: 2px solid #4671EA;
}
textarea::-webkit-scrollbar{
  width: 0px;
}

That’s all, now you’ve successfully created an Auto Resize Textarea using HTML CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem then please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.

 

  • TAGS
  • Auto Resizable Textarea
  • Auto Resize Textarea
  • Auto Resize Textarea Height
  • Textarea in HTML
Share
Facebook
WhatsApp
Twitter
Copy URL
    Previous articleHow to Detect Browser in JavaScript
    Next articleEmail Validation in HTML CSS and JavaScript
    CodingNepal

    RELATED ARTICLESMORE FROM AUTHOR

    Build An AI Image Generator Website in HTML CSS and JavaScript

    Build An AI Image Generator Website in HTML CSS and JavaScript

    Create Responsive Image Slider in HTML CSS and JavaScript Image Slider in JavaScript

    Create A Responsive Image Slider in HTML CSS and JavaScript

    Create Website with Login & Registration Form in HTML CSS and JavaScript

    Create Website with Login & Registration Form in HTML CSS and JavaScript

    Recent Posts

    Build An AI Image Generator Website in HTML CSS and JavaScript

    Build An AI Image Generator Website in HTML CSS and JavaScript

    December 15, 2023
    How to Create Responsive Fiverr Website in HTML CSS and JavaScript

    How to Create Responsive Fiverr Website in HTML and CSS

    October 11, 2023
    Create A Responsive Coffee Website in HTML and CSS

    Create A Responsive Coffee Website in HTML and CSS

    October 1, 2023
    Create Beautiful Responsive Cards in HTML and CSS

    How to Create Responsive Cards in HTML and CSS

    September 23, 2023
    Create A Responsive Footer in HTML and CSS Only

    Create A Responsive Footer Section in HTML and CSS

    September 16, 2023

    Featured Post

    Build An Image Editor in HTML CSS & JavaScript

    Build An Image Editor in HTML CSS & JavaScript

    CodingNepal - July 15, 2022 11

    Categories

    • HTML and CSS225
    • Javascript168
    • JavaScript Projects97
    • Login Form51
    • Card Design43
    • Navigation Bar35
    • Website Designs25
    • Image Slider21
    • CSS Buttons20
    • Sidebar Menu17
    • JavaScript Games16
    • API Projects15
    • Preloader or Loader15
    • Form Validation14
    • PHP12
    CodingNepal
    ABOUT US
    CodingNepal is a blog dedicated to providing valuable and informative content about web development technologies such as HTML, CSS, JavaScript, and PHP... Read more
    FOLLOW US
    Facebook Instagram Youtube
    • About us
    • Terms & Conditions
    • Privacy policy
    • Contact us
    Copyright © 2024 CodingNepal All Rights Reserved

    AdBlock Detected

    Ad