How To Add Badge Components Using Tailwind CSS?
Last Updated :
25 Sep, 2024
Badges are versatile UI components that display notifications, counts, or statuses. Web applications use them extensively to enhance user experience by providing visual cues. Whether you are creating a social media platform, a notification system, or an e-commerce application, badges help improve interaction by highlighting key information.
Prerequisites
Steps To Add Badge Components
Step 1: Install Node.js and NPM
node -v
npm -v
Step 2: Initialize the Project
Create a new folder for your project, navigate to it in your terminal, and run:
mkdir myprojects
cd myprojects
npm init -y
Step 3: Install Tailwind CSS
npm install tailwindcss
npx tailwindcss init
Step 4: Configure Tailwind
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
Step 5: Create a CSS File for Tailwind
In the src/ folder, create a styles.css file, and add the following lines to include Tailwind's base, components, and utilities:
@tailwind base;
@tailwind components;
@tailwind utilities;
Project Structure:
Folder StructureDependencies
"dependencies": {
"tailwindcss": "^3.4.13"
}
Example: Now that the project is set up, let’s move on to writing the HTML and Tailwind CSS code for the badge components. Open the index.html file and add the following example code for different types of badges:
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Badge Components using Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-8 flex flex-col items-center">
<h1 class="text-3xl font-bold text-green-500 text-center mb-4">
GeeksforGeeks</h1>
<h2 class="text-2xl font-bold text-black text-center mb-8">
Badge Components using Tailwind CSS</h2>
<div class="space-y-8 w-full max-w-2xl">
<!-- Notification Badges -->
<section class="flex flex-col items-center">
<h2 class="text-xl font-semibold mb-4">Notification Badges</h2>
<div class="space-x-4">
<button class="bg-blue-500 text-white px-4 py-2 rounded relative">
Inbox
<span
class="absolute -top-1 -right-1 bg-red-500
text-white text-xs font-bold px-2 py-1
rounded-full">3</span>
</button>
<button class="bg-gray-500 text-white px-4 py-2 rounded relative">
Notifications
<span
class="absolute -top-1 -right-1 bg-green-500
text-white text-xs font-bold px-2 py-1
rounded-full">New</span>
</button>
</div>
</section>
<!-- Status Badges -->
<section class="flex flex-col items-center">
<h2 class="text-xl font-semibold mb-4">Status Badges</h2>
<div class="space-x-2">
<span class="bg-green-500 text-white text-xs font-medium
px-2.5 py-0.5 rounded-full">Online</span>
<span class="bg-red-500 text-white text-xs font-medium
px-2.5 py-0.5 rounded-full">Offline</span>
<span class="bg-yellow-500 text-black text-xs font-medium
px-2.5 py-0.5 rounded-full">Away</span>
</div>
</section>
<!-- Label Badges -->
<section class="flex flex-col items-center">
<h2 class="text-xl font-semibold mb-4">Label Badges</h2>
<div class="space-x-2">
<span class="bg-blue-100 text-blue-800 text-xs font-medium
px-2.5 py-0.5 rounded">Feature</span>
<span class="bg-red-100 text-red-800 text-xs font-medium
px-2.5 py-0.5 rounded">Bug</span>
<span class="bg-green-100 text-green-800 text-xs font-medium
px-2.5 py-0.5 rounded">Improvement</span>
</div>
</section>
<!-- Interactive Badges -->
<section class="flex flex-col items-center">
<h2 class="text-xl font-semibold mb-4">Interactive Badges</h2>
<div class="space-x-2">
<a href="#"
class="bg-purple-500 text-white text-xs
font-medium px-2.5 py-0.5 rounded
hover:bg-purple-600 transition duration-300">
Click me
</a>
<button
class="bg-indigo-500 text-white text-xs
font-medium px-2.5 py-0.5 rounded
hover:bg-indigo-600 transition duration-300">
Hover me
</button>
</div>
</section>
</div>
</body>
</html>
JavaScript
// src/badge.js
document.addEventListener('DOMContentLoaded', () => {
// Example: Update notification badge count
const notificationBadge = document.querySelector('#notificationBadge');
let count = 3;
setInterval(() => {
count = (count % 5) + 1; // Cycle between 1 and 5
notificationBadge.textContent = count;
}, 3000);
// Example: Click handler for clickable badge
const clickableBadge = document.querySelector('#clickableBadge');
clickableBadge.addEventListener('click', (e) => {
e.preventDefault();
alert('Badge clicked!');
});
});
To Run the Application
npx tailwindcss -i ./src/styles.css -o ./Public/output.css --watch
This command watches your styles.css file and outputs the compiled CSS into Public/output.css.
Once the Tailwind CSS is compiled, open the index.html file in a browser to see your badge components in action.
Output:
Badge components using Tailwind CSS
Similar Reads
How to Add Avatar Components using Tailwind CSS? An avatar is a graphical representation of a user or identity, often used in web applications to represent individuals in profiles, comments, or chat systems. This article will guide you through creating customizable avatar components using Tailwind CSS, a utility-first CSS framework that allows for
10 min read
How to Build a Card component using Tailwind CSS ? In this article, we will learn about how to Build a card component using Tailwind CSS, Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Tailwind CSS creates small util
4 min read
Create Progress Bar Component using React and Tailwind CSS A progress bar is a visual representation of progress in a process. It is commonly used in web applications to indicate the completion status of an operation. In this article, we will learn how to create a progress bar component in React using Tailwind CSS for styling.PrerequisitesJavaScriptReactTai
2 min read
How to Add New Colors in Tailwind CSS ? Tailwind CSS is a popular utility-first CSS framework that offers a wide range of pre-designed styles and classes to simplify the process of styling web applications. However, the default set of colors provided by Tailwind may not always meet the requirements of your project. In such cases, you may
4 min read
How To Add Gradient Background With Tailwind? Gradient backgrounds are a common design element that can give your website depth and visual appeal. You can make both linear and radial gradients with Tailwind CSS without writing any custom CSS. With minimal effort, you can apply stunning gradients to your elements by using Tailwind's utility clas
2 min read
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