Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Styling Forms and Inputs | Building Core UI Components
Tailwind CSS Styling in React Applications

bookStyling Forms and Inputs

When building user interfaces in React, forms and input elements are central to capturing user data. Tailwind CSS offers a powerful set of utilities that allow you to style input fields, labels, and validation states for a polished, accessible experience. You can use base utilities like border, rounded, px, and py to define the shape and space of your inputs. For color and contrast, utilities such as bg-white, text-gray-700, border-gray-300, and placeholder-gray-400 help maintain clarity and readability.

To enhance user experience, Tailwind provides state-based utilities for focus, error, and disabled states. For instance, focus:outline-none and focus:ring-2 focus:ring-blue-500 highlight an input when it receives focus, making it clear to users where they are typing. For error states, border-red-500 and text-red-600 visually indicate issues, while disabled:bg-gray-100 and disabled:cursor-not-allowed make it clear when an input cannot be interacted with. Labels can be styled with block, mb-1, and font-medium for proper spacing and emphasis, ensuring forms are both attractive and accessible.

Consider this example of a React form component styled with Tailwind. It includes a text input with label and displays different styles for normal, focus, error, and disabled states. You can see how each state is handled using Tailwind classes.

import React, { useState } from "react";

function ExampleForm() {
  const [value, setValue] = useState("");
  const [touched, setTouched] = useState(false);
  const [disabled, setDisabled] = useState(false);
  const hasError = touched && value.trim() === "";

  return (
    <form className='max-w-sm mx-auto mt-8'>
      <label
        htmlFor='username'
        className='block mb-1 font-medium text-gray-700'
      >
        Username
      </label>
      <input
        id='username'
        type='text'
        value={value}
        onChange={(e) => setValue(e.target.value)}
        onBlur={() => setTouched(true)}
        disabled={disabled}
        className={`w-full px-3 py-2 border rounded-md shadow-sm placeholder-gray-400
          focus:outline-none focus:ring-2 focus:ring-blue-500
          ${
            hasError
              ? "border-red-500 text-red-600"
              : "border-gray-300 text-gray-700"
          }
          ${disabled ? "bg-gray-100 cursor-not-allowed" : "bg-white"}
        `}
        placeholder='Enter your username'
      />
      {hasError && (
        <p className='mt-1 text-sm text-red-600'>Username is required.</p>
      )}
      <button
        type='button'
        className='mt-4 px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700'
        onClick={() => setDisabled(!disabled)}
      >
        {disabled ? "Enable Input" : "Disable Input"}
      </button>
    </form>
  );
}

This form uses Tailwind utilities to handle the following:

  • Normal state: border-gray-300, text-gray-700, bg-white;
  • Focus state: focus:ring-2, focus:ring-blue-500, focus:outline-none;
  • Error state: border-red-500, text-red-600, and an error message in text-red-600;
  • Disabled state: bg-gray-100, cursor-not-allowed.

By combining these utilities, you can create forms that are visually clear, accessible, and responsive to user interaction.

question mark

Which combination of Tailwind classes would you apply to an input element to visually indicate an error state after validation?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookStyling Forms and Inputs

Scorri per mostrare il menu

When building user interfaces in React, forms and input elements are central to capturing user data. Tailwind CSS offers a powerful set of utilities that allow you to style input fields, labels, and validation states for a polished, accessible experience. You can use base utilities like border, rounded, px, and py to define the shape and space of your inputs. For color and contrast, utilities such as bg-white, text-gray-700, border-gray-300, and placeholder-gray-400 help maintain clarity and readability.

To enhance user experience, Tailwind provides state-based utilities for focus, error, and disabled states. For instance, focus:outline-none and focus:ring-2 focus:ring-blue-500 highlight an input when it receives focus, making it clear to users where they are typing. For error states, border-red-500 and text-red-600 visually indicate issues, while disabled:bg-gray-100 and disabled:cursor-not-allowed make it clear when an input cannot be interacted with. Labels can be styled with block, mb-1, and font-medium for proper spacing and emphasis, ensuring forms are both attractive and accessible.

Consider this example of a React form component styled with Tailwind. It includes a text input with label and displays different styles for normal, focus, error, and disabled states. You can see how each state is handled using Tailwind classes.

import React, { useState } from "react";

function ExampleForm() {
  const [value, setValue] = useState("");
  const [touched, setTouched] = useState(false);
  const [disabled, setDisabled] = useState(false);
  const hasError = touched && value.trim() === "";

  return (
    <form className='max-w-sm mx-auto mt-8'>
      <label
        htmlFor='username'
        className='block mb-1 font-medium text-gray-700'
      >
        Username
      </label>
      <input
        id='username'
        type='text'
        value={value}
        onChange={(e) => setValue(e.target.value)}
        onBlur={() => setTouched(true)}
        disabled={disabled}
        className={`w-full px-3 py-2 border rounded-md shadow-sm placeholder-gray-400
          focus:outline-none focus:ring-2 focus:ring-blue-500
          ${
            hasError
              ? "border-red-500 text-red-600"
              : "border-gray-300 text-gray-700"
          }
          ${disabled ? "bg-gray-100 cursor-not-allowed" : "bg-white"}
        `}
        placeholder='Enter your username'
      />
      {hasError && (
        <p className='mt-1 text-sm text-red-600'>Username is required.</p>
      )}
      <button
        type='button'
        className='mt-4 px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700'
        onClick={() => setDisabled(!disabled)}
      >
        {disabled ? "Enable Input" : "Disable Input"}
      </button>
    </form>
  );
}

This form uses Tailwind utilities to handle the following:

  • Normal state: border-gray-300, text-gray-700, bg-white;
  • Focus state: focus:ring-2, focus:ring-blue-500, focus:outline-none;
  • Error state: border-red-500, text-red-600, and an error message in text-red-600;
  • Disabled state: bg-gray-100, cursor-not-allowed.

By combining these utilities, you can create forms that are visually clear, accessible, and responsive to user interaction.

question mark

Which combination of Tailwind classes would you apply to an input element to visually indicate an error state after validation?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
some-alt