Create Form Layouts using React and Tailwind CSS
Last Updated :
19 Sep, 2024
We will create a responsive form layout using React and Tailwind CSS. We will design the form with input fields, buttons, and icons to create a modern UI. This form layout can be used in various applications such as login pages sign-up forms and contact forms. Forms are essential components of web applications and are widely used in real-time scenarios. They allow users to input data and interact with a web application.
Forms in Real-time are essential for Data Collection and Interaction, User Authentication, Security, Customization and Personalization, Enhanced User Experience, Business Operations and Transactions, Regulatory Compliance, Documentation and Real-Time Data Processing.
Prerequisites
Approach and Functionalities
- Create a React app.
- Configure Tailwind CSS to style the components.
- Build form components that include input fields and buttons.
- Add icons to form fields using the React Icons package.
- The form will have fields like name, email, password, and a submit button.
- Tailwind CSS will be used to style the form responsively.
- React Icons will be used for a better user experience by adding icons like email and password.
Steps to Create & Configure the Project
Here we will create a sample React JS project then we will install Tailwind CSS once it is completed we will start development for Form Layouts using React and Tailwind CSS. Below are the steps to create and configure the project:
Step 1: Set up a React Application
First create a sample React JS application by using the mentioned command then navigate to the project folder
npx create-react-app react-app
cd react-app
Project Structure:
project StructureUpdated Dependencies:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Step 2: Install and Configure Tailwind CSS
Once Project is created successfully Now install and configure the Tailwind css by using below commands in your project.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Step 3: Install React Icons
Once Project is created successfully Now install react icons by using below commands in your project.
npm install react-icons
Step 4: Develop Business logic
Once Tailwind css installation and configuration is completed. Now we need develop user interface for Form Layouts using tailwind css and html. And it is responsive web page for this we use App.js and App.css files we provide that source code for your reference.
- App.js
- index.css
- tailwind.config.js
Example: This example demonstrates the creation of form layouts using React and Tailwind CSS:
CSS
/*index.css*/
@tailwind base;
@tailwind components;
@tailwind utilities;
body {
overflow: hidden;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
JavaScript
//App.js
import React from 'react';
import { FaUserAlt, FaEnvelope, FaLock } from 'react-icons/fa';
const App = () => {
return (
<div className="flex justify-center
items-center h-screen
bg-green-600">
<form className="bg-white shadow-md
rounded px-8 pt-6 pb-8
mb-4 max-w-md w-full">
<div className="mb-4">
<label className="block text-gray-700
text-sm font-bold mb-2"
htmlFor="username">
Name
</label>
<div className="flex items-center
border-b border-gray-300 py-2">
<FaUserAlt className="text-gray-500 mr-3" />
<input
className="appearance-none
bg-transparent
border-none w-full
text-gray-700 mr-3
py-1 px-2 leading-tight
focus:outline-none"
id="username" type="text"
placeholder="Your name"
/>
</div>
</div>
<div className="mb-4">
<label className="block text-gray-700
text-sm font-bold mb-2"
htmlFor="email">
Email
</label>
<div className="flex items-center
border-b border-gray-300
py-2">
<FaEnvelope className="text-gray-500 mr-3" />
<input
className="appearance-none
bg-transparent border-none
w-full text-gray-700 mr-3
py-1 px-2 leading-tight
focus:outline-none"
id="email"
type="email"
placeholder="Email address"
/>
</div>
</div>
<div className="mb-6">
<label className="block text-gray-700
text-sm font-bold mb-2"
htmlFor="password">
Password
</label>
<div className="flex items-center
border-b border-gray-300
py-2">
<FaLock className="text-gray-500 mr-3" />
<input
className="appearance-none
bg-transparent
border-none w-full
text-gray-700 mr-3
py-1 px-2 leading-tight
focus:outline-none"
id="password"
type="password"
placeholder="******************"
/>
</div>
</div>
<div className="flex items-center
justify-between">
<button
className="bg-green-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>
</div>
);
}
export default App;
JavaScript
/*tailwind.config.js*/
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Step 5: Run the Application
Once Development is completed Now we need run the react js application by using below command. By default the react js application run on port number 3000.
npm start
Output: Once Project is successfully running then open the below URL to test the output.
http://localhost:3000/
Conclusion
By using React Tailwind CSS and React Icons we were able to quickly create a responsive and user-friendly form. The combination of these technologies helps in building clean and modern UIs efficiently and the utility first approach of Tailwind CSS speeds up the styling process. You can further enhance this form by adding validation error handling and additional fields depending on the requirements of your application.