DEV Community

Cover image for How to create and Style a Responsive Form using Tailwind CSS
Sampson Ovuoba for Devwares

Posted on • Originally published at devwares.com on

How to create and Style a Responsive Form using Tailwind CSS

If you are one of those keeping in the trend of CSS framework, you will notice that tailwind CSS is fast growing in popularity among developers around the world.

It’s different from the other CSS framework in that it makes use of utility-first classes.
In today’s tailwind CSS tutorial, we will be creating a responsible form using Tailwind CSS.
The tailwind CSS form will look like the image below.

Tailwind CSS Form

Table of content

  • Prerequisites
  • Add Tailwind CSS to project
  • Adding a logo to the Tailwind CSS form
  • Adding the Tailwind CSS form input fields
  • Adding the button
  • Overview of everything
  • Conclusion

Prerequisites

  • Latest version of Tailwind CSS
  • Basic knowledge of HTML
  • Basic knowledge of Tailwind CSS

Add Tailwind CSS to project
For us to be able to build our Tailwind CSS form, we must first install it. You can check our article on how to go about this here, or you can check out the Tailwind CSS docs.

Once the Tailwind CSS is installed, we can link the Tailwind CSS stylesheet to our page by using the link tag on our head element, as shown below.

Code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login form</title>
    <link rel="stylesheet" href="style.css">
</head>
Enter fullscreen mode Exit fullscreen mode

Now, we can build the rest of our login form.
Adding a logo to the Tailwind CSS form

At this stage, we can go ahead to add some styles to the body of our page. This Tailwind CSS style will serve everything inside our page.

Code:

<body class="bg-gray-300" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">.
Enter fullscreen mode Exit fullscreen mode

Furthermore, we can go ahead to add the logo to our Tailwind CSS login form.

Code:

  <div class="h-screen flex items-center justify-center">
        <form action="" class="w-full md:w-1/3 bg-white rounded-lg items-center">
            <div class="flex font-bold justify-center mt-6">
                <img src="images/avater1.png" alt="" class="h-24 w-24">
            </div>
            <h2 class="text-3xl text-center text-gray-700 mb-4">Welcome Back!</h2>.
Enter fullscreen mode Exit fullscreen mode

In the code above, we added the header for our form and the image of the header. Some of the tailwind CSS classes we included on this page include

  • Flex which allows items to shrink and grow to prevent overflow
  • w-full gives the form a width of 100%
  • bg-white sets the background to white
  • rounded-lg gives the edge of the form a rounded look
  • md:w-1/3 sets the minimum width to 33.333%
  • mt-6, which sets the top margin to 24px and many other classes included.

The login form at this stage should look like the image below.

Tailwind CSS Form

Adding the Tailwind CSS form input fields

The next step will be to add the input fields and style them using Tailwind CSS.

Code:

 <div class="px-12 pb-10">
                <div class="w-full mb-2">
                    <div class="flex justify-center">
                        <input type="text" placeholder="Username"
                            class="px-8 w-full border rounded py-2 text-gray-700 focus:outline-none items-center">
                    </div>
                </div>
                <div class="w-full mb-2">
                    <div class="flex justify-center">
                        <input type="password" placeholder="Password"
                            class="px-8 w-full border rounded py-2 text-gray-700 focus:outline-none">
                    </div>
                </div>
Enter fullscreen mode Exit fullscreen mode

In our code, we made two inputs Username and Password. We also styled them the same way. Using some Tailwind CSS classes like

  • Border which sets the width of the input to 1px
  • Px-8 set the left and right padding to 32px while py-2 sets the top and bottom margin to 8px

The image of our project at this point should look like the image below.

Tailwind CSS Form

Adding the button

For the final step, we need to add the button that the user will click to log in. We are also going to include a forget password and create an account link below our button.

Code:

 <button class="w-full mt-6 py-2 rounded bg-purple-500 text-gray-100 focus:outline-none">Log In</button>
                <a href="#"
                    class="text-sm text-opacity-200 float-right mt-6 mb-4 hover:text-purple-600 underline">
                        Forget Password?
                </a>
                <a href="#" class="text-sm text-opacity-200 float-left mt-6 mb-8 hover:text-purple-600 underline">
                    Create Account
                </a>
            </div>
    </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The classes we added to the button will have rounded borders with a purple background color, full width, outline focus with padding and margins.

Our web page should look like the image below.

Tailwind CSS Form

Overview of everything

If you have made mistakes while coding due to the wrong usage of “div” or didn’t place a tag well. You can check our complete code below.

Code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login form</title>
    <link rel="stylesheet" href="style.css">
</head>

<body class="bg-gray-300"
    style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;">
    <div class="h-screen flex items-center justify-center">
        <form action="" class="w-full md:w-1/3 bg-white rounded-lg items-center">
            <div class="flex font-bold justify-center mt-6">
                <img src="images/avater1.png" alt="" class="h-24 w-24">
            </div>
            <h2 class="text-3xl text-center text-gray-700 mb-4">Welcome Back!</h2>
            <div class="px-12 pb-10">
                <div class="w-full mb-2">
                    <div class="flex justify-center">
                        <input type="text" placeholder="Username"
                            class="px-8 w-full border rounded py-2 text-gray-700 focus:outline-none items-center">
                    </div>
                </div>
                <div class="w-full mb-2">
                    <div class="flex justify-center">
                        <input type="password" placeholder="Password"
                            class="px-8 w-full border rounded py-2 text-gray-700 focus:outline-none">
                    </div>
                </div>
                 <button class="w-full mt-6 py-2 rounded bg-purple-500 text-gray-100 focus:outline-none">Log In</button>
                <a href="#"
                    class="text-sm text-opacity-200 float-right mt-6 mb-4 hover:text-purple-600 underline">
                        Forget Password?
                </a>
                <a href="#" 
                    class="text-sm text-opacity-200 float-left mt-6 mb-8 hover:text-purple-600 underline">
                        Create Account
                </a>
            </div>
    </div>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Conclusion

In today Tailwind CSS tutorial practice, we built a login form using Tailwind CSS. The tailwind CSS form had an image, two inputs (Username and Password) and a login button.

However, we styled our Tailwind CSS form using a CSS framework called Tailwind CSS.
I hope you enjoyed the Tailwind CSS tutorial.

Design and code tailwind css websites 3x faster

We created a tool to visually build tailwind css components, prototypes, websites, and webapps. Ship projects faster using an intuitive tailwind builder and editor.Try Windframe out for free.

WINDFRAME

Resources

Top comments (0)