Written by: Geoffrey Callaghan

Building your first form with tailwind

Building Your First Form With Tailwind

Building your first form with Tailwind CSS can be a straightforward process. Tailwind CSS is a utility-first CSS framework that allows you to quickly create custom designs by composing utility classes. Here’s a basic example of how you can build a simple form using Tailwind CSS:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Form with Tailwind CSS</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
    <div class="max-w-md mx-auto my-10 bg-white p-6 rounded-md shadow-md">
        <h2 class="text-2xl font-semibold mb-4">Contact Us</h2>
        <form action="#" method="POST">
            <div class="mb-4">
                <label for="name" class="block text-gray-700 font-semibold mb-2">Name</label>
                <input type="text" id="name" name="name" class="w-full px-3 py-2 border rounded-md focus:outline-none focus:border-blue-500" placeholder="Enter your name">
            </div>
            <div class="mb-4">
                <label for="email" class="block text-gray-700 font-semibold mb-2">Email</label>
                <input type="email" id="email" name="email" class="w-full px-3 py-2 border rounded-md focus:outline-none focus:border-blue-500" placeholder="Enter your email">
            </div>
            <div class="mb-6">
                <label for="message" class="block text-gray-700 font-semibold mb-2">Message</label>
                <textarea id="message" name="message" class="w-full px-3 py-2 border rounded-md focus:outline-none focus:border-blue-500" rows="4" placeholder="Enter your message"></textarea>
            </div>
            <button type="submit" class="bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 focus:outline-none focus:bg-blue-600">Submit</button>
        </form>
    </div>
</body>
</html>

This code creates a simple contact form with fields for name, email, and message. Here’s a breakdown of how Tailwind CSS classes are used:

  • Utility Classes: Classes like mx-auto, my-10, bg-white, p-6, rounded-md, and shadow-md are utility classes provided by Tailwind CSS. They control layout, spacing, background color, padding, border radius, and box shadow.
  • Typography: Classes like text-2xl and font-semibold control the typography, setting the heading size and font weight.
  • Form Elements Styling: Classes like px-3, py-2, border, rounded-md, focus:outline-none, focus:border-blue-500 are used to style form elements like input fields and text areas.
  • Button Styling: Classes like bg-blue-500, text-white, py-2, px-4, rounded-md, hover:bg-blue-600, focus:outline-none, and focus:bg-blue-600 are used to style the submit button.

You can further customize the form by adding or modifying Tailwind CSS classes based on your specific design requirements. Tailwind CSS documentation provides comprehensive documentation on all available utility classes and their usage.