0
0
Tailwindmarkup~5 mins

Disabled state styling in Tailwind

Choose your learning style9 modes available
Introduction

Disabled state styling shows when a button or input cannot be used. It helps users understand what is not clickable or editable.

When a form button should not be clicked until all fields are filled.
When a checkbox disables other options in a form.
When a feature is temporarily unavailable and should not be used.
When a submit button is disabled after sending data to avoid double clicks.
When inputs are locked based on user permissions.
Syntax
Tailwind
<button disabled class="disabled:opacity-50 disabled:cursor-not-allowed">Click me</button>
Use the disabled attribute on HTML elements like button, input, or select.
Tailwind uses the disabled: prefix to apply styles only when the element is disabled.
Examples
This button looks blue and white normally but becomes faded and not clickable when disabled.
Tailwind
<button disabled class="disabled:opacity-50 disabled:cursor-not-allowed bg-blue-500 text-white px-4 py-2 rounded">Submit</button>
This input box changes background and text color when disabled to show it is inactive.
Tailwind
<input disabled class="disabled:bg-gray-200 disabled:text-gray-500 border rounded px-2 py-1" placeholder="Can't type here" />
The dropdown becomes grayed out and not clickable when disabled.
Tailwind
<select disabled class="disabled:bg-gray-100 disabled:cursor-not-allowed border rounded px-2 py-1"><option>Option 1</option></select>
Sample Program

This page shows a disabled button, input, and select. Each uses Tailwind's disabled: styles to look faded and not interactive.

Tailwind
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Disabled State Styling Example</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="flex flex-col items-center justify-center min-h-screen gap-6 bg-gray-50 p-6">
  <button disabled class="disabled:opacity-50 disabled:cursor-not-allowed bg-blue-600 text-white px-6 py-3 rounded font-semibold">
    Disabled Button
  </button>

  <input disabled placeholder="Disabled input" class="disabled:bg-gray-200 disabled:text-gray-500 border border-gray-400 rounded px-3 py-2 w-64" />

  <select disabled class="disabled:bg-gray-100 disabled:cursor-not-allowed border border-gray-400 rounded px-3 py-2 w-64">
    <option>Disabled Select</option>
  </select>
</body>
</html>
OutputSuccess
Important Notes

Always use the disabled attribute in HTML to make elements truly disabled for accessibility.

Tailwind's disabled: prefix only applies styles when the element is disabled.

Use cursor-not-allowed to show a 'no' symbol cursor on disabled elements.

Summary

Disabled state styling visually shows when elements cannot be used.

Use the HTML disabled attribute with Tailwind's disabled: prefix for styling.

Common styles include faded colors and a 'not allowed' cursor.