📖 Scenario: You are building a simple contact form for a website. The form should have fields for the user's name, email, and phone number. You want to make sure the phone number input only accepts numbers in a specific pattern.
🎯 Goal: Create a form using Tailwind CSS with input fields for name, email, and phone. Add a pattern to the phone input that only allows 10 digits.
📋 What You'll Learn
Create a form with three input fields: name, email, and phone.
Use Tailwind CSS classes to style the inputs with padding, border, and rounded corners.
Add a pattern attribute to the phone input to accept exactly 10 digits.
Add a required attribute to all inputs.
Use semantic HTML elements and accessible labels.
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites for collecting user information. Using input patterns helps ensure users enter data correctly, improving user experience and data quality.
💼 Career
Web developers often build forms and need to style them with CSS frameworks like Tailwind. They also need to add validation patterns and accessibility features to make forms usable by everyone.
Progress0 / 4 steps
1
Create the basic form structure
Create a <form> element with three input fields: name, email, and phone. Each input should have a matching <label> with the for attribute. Use type="text" for name, type="email" for email, and type="tel" for phone.
Tailwind
Hint
Use <label> and <input> tags with matching for and id attributes.
2
Add Tailwind CSS classes for styling
Add Tailwind CSS classes p-2, border, and rounded to each <input> element to give padding, border, and rounded corners.
Tailwind
Hint
Add the classes inside the class attribute of each input.
3
Add pattern and required attributes
Add the required attribute to all three inputs. Add a pattern attribute to the phone input with the value \d{10} to allow exactly 10 digits.
Tailwind
Hint
The pattern \d{10} means exactly 10 digits.
4
Add accessibility and spacing
Wrap each label and input pair inside a <div> with the Tailwind class mb-4 for spacing. Add aria-label attributes to each input matching their label text for better accessibility.
Tailwind
Hint
Use aria-label on inputs and wrap each label-input pair in a div with mb-4 for spacing.
Practice
(1/5)
1. Which Tailwind CSS class is used to add a border around a form input field?
easy
A. flex
B. bg-blue-500
C. text-center
D. border
Solution
Step 1: Understand Tailwind border utility
The border class adds a 1px solid border around an element, commonly used for inputs.
border-2 is valid for 2px border, rounded is valid for small rounding.
Step 2: Analyze focus:ring usage
focus:ring requires a size like focus:ring-2 to show ring thickness; missing size means no visible ring.
Final Answer:
Incorrect use of focus:ring without ring size -> Option C
Quick Check:
Focus:ring needs size like ring-2 [OK]
Hint: Always add ring size with focus:ring for visible effect [OK]
Common Mistakes:
Omitting ring size after focus:ring
Thinking border-2 is invalid
Assuming rounded is not a Tailwind class
5. You want to create a responsive form input that has: - A border that changes color on focus - Rounded corners - Padding inside - A shadow only on focus Which Tailwind class combination achieves this best?
hard
A. border-2 border-red-500 rounded-full p-1 focus:ring focus:ring-red-300
B. border border-gray-300 rounded-md p-3 focus:border-blue-600 focus:ring-2 focus:ring-blue-600 focus:shadow-lg
C. border border-black rounded-none p-4 focus:outline-none
D. border border-gray-400 rounded-lg p-2
Solution
Step 1: Check border and focus color change
border border-gray-300 sets default border, focus:border-blue-600 changes border color on focus.
Step 2: Verify rounding, padding, and shadow on focus
rounded-md rounds corners, p-3 adds padding, focus:ring-2 focus:ring-blue-600 adds blue ring on focus, and focus:shadow-lg adds shadow only on focus.
Step 3: Compare other options
border-2 border-red-500 rounded-full p-1 focus:ring focus:ring-red-300 uses full rounding and no shadow, C lacks shadow and focus border color, D lacks focus effects.