fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← HTML

HTML / 8 MIN READ

Text Area

Where Text enters

From the original Fervor library. Examples may use older package versions.

Title: Mastering HTML Textarea: A Fun and Easy Tutorial

Introduction: Today, we’re going to dive into the wonderful world of HTML textarea. Get ready to master this versatile input element that allows users to input and edit multiple lines of text. By the end of this tutorial, you’ll be armed with all the knowledge you need to create amazing text input areas. So let’s get started!

Step 1: Basic Syntax To begin, let’s take a look at the basic syntax for creating a textarea in HTML:

<textarea rows="4" cols="50"></textarea>

Here, we have a <textarea> tag with two attributes: rows and cols. The rows attribute determines the number of visible lines, and the cols attribute sets the width of the textarea in characters.

Step 2: Placeholder Text

Sometimes, it’s helpful to provide users with a hint about what they should enter in the textarea. We can achieve this by using the placeholder attribute. Let’s see an example:

<textarea rows="4" cols="50" placeholder="Enter your message here"></textarea>

The placeholder text will appear inside the textarea until the user starts typing.

Step 3: Limiting Input Length

If you want to restrict the maximum number of characters a user can enter in the textarea, you can use the maxlength attribute. Let’s take a look:

<textarea rows="4" cols="50" maxlength="100"></textarea>

In this example, the user will be limited to entering 100 characters. Once they reach the limit, they won’t be able to type any more characters.

Step 4: Wrapping Text

By default, textarea content wraps automatically to the next line when it reaches the edge of the specified width. However, you can control this behavior using the wrap attribute. Let’s see the available options:

  • wrap="soft" (default): Text wraps when it reaches the edge.
  • wrap="hard": Text wraps at a specified line break character, and no automatic wrapping occurs.
  • wrap="off": Text does not wrap at all, requiring manual line breaks.

Here’s an example:

<textarea rows="4" cols="50" wrap="off"></textarea>

Step 5: Styling the Textarea

Now, let’s spice up our textarea with some CSS styling. You can use CSS to customize the appearance of the textarea and make it fit perfectly into your web page’s design. Here’s a simple example:

<style>
    textarea {
        border: 2px solid #ccc;
        border-radius: 5px;
        padding: 10px;
        font-family: Arial, sans-serif;
        font-size: 14px;
    }
</style>

<textarea rows="4" cols="50"></textarea>

Feel free to experiment and add your own creative touch to make the textarea truly shine!

Step 6: Retrieving User Input

To access the content entered by the user in the textarea, you can use JavaScript. Here’s a basic example using the value property:

<textarea id="myTextarea" rows="4" cols="50"></textarea>

<button onclick="showInput()">Submit</button>

<script>
    function showInput() {
        var input = document.getElementById("myTextarea").value;
        alert("User input: " + input);
    }
</script>

In this example, we retrieve the textarea’s value using its id and display it in an alert box when the user clicks the “Submit” button.

Bonus Time

Title: Unleash Your Creativity with a Cool Textarea Design

Introduction: Welcome back, adventurous web developer! In this section, we’re going to explore some exciting ways to enhance the visual appeal and functionality of your textarea. Let’s dive in and discover how to create a cool textarea design and leverage its power in unique ways.

Step 1: Styling with CSS

To create a cool textarea, we’ll start by applying some CSS magic. Let’s make our textarea stand out from the crowd:

<style>
    .cool-textarea {
        background-color: #f1f1f1;
        border: 2px solid #999;
        border-radius: 8px;
        padding: 12px;
        font-family: 'Arial', sans-serif;
        font-size: 16px;
        color: #333;
        resize: none;
    }
</style>

<textarea class="cool-textarea" rows="6" cols="40" placeholder="Express your creativity here!"></textarea>

In this example, we’ve added a class called “cool-textarea” and applied various CSS properties to achieve a sleek and visually appealing design. Feel free to experiment and adapt the styles to match your own unique ideas.

Step 2: Adding Auto-Expanding Feature

Wouldn’t it be amazing if our textarea could automatically expand as the user types? Let’s make it happen using JavaScript:

<style>
    .cool-textarea {
        /* ...existing styles... */
        overflow: hidden;
        transition: height 0.3s;
    }
</style>

<textarea
    class="cool-textarea"
    rows="1"
    oninput="autoExpand(this)"
    placeholder="Express your creativity here!"></textarea>

<script>
    function autoExpand(textarea) {
        textarea.style.height = "auto";
        textarea.style.height = textarea.scrollHeight + "px";
    }
</script>

With this code, the textarea will dynamically adjust its height to accommodate the entered text. It’s an elegant feature that provides a smooth user experience.

Step 3: Adding Emoji Picker

Adding an emoji picker to your textarea can bring a touch of fun and expressiveness to your web page. Let’s integrate an emoji picker library called “emoji-button” to achieve this:

<script src="https://unpkg.com/emoji-button"></script>

<textarea class="cool-textarea" rows="4" cols="40" id="myTextarea"></textarea>

<button onclick="showEmojiPicker()">Pick an Emoji</button>

<script>
    function showEmojiPicker() {
        const textarea = document.getElementById("myTextarea");
        const picker = new EmojiButton();
        picker.on("emoji", (selection) => {
            textarea.value += selection.emoji;
        });
        picker.togglePicker();
    }
</script>

In this example, we’ve added a button that triggers the emoji picker. When the user selects an emoji, it will be appended to the textarea.

Conclusion: Congratulations on creating a cool textarea design and exploring its amazing capabilities! You’ve learned how to style a textarea using CSS, implement an auto-expanding feature, and even integrate an emoji picker to add a dash of excitement. Now it’s time to unleash your creativity and implement these ideas into your own web projects. Have fun and keep innovating!

Bonus React

Title: Implementing a Cool Textarea in React

Introduction: Hey React enthusiast! In this section, we’ll explore how to implement a cool textarea in a React component. We’ll cover styling, auto-expanding functionality, and integrating an emoji picker. Get ready to bring some React awesomeness to your textarea!

Step 1: Setting up a React Component

Let’s start by creating a new React component for our textarea. Open your favorite code editor and create a new file called “CoolTextarea.js”. Here’s a basic template to get you started:

import React, { useState } from 'react';

const CoolTextarea = () => {
  // State to store the textarea value
  const [textareaValue, setTextareaValue] = useState('');

  // Function to handle textarea input
  const handleTextareaChange = (event) => {
    setTextareaValue(event.target.value);
  };

  // JSX to render the component
  return (
    <textarea
      className="cool-textarea"
      rows="1"
      value={textareaValue}
      onChange={handleTextareaChange}
      placeholder="Express your creativity here!"
    />
  );
};

export default CoolTextarea;

In this example, we’re using React’s useState hook to manage the textarea value. The handleTextareaChange function updates the textarea value as the user types.

Step 2: Styling the Component

Let’s style our CoolTextarea component by adding CSS. Create a new CSS file called “CoolTextarea.css” and add the following styles:

.cool-textarea {
  background-color: #f1f1f1;
  border: 2px solid #999;
  border-radius: 8px;
  padding: 12px;
  font-family: 'Arial', sans-serif;
  font-size: 16px;
  color: #333;
  resize: none;
}

Make sure to import the CSS file into your component:

import React, { useState } from 'react';
import './CoolTextarea.css'; // Import the CSS file

// Rest of the component code...

Step 3: Adding Auto-Expanding Functionality

Next, let’s add the auto-expanding functionality to our CoolTextarea component. We’ll update the component code to dynamically adjust the textarea’s height based on the content.

import React, { useState, useRef, useEffect } from 'react';

const CoolTextarea = () => {
  const [textareaValue, setTextareaValue] = useState('');
  const textareaRef = useRef(null); // Reference to the textarea element

  const handleTextareaChange = (event) => {
    setTextareaValue(event.target.value);
  };

  useEffect(() => {
    // Auto-expanding logic
    const textarea = textareaRef.current;
    textarea.style.height = 'auto';
    textarea.style.height = `${textarea.scrollHeight}px`;
  }, [textareaValue]);

  return (
    <textarea
      ref={textareaRef}
      className="cool-textarea"
      rows="1"
      value={textareaValue}
      onChange={handleTextareaChange}
      placeholder="Express your creativity here!"
    />
  );
};

export default CoolTextarea;

In this updated code, we’re using the useRef hook to create a reference to the textarea element. Inside the useEffect hook, we set the textarea’s height to auto and then update it to match the content’s scrollHeight.

Step 4: Integrating Emoji Picker

To integrate an emoji picker into our CoolTextarea component, we’ll use a popular library called “emoji-mart”. First, install the library by running the following command in your project directory:

npm install emoji-mart

Step 4: Integrating Emoji Picker (continued) Once you’ve installed the “emoji-mart” library, we can proceed with integrating the emoji picker into our CoolTextarea component.

import React, { useState, useRef } from 'react';
import { Picker } from 'emoji-mart';
import 'emoji-mart/css/emoji-mart.css'; // Import the CSS for emoji-mart
import './CoolTextarea.css';

const CoolTextarea = () => {
  const [textareaValue, setTextareaValue] = useState('');
  const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  const textareaRef = useRef(null);

  const handleTextareaChange = (event) => {
    setTextareaValue(event.target.value);
  };

  const handleEmojiSelect = (emoji) => {
    setTextareaValue(textareaValue + emoji.native);
    setShowEmojiPicker(false);
  };

  return (
    <div className="cool-textarea-container">
      <textarea
        ref={textareaRef}
        className="cool-textarea"
        rows="1"
        value={textareaValue}
        onChange={handleTextareaChange}
        placeholder="Express your creativity here!"
      />
      <button onClick={() => setShowEmojiPicker(!showEmojiPicker)}>😄</button>
      {showEmojiPicker && (
        <Picker
          onSelect={handleEmojiSelect}
          title="Pick an Emoji"
          emoji="point_up"
          style={{ position: 'absolute', bottom: '100%', right: '0' }}
        />
      )}
    </div>
  );
};

export default CoolTextarea;

In this updated code, we’ve added a button that toggles the visibility of the emoji picker. When an emoji is selected, it is appended to the textarea value. The Picker component from the “emoji-mart” library is used to display the emoji picker.

Step 5: Using the CoolTextarea Component

Now that our CoolTextarea component is complete, we can use it in other parts of our React application. Import the component into a parent component and render it as desired:

import React from 'react';
import CoolTextarea from './CoolTextarea';

const App = () => {
  return (
    <div>
      <h1>My Cool App</h1>
      <CoolTextarea />
    </div>
  );
};

export default App;

In this example, we’ve imported the CoolTextarea component and included it within the App component.

Conclusion: Congratulations on implementing a cool textarea in React! You’ve learned how to create a React component with styling, auto-expanding functionality, and an integrated emoji picker. Feel free to customize and expand upon this component to fit your project’s needs. Happy coding!

Keep your curiosity going.Explore more HTML →
287 TUTORIALS · 22 TOPICSREADY