📖 Scenario: You are building a simple web form with a button that can be disabled based on a condition. Disabled buttons should look different to show users they cannot click them.
🎯 Goal: Create a button styled with Tailwind CSS that changes appearance when disabled. The button should have a blue background and white text when enabled, and a gray background with lighter text when disabled.
📋 What You'll Learn
Use Tailwind CSS classes to style the button
Add a disabled attribute to the button to control its state
Use Tailwind's disabled: variant to style the disabled state
Ensure the button is accessible with proper aria attributes
💡 Why This Matters
🌍 Real World
Buttons are common in web forms and interfaces. Styling disabled buttons clearly helps users understand when actions are unavailable.
💼 Career
Front-end developers must create accessible and visually clear interactive elements using CSS frameworks like Tailwind.
Progress0 / 4 steps
1
Create a basic button element
Write an HTML <button> element with the text Submit. Do not add any classes or attributes yet.
Tailwind
Hint
Start by writing a simple button with the text 'Submit'.
2
Add Tailwind classes for enabled button styling
Add Tailwind CSS classes bg-blue-600, text-white, px-4, py-2, and rounded to the <button> element to style it with a blue background, white text, padding, and rounded corners.
Tailwind
Hint
Use Tailwind classes to style the button with blue background and white text.
3
Add the disabled attribute and disabled state styling
Add the disabled attribute to the <button> element. Then add Tailwind classes disabled:bg-gray-400 and disabled:text-gray-200 to style the button with a gray background and lighter text when disabled.
Tailwind
Hint
Add the disabled attribute and use Tailwind's disabled: variant classes to style the disabled state.
4
Add accessibility attributes for disabled button
Add the aria-disabled="true" attribute to the <button> element to improve accessibility by indicating the button is disabled.
Tailwind
Hint
Use aria-disabled="true" to tell assistive technologies the button is disabled.
Practice
(1/5)
1. What does the disabled: prefix in Tailwind CSS do?
easy
A. Removes the element from the page
B. Disables the element functionality
C. Applies styles only when the element has the disabled attribute
D. Makes the element visible only on hover
Solution
Step 1: Understand the disabled attribute
The HTML disabled attribute marks an element as inactive or unusable.
Step 2: Role of Tailwind's disabled: prefix
The disabled: prefix applies CSS styles only when the element is disabled, allowing visual changes without affecting functionality.
Final Answer:
Applies styles only when the element has the disabled attribute -> Option C
Quick Check:
disabled: prefix = style on disabled [OK]
Hint: Remember disabled: styles only affect disabled elements [OK]
Common Mistakes:
Thinking disabled: disables functionality
Confusing disabled: with hover: or focus:
Assuming disabled: removes elements
2. Which Tailwind CSS class correctly changes the cursor to 'not-allowed' when a button is disabled?
easy
A. disabled:cursor-not-allowed
B. cursor-not-allowed
C. cursor-disabled
D. disabled:cursor-disabled
Solution
Step 1: Identify the cursor style for disabled state
The cursor style for disabled elements is not-allowed, which shows a 'no' symbol.
Step 2: Use Tailwind's disabled prefix
To apply this style only when disabled, use disabled:cursor-not-allowed. Plain cursor-not-allowed applies always.
Final Answer:
disabled:cursor-not-allowed -> Option A
Quick Check:
disabled:cursor-not-allowed = correct syntax [OK]
Hint: Use disabled: prefix before cursor class for disabled styling [OK]
Common Mistakes:
Using cursor-not-allowed without disabled:
Using non-existent cursor-disabled class
Placing disabled: after the class name
3. What will be the background color of this button when disabled?
The input has the disabled attribute, so disabled styles can apply.
Step 2: Analyze cursor style usage
The cursor style cursor-not-allowed applies always. To apply only when disabled, it should be disabled:cursor-not-allowed.
Final Answer:
Cursor style should use disabled: prefix -> Option A
Quick Check:
Cursor needs disabled: prefix for disabled state [OK]
Hint: Add disabled: prefix to cursor styles for disabled elements [OK]
Common Mistakes:
Forgetting disabled: prefix on cursor
Thinking disabled:bg-gray-300 is wrong
Assuming inputs can't be styled
5. You want a button to have a blue background normally, but when disabled, it should have a gray background and the text should be lighter gray. Which Tailwind classes achieve this?
hard
A. bg-blue-600 disabled:bg-gray-400 text-white text-gray-300
B. bg-blue-600 disabled:bg-gray-400 disabled:text-gray-300 text-white
C. bg-blue-600 bg-gray-400 disabled:text-gray-300 text-white
D. bg-blue-600 disabled:bg-gray-400 text-gray-300 disabled:text-white
Solution
Step 1: Set normal background and text colors
Use bg-blue-600 for blue background and text-white for white text normally.
Step 2: Apply disabled styles with prefix
Use disabled:bg-gray-400 for gray background and disabled:text-gray-300 for lighter gray text when disabled.
Step 3: Verify order and prefixes
Classes must have disabled: prefix for disabled styles; base styles apply otherwise.
Final Answer:
bg-blue-600 disabled:bg-gray-400 disabled:text-gray-300 text-white -> Option B
Quick Check:
Disabled styles need disabled: prefix [OK]
Hint: Use disabled: prefix on all styles changing when disabled [OK]
Common Mistakes:
Missing disabled: prefix on text color
Placing disabled classes without prefix
Mixing order causing style override issues
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.