Tailwind CSS Form Layout Examples
Tailwind CSS is a powerful utility-first CSS framework that allows developers to build custom designs quickly without leaving their HTML files. One of its key applications lies in creating responsive and beautifully styled forms. This guide will walk you through the process of crafting detailed form layouts using Tailwind CSS, highlighting important information and showcasing examples.
Basics of Creating a Form with Tailwind CSS
Before getting into complex layouts, let's cover the basics of structuring a form with Tailwind CSS.
1. Basic Form Structure
A typical HTML form consists of labels, input fields, buttons, and possibly error messages. Below is a simple example of a basic form using Tailwind CSS.
<form class="max-w-md mx-auto p-6 bg-white rounded shadow-md">
<div class="mb-4">
<label for="email" class="block text-gray-700 text-sm font-bold mb-2">Email</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="email" type="email" placeholder="Enter your email">
</div>
<div class="mb-6">
<label for="password" class="block text-gray-700 text-sm font-bold mb-2">Password</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline" id="password" type="password" placeholder="Enter your password">
<p class="text-red-500 text-xs italic">Please choose a password.</p>
</div>
<div class="flex items-center justify-between">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">
Sign In
</button>
<a class="inline-block align-baseline font-bold text-sm text-blue-500 hover:text-blue-800" href="#">
Forgot Password?
</a>
</div>
</form>
- Container Styling:
max-w-md mx-auto p-6 bg-white rounded shadow-md
- This class sets the width, horizontal margin (centers the form), padding, background color, rounded corners, and a shadow to the form. - Input and Label Styling: The
shadow
,appearance-none
,border
, androunded
classes style the inputs, andw-full py-2 px-3
sets their width and padding. - Button Styling: The button is styled using
bg-blue-500
(background color),hover:bg-blue-700
(changes color on hover),text-white
,font-bold
, andpy-2 px-4
(sets padding and font style).
2. Float Label Technique
The float label technique, also known as the "Clevyr Effect," is a design pattern that moves the label above the input field when the input is focused or not empty. Tailwind makes it easy to achieve this effect.
<form class="max-w-md mx-auto p-6 bg-white rounded shadow-md">
<div class="mb-4 relative">
<input type="text" id="name" name="name" required class="peer h-10 w-full border-b-2 border-gray-300
text-gray-900 placeholder-transparent focus:outline-none focus:border-blue-500" placeholder="John Doe" />
<label for="name" class="absolute left-0 -top-3.5 text-gray-600 text-sm peer-placeholder-shown:text-base
peer-placeholder-shown:text-gray-400 peer-placeholder-shown:top-2 transition-all peer-focus:-top-3.5
peer-focus:text-gray-600 peer-focus:text-sm">Name</label>
</div>
</form>
- The
peer
class is utilized for sibling querying to apply styles conditionally based on the state of the input. - The
-top-3.5
andpeer-placeholder-shown:
Tailwind classes move or modify the label's position based on the state of the input field.
3. Multiple Input Fields Form
Creating a form with multiple input fields, labels, and buttons can be achieved easily. Below is an example of a multi-field contact form.
<form class="max-w-md mx-auto p-6 bg-white rounded shadow-md">
<div class="mb-4">
<label for="first_name" class="block text-gray-700 font-bold mb-2">First Name</label>
<input class="border border-gray-300 px-3 py-2 rounded w-full" id="first_name" type="text" placeholder="John">
</div>
<div class="mb-4">
<label for="last_name" class="block text-gray-700 font-bold mb-2">Last Name</label>
<input class="border border-gray-300 px-3 py-2 rounded w-full" id="last_name" type="text" placeholder="Doe">
</div>
<div class="mb-4">
<label for="email" class="block text-gray-700 font-bold mb-2">Email</label>
<input class="border border-gray-300 px-3 py-2 rounded w-full" id="email" type="email" placeholder="john.doe@example.com">
</div>
<div class="mb-4">
<label for="message" class="block text-gray-700 font-bold mb-2">Message</label>
<textarea class="border border-gray-300 p-2.rounded w-full resize-none h-24" id="message" placeholder="Your message here..."></textarea>
</div>
<div class="flex items-center justify-between">
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Send Message</button>
</div>
</form>
- Every input field is wrapped in a
div
with amb-4
class to add margin-bottom. - Labels and inputs are styled using standard Tailwind classes for border, padding, and width.
4. Checkbox and Radio Input
In addition to text inputs, forms often contain checkboxes and radio buttons.
<form class="max-w-md mx-auto p-6 bg-white rounded shadow-md">
<div class="mb-4">
<label class="inline-flex items-center">
<input type="checkbox" class="h-5 w-5 text-blue-600">
<span class="ml-2">Subscribe to our newsletter</span>
</label>
</div>
<div class="mb-4">
<label class="inline-flex items-center">
<input type="radio" name="subscription" class="h-4 w-4 text-blue-600" value="weekly">
<span class="ml-2">Weekly</span>
</label>
<label class="inline-flex items-center ml-6">
<input type="radio" name="subscription" class="h-4 w-4 text-blue-600" value="monthly">
<span class="ml-2">Monthly</span>
</label>
</div>
</form>
- The
inline-flex items-center
classes make the label and input align horizontally. - The
h-5 w-5
andh-4 w-4
classes control the height and width of the checkbox and radio button respectively.
5. Responsive Form Layouts
Creating responsive form layouts, which adapt to various screen sizes, is another crucial aspect of form design. Tailwind CSS’s utility-first approach makes this straightforward.
<form class="bg-white rounded-lg shadow-md p-8">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="block text-gray-700 text-sm font-bold mb-2" for="first_name">
First Name
</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="first_name" type="text" placeholder="John">
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2" for="last_name">
Last Name
</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="last_name" type="text" placeholder="Doe">
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2" for="email">
Email
</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="email" type="email" placeholder="john.doe@example.com">
</div>
<div>
<label class="block text-gray-700 text-sm font-bold mb-2" for="phone">
Phone
</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="phone" type="tel" placeholder="123-456-7890">
</div>
<div class="col-span-2">
<label class="block text-gray-700 text-sm font-bold mb-2" for="message">
Message
</label>
<textarea class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="message" rows="4"></textarea>
</div>
<div class="col-span-2 flex justify-end">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">
Submit
</button>
</div>
</div>
</form>
- The
grid grid-cols-1 md:grid-cols-2 gap-4
classes define a grid layout. It consists of one column on small screens and two columns on medium and up screens, with a gap of4
units. col-span-2
makes elements span across two columns within the grid, useful for elementos like a message textarea and the submit button.
6. Validation and Error Handling with Tailwind
Validating form fields and handling errors visually can improve the user experience. Tailwind CSS can also be used to style form fields based on their validation state.
<form class="max-w-md mx-auto p-6 bg-white rounded shadow-md">
<div class="mb-4">
<label for="email" class="block text-gray-700 font-bold mb-2">Email</label>
<input class="border-red-500 border-2 px-3 py-2 rounded w-full" id="email" type="email" placeholder="Enter your email">
<p class="text-red-500 text-xs italic mt-2">Please enter a valid email.</p>
</div>
<div class="mb-6">
<label for="password" class="block text-gray-700 font-bold mb-2">Password</label>
<input class="border-green-500 border-2 px-3 py-2 rounded w-full" id="password" type="password" placeholder="Enter your password">
</div>
<div class="flex items-center justify-between">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">
Sign In
</button>
</div>
</form>
- The
border-red-500
andborder-2
classes apply a red border indicating an error on the email input field. - An error message below the input field is styled using
text-red-500
for red text,text-xs
for small font size, anditalic
for italic styling.
7. Final Thoughts on Tailwind CSS Form Layouts
Tailwind CSS offers numerous utility classes that facilitate the creation of customizable and responsive form layouts. By leveraging these utilities, developers can quickly prototype and design forms that fit their specific needs and design aesthetics.
Key Takeaways:
- Utility First: Tailwind's utility-first approach enables rapid development of unique form designs.
- Responsive Design: Forms can easily adapt to different screen sizes using grid layouts.
- Validation and Errors: Tailwind utilities can be used to visually indicate validation states.
- Customization: Every aspect of form elements can be customized using combinations of predefined and custom CSS classes.
Using Tailwind CSS for form layouts empowers developers to create modern, responsive, and user-friendly forms with flexibility and ease.
Tailwind CSS Form Layout Examples: A Step-by-Step Guide for Beginners
Creating visually appealing forms is a crucial aspect of web development, and using Tailwind CSS can greatly simplify this process. Here, we'll guide you through setting up your project with Tailwind CSS, creating a form layout, running your application, and understanding the data flow in a typical form submission scenario.
Step 1: Setting Up Your Project
Before we begin, ensure that you have Node.js and npm (Node Package Manager) installed on your machine. Then, proceed as follows:
Initialize Your Project
Create a new directory for your project and navigate into it:
mkdir tailwind-form-examples cd tailwind-form-examples
Initialize your project with npm:
npm init -y
Install Tailwind CSS
Install Tailwind CSS and its dependencies:
npm install -D tailwindcss postcss autoprefixer
Create Tailwind Configuration Files
Generate the
tailwind.config.js
andpostcss.config.js
files:npx tailwindcss init -p
Configure Tailwind for Production
Modify the
tailwind.config.js
to purge unused styles in production. This ensures only the styles used in your project are included:/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./src/**/*.{html,js}", ], theme: { extend: {}, }, plugins: [], };
Set Up an HTML Template
Create a
src
directory and add anindex.html
file:mkdir src echo '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tailwind CSS Forms</title> <link href="output.css" rel="stylesheet"> </head> <body class="bg-gray-100 flex items-center justify-center min-h-screen"> <div id="app"></div> <script src="app.js"></script> </body> </html>' > src/index.html
Add a Basic JavaScript File
Create an
app.js
file inside thesrc
directory:echo '' > src/app.js
Compile Tailwind CSS
Compile your CSS using Tailwind's command-line tool:
npx tailwindcss -i ./src/input.css -o ./src/output.css --watch
Note: Before running this command, create an
input.css
file in thesrc
folder.echo '@tailwind base; @tailwind components; @tailwind utilities;' > src/input.css
Step 2: Creating a Form with Tailwind CSS
Now that our project setup is complete, let's create a simple contact form:
HTML Structure
Update the
index.html
file to include the form layout:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tailwind CSS Forms</title> <link href="output.css" rel="stylesheet"> </head> <body class="bg-gray-100 flex items-center justify-center min-h-screen"> <form action="#" class="bg-white p-8 rounded-lg shadow-md w-full max-w-sm"> <h2 class="text-2xl font-bold mb-4">Contact Us</h2> <div class="mb-4"> <label for="name" class="block text-gray-700 font-medium mb-2">Name</label> <input type="text" id="name" name="name" required class="border border-gray-300 rounded py-2 px-3 w-full focus:outline-none focus:border-blue-500"> </div> <div class="mb-4"> <label for="email" class="block text-gray-700 font-medium mb-2">Email</label> <input type="email" id="email" name="email" required class="border border-gray-300 rounded py-2 px-3 w-full focus:outline-none focus:border-blue-500"> </div> <div class="mb-4"> <label for="message" class="block text-gray-700 font-medium mb-2">Message</label> <textarea id="message" name="message" rows="4" required class="border border-gray-300 rounded py-2 px-3 w-full focus:outline-none focus:border-blue-500"></textarea> </div> <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"> Submit </button> </form> </body> </html>
Styling the Form
Notice how we use Tailwind CSS utility classes to style the form. For example, classes like
bg-white
,p-8
,rounded-lg
,shadow-md
, and more are used for styling different elements such as the form container and input fields.
Step 3: Handling Form Submission
To make the form functional, let’s add some JavaScript for handling form submissions:
JavaScript Logic
Open or modify the
app.js
file to handle form submissions:document.addEventListener('DOMContentLoaded', function() { const form = document.querySelector('form'); form.addEventListener('submit', function(event) { event.preventDefault(); // Prevent the default form submission const name = document.getElementById('name').value; const email = document.getElementById('email').value; const message = document.getElementById('message').value; // Log the form data console.log({ name, email, message }); // Here you would typically send the data to a server using fetch API // e.g., fetch('/server-endpoint', { method: 'POST', body: JSON.stringify({ name, email, message }) }); alert('Form submitted successfully!'); }); });
Understanding the Data Flow
When the user fills out the form and clicks the "Submit" button:
a. The form submission event is intercepted by the
event.preventDefault()
method, preventing the page from refreshing.b. The values of the input fields are retrieved using
document.getElementById
and stored in variables (name
,email
,message
).c. These values are logged to the console. In a real-world scenario, you would send these values to a backend server using AJAX or Fetch API.
d. An alert is shown to the user indicating successful submission.
Step 4: Running the Application
To see our form in action, we need to ensure the development server is running. Since our setup includes Tailwind CSS watch mode, any changes to the code will be reflected automatically.
Start the Development Server
If you haven't already started the Tailwind CSS watcher, do so now:
npx tailwindcss -i ./src/input.css -o ./src/output.css --watch
Open the HTML File in a Browser
Simply open the
src/index.html
file in your browser to view the form. Alternatively, you could set up a local server using tools likehttp-server
.
Conclusion
In this guide, you learned how to set up a project with Tailwind CSS, create a styled form layout, and handle form submissions using basic JavaScript. By following these steps, you are now well-equipped to design attractive and functional forms with Tailwind CSS in your web projects. Happy coding!
Tailwind CSS Form Layout Examples: Top 10 Questions and Answers
Tailwind CSS is a highly customizable utility-first CSS framework that makes it easy to design consistent and responsive websites quickly. One of the areas where Tailwind shines is in creating form layouts, thanks to its extensive set of utilities and flexibility.
Here are ten common questions about using Tailwind CSS to create form layouts, along with detailed answers and examples:
1. How can I create a simple vertical form layout using Tailwind CSS?
Creating a vertical form layout is straightforward in Tailwind CSS because inputs are block-level elements by default, stacking vertically without additional classes. Utilize margin utilities to space them out.
<form>
<div class="mb-4">
<label for="name" class="block mb-1 text-sm font-medium">Name</label>
<input type="text" id="name" name="name" class="w-full p-2 border rounded" placeholder="Enter your name">
</div>
<div class="mb-4">
<label for="email" class="block mb-1 text-sm font-medium">Email</label>
<input type="email" id="email" name="email" class="w-full p-2 border rounded" placeholder="Enter your email">
</div>
<div class="mb-4">
<label for="password" class="block mb-1 text-sm font-medium">Password</label>
<input type="password" id="password" name="password" class="w-full p-2 border rounded" placeholder="Enter your password">
</div>
<button type="submit" class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600">Submit</button>
</form>
In this example, we've used mb-4
to add margin-bottom to each input container, ensuring they don't bunch up next to each other.
2. Can I create a horizontal form layout with Tailwind CSS?
Yes, you can create a horizontal form layout using Flexbox utilities provided by Tailwind. The key is to wrap labels and inputs within a flex-container and distribute them across two columns.
<form>
<div class="flex mb-4">
<label for="name" class="basis-1/4 pr-2 text-sm font-medium">Name:</label>
<div class="basis-3/4">
<input type="text" id="name" name="name" class="w-full p-2 border rounded" placeholder="Enter your name">
</div>
</div>
<div class="flex mb-4">
<label for="email" class="basis-1/4 pr-2 text-sm font-medium">Email:</label>
<div class="basis-3/4">
<input type="email" id="email" name="email" class="w-full p-2 border rounded" placeholder="Enter your email">
</div>
</div>
<div class="flex mb-4">
<label for="password" class="basis-1/4 pr-2 text-sm font-medium">Password:</label>
<div class="basis-3/4">
<input type="password" id="password" name="password" class="w-full p-2 border rounded" placeholder="Enter your password">
</div>
</div>
<button type="submit" class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600">Submit</button>
</form>
Using basis-1/4
and basis-3/4
, we allocate different amounts of space to the labels and inputs. The flex
class turns each div into a flex-container.
3. How do I add error styles to form fields in Tailwind CSS?
To display form errors, you can use Tailwind's color utilities to change border and text colors when validation fails. Here’s how you can apply conditional classes for error states.
<div class="mb-4">
<label for="error-name" class="block mb-1 text-sm font-medium text-red-500">Name (Required)</label>
<input type="text" id="error-name" name="error-name" class="w-full p-2 border-red-500 border rounded" placeholder="Enter your name">
<p class="mt-1 text-sm text-red-500">This field is required.</p>
</div>
In the above code, the error-related parts have been highlighted using text-red-500
for error messages and labels and border-red-500
for the input border.
4. How can I make the form responsive with Tailwind CSS?
To ensure responsiveness, Tailwind provides responsive prefixes like md:
, lg:
, xl:
, etc., which apply styles conditionally based on the screen size. Combining it with Tailwind’s grid system is also very effective.
Responsive Vertical Form (becomes horizontal on medium screens):
<form class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-4">
<div>
<label for="name" class="block mb-1 text-sm font-medium">Name</label>
<input type="text" id="name" name="name" class="w-full p-2 border rounded" placeholder="Enter your name">
</div>
<div>
<label for="email" class="block mb-1 text-sm font-medium">Email</label>
<input type="email" id="email" name="email" class="w-full p-2 border rounded" placeholder="Enter your email">
</div>
<div>
<label for="password" class="block mb-1 text-sm font-medium">Password</label>
<input type="password" id="password" name="password" class="w-full p-2 border rounded" placeholder="Enter your password">
</div>
<button type="submit" class="col-span-2 px-4 py-2 mb-4 text-white bg-blue-500 rounded hover:bg-blue-600">Submit</button>
</form>
The form will have three inputs in a vertical stack on smaller screens and shift to two columns on medium-sized screens or larger due to the md:grid-cols-2
.
5. What’s a good approach for aligning multi-line text areas and labels?
Textareas often need special attention since they have a different height compared to single-line inputs. You can use Tailwind’s vertical alignment utilities to align labels with textareas seamlessly.
<div class="flex items-center mb-4">
<label for="message" class="basis-1/4 pr-2 text-sm font-medium">Message:</label>
<div class="basis-3/4">
<textarea id="message" name="message" rows="5" class="w-full p-2 border rounded" placeholder="Write your message..."></textarea>
</div>
</div>
By wrapping the label and textarea in a flex-container (flex
), we can use items-center
to vertically align the label.
6. How to create an inline form layout using Tailwind CSS?
Inline forms are compact and ideal for short sets of input fields. To achieve this layout, use the inline-flex
utility class along with spacing between child elements.
<form class="inline-flex space-x-4">
<div class="space-y-1">
<label for="name" class="block text-sm font-medium">Name:</label>
<input type="text" id="name" name="name" class="p-1 border rounded w-32" placeholder="Enter your name">
</div>
<div class="space-y-1">
<label for="email" class="block text-sm font-medium">Email:</label>
<input type="email" id="email" name="email" class="p-1 border rounded w-48" placeholder="Enter your email">
</div>
<button type="submit" class="px-2 py-1 text-white bg-blue-500 rounded hover:bg-blue-600">Submit</button>
</form>
Each field and button are placed in separate containers but are aligned next to each other horizontally thanks to inline-flex
and space-x-4
.
7. How can I incorporate custom components or icons into my Tailwind CSS form layout?
Tailwind doesn’t enforce any pre-made component designs; instead, it gives you the freedom to build custom elements. If you want to integrate icons with your form inputs, you can use any SVG-based icon library or even FontAwesome combined with Tailwind's inline-flex
and positioning utilities.
<div class="relative mb-4">
<label for="email" class="absolute top-0 left-0 -ml-4 mt-2 text-sm font-medium text-gray-500 pointer-events-none">Email:</label>
<svg class="w-4 h-4 absolute left-0 top-1/2 transform -translate-y-1/2 text-gray-500" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 5v.01M12 12v.01M12 19v.01M12 8h.01M12 15h.01M12 22h.01"/>
<circle cx="12" cy="5" r="2"/>
<line x1="18" y1="23" x2="13" y2="13.1"/>
<line x1="6" y1="23" x2="11" y2="13"/>
<line x1="15" y1="11" x2="12" y2="8"/>
<line x1="9" y1="11" x2="12" y2="8"/>
</svg>
<input type="email" id="email" name="email" class="pl-10 w-full p-2 border rounded" placeholder="Enter your email">
</div>
Here, we placed a label absolutely inside an inline-block
input field and used an SVG element for the icon.
8. How can I style radio buttons and checkboxes?
Styling radio buttons and checkboxes with Tailwind can be a bit involved, as they’re typically browser-controlled, but you can customize their appearance by wrapping them inside a container and styling the container.
<label class="inline-flex items-center text-sm">
<input type="checkbox" class="mr-2 form-checkbox cursor-pointer" checked>
Subscribe to newsletter
</label>
<label class="inline-flex items-center mt-4 text-sm">
<input type="radio" name="notification_method" value="email" class="mr-2 form-radio cursor-pointer" checked>
Receive via Email
</label>
<label class="inline-flex items-center text-sm">
<input type="radio" name="notification_method" value="message" class="mr-2 form-radio cursor-pointer">
Receive via SMS
</label>
We've used custom styling classes like form-radio
and form-checkbox
for better control over the appearance. Alternatively, consider leveraging third-party libraries designed for Tailwind which offer beautifully styled form controls.
9. How to create a form with multiple columns using Tailwind CSS?
Using Grid utilities, you can easily create forms with multiple columns. This allows you to organize many inputs efficiently and take full advantage of Tailwind’s responsive design capabilities.
<form class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="first_name" class="block mb-1 text-sm font-medium">First Name</label>
<input type="text" id="first_name" name="first_name" class="w-full p-2 border rounded" placeholder="Enter your first name">
</div>
<div>
<label for="last_name" class="block mb-1 text-sm font-medium">Last Name</label>
<input type="text" id="last_name" name="last_name" class="w-full p-2 border rounded" placeholder="Enter your last name">
</div>
<div>
<label for="street_address" class="block mb-1 text-sm font-medium">Street Address</label>
<input type="text" id="street_address" name="street_address" class="w-full p-2 border rounded" placeholder="Enter your street address">
</div>
<div>
<label for="city" class="block mb-1 text-sm font-medium">City</label>
<input type="text" id="city" name="city" class="w-full p-2 border rounded" placeholder="Enter your city">
</div>
<div>
<label for="state" class="block mb-1 text-sm font-medium">State</label>
<input type="text" id="state" name="state" class="w-full p-2 border rounded" placeholder="Enter your state">
</div>
<div>
<label for="postal_code" class="block mb-1 text-sm font-medium">Postal Code</label>
<input type="text" id="postal_code" name="postal_code" class="w-full p-2 border rounded" placeholder="Enter your postal code">
</div>
<!-- Button spans the entire column width -->
<button type="submit" class="col-span-2 px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600">Submit</button>
</form>
This setup splits form fields evenly into two columns using md:grid-cols-2
on screens that are medium-sized or larger.
10. How can I create a floating label input form layout using Tailwind CSS?
Floating labels are a visually appealing pattern where the input label slides down above the input field once the user starts typing or focuses on the input.
<div class="relative mb-4">
<input id="username" type="text" name="username" class="peer block w-full px-4 py-2 pb-0 text-gray-900 border-b-2 border-gray-200 outline-none focus:border-blue-500 focus:pb-[10px]" placeholder=" " autocomplete="off"/>
<label for="username" class="absolute duration-300 transform origin-left peer-[placeholder-shown]:scale-100 peer-[placeholder-shown]:translate-y-2 peer-focus:translate-y-[-10px] peer-focus:text-blue-500 peer-focus:scale-75">Username</label>
</div>
By setting outline-none
on the input, removing all default browser styles and using Tailwind’s peer
and placeholder-shown
utilities, we control the label's behavior based on the input state (focused or placeholder being shown).
Tailwind’s powerful utility classes allow you to create highly customized form layouts for all types of needs—without leaving behind accessibility and performance best practices. Feel free to experiment and combine various utilities as needed!