Create Buttons UI using React and Tailwind CSS
Last Updated :
08 Oct, 2024
Tailwind CSS is a utility-first CSS framework that allows developers to quickly build responsive and reusable components. We'll explore how to create buttons with different styles such as primary, secondary, and disabled states, and buttons of various sizes.
Prerequisites
Approach For Creating Buttons UI
- Create a new React project using "npx create-react-app" command.
- Inside the src folder create a components to keep a file called Button.js.
- Write the Button component code that leverages Tailwind CSS utility classes. The component takes props like type, size, and disabled for flexibility.
- The component uses Tailwind utility classes like rounded, text-white, bg-blue-500, and hover bg-blue-600 to create different styles for the button.
Steps to create Buttons UI
Step 1: Set up the project using the command.
npx create-react-app buttons-ui-tailwind
cd buttons-ui-tailwind
Step 2: Install Tailwind CSS using the command.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Step 3: Configure the tailwind paths in your tailwind.config.js file.
// tailwind.config.js
module.exports = {
content: [
'./src/**/*.{js,jsx,ts,tsx}',
],
theme: {
extend: {},
},
plugins: [],
}
Step 4: Add tailwind directives to your index.css file.
@tailwind base;
@tailwind components;
@tailwind utilities;
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-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Example: In this example, we will write the following code in different files(The files' names are mentioned in the first line of each code block).
JavaScript
// src/App.js
import React from 'react';
import Button from './components/Button';
function App() {
return (
<div className="flex flex-col items-center justify-center h-screen
bg-gray-100 space-y-4">
<h1 className="text-2xl font-bold">Button UI using React and
Tailwind CSS</h1>
<Button type="primary" size="small">Primary Small</Button>
<Button type="primary" size="medium">Primary Medium</Button>
<Button type="primary" size="large">Primary Large</Button>
<Button type="secondary" size="medium">Secondary Button</Button>
<Button type="primary" size="medium" disabled={true}>Disabled Button</Button>
</div>
);
}
export default App;
JavaScript
// src/components/Button.js
import React from 'react';
const Button = ({ type = 'primary', size = 'medium', disabled = false,
children }) => {
const baseClasses = `rounded px-4 py-2 font-semibold
focus:outline-none transition`;
const typeClasses = {
primary: `bg-blue-500 text-white hover:bg-blue-600`,
secondary: `bg-gray-500 text-white hover:bg-gray-600`,
disabled: `bg-gray-300 text-gray-700 cursor-not-allowed`,
};
const sizeClasses = {
small: `text-xs py-1 px-2`,
medium: `text-base py-2 px-4`,
large: `text-lg py-3 px-6`,
};
const buttonClasses = `${baseClasses} ${sizeClasses[size]}
${disabled ? typeClasses.disabled : typeClasses[type]
}`;
return (
<button className={buttonClasses} disabled={disabled}>
{children}
</button>
);
};
export default Button;
To start the Application run the following command.
npm start
Output:
Buttons UI
Similar Reads
Create Banners using React and Tailwind CSS We will build a responsive banner component using React and Tailwind CSS. Tailwind CSS allows us to create highly customizable and modern UI components with minimal effort. The banner will include a heading and subheading and a call to action button styled for responsiveness and accessibility.Prereq
3 min read
Create Checkboxes UI using React and Tailwind CSS Web forms rely on checkboxes to allow users to choose one option or multiple of several options. In the following post, we are going to walk through how to create a simple and reusable checkbox UI in React using Tailwind CSS. We will go over setting up the project, implementing the component, and st
5 min read
Create Feeds UI using React and Tailwind CSS In the world of social networking, feeds are the primary way users interact with content. Whether it is on LinkedIn, Facebook, or Twitter the feed showcases posts updates, and activities from people or organizations. This article will help you build a LinkedIn-style feed UI using React and Tailwind
4 min read
Create Tables UI using React and Tailwind CSS The Basic Table is the simplest form of a table ideal for displaying simple data without any additional styling. It consists of a header row and data rows. This type of table is useful for presenting lists or tabular data where complex styling or interaction is not required. This is perfect for disp
5 min read
Create Navbars UI using React and Tailwind CSS A UI plays an important role because a clean, well-designed interface creates a positive first impression and if the UI is good then users can stay on our website some more time and if the UI is bad then they can not stay on our site for more time. we will see how to Create Navbars UI using React an
5 min read
Create Logo Clouds using React and Tailwind CSS A Logo Cloud is a webpage section that displays logos of partners, sponsors, clients, or technologies in a visually appealing way. This component is often used to establish credibility or showcase affiliations. Using React and Tailwind CSS you can create a responsive and customizable logo cloud that
3 min read
Create Command Palettes UI using React and Tailwind CSS This article shows you how to create a Command Palette UI using React and Tailwind CSS. A command palette lets users easily search for and run commands within an app, making navigation faster and more efficient. Weâll walk you through building a simple and intuitive interface where users can type, s
4 min read
Create Dropdowns UI using React and Tailwind CSS Dropdown UI components are often used in web applications for forms, navigation, or user preferences, allow users to select from a list of options by clicking a button, which will display a drop-down menu, in this article we will create a dropdown element using React for functionality and Tailwind C
3 min read
Create FAQs using React and Tailwind CSS A Frequently Asked Questions section is a common feature found on websites and applications that helps users find answers to common queries in an organized manner. A well-designed FAQ can improve user experience reduce support requests and provide users with quick and easy access to helpful informat
5 min read
Create Header using React and Tailwind CSS In modern web development building responsive and customizable user interfaces is crucial. One of the essential elements of any web application is the header which typically contains navigation links branding or other important controls. we will create a responsive header section using React and Tai
4 min read