DEV Community

Cover image for How to Create Reusable Components in Tailwind CSS
aryan015
aryan015

Posted on

1

How to Create Reusable Components in Tailwind CSS

Tailwind CSS is a utility-first CSS framework that allows developers to build modern and responsive UIs quickly. While Tailwind is highly customizable, it does not provide predefined components like Bootstrap. However, we can create our own reusable components using Tailwind’s @apply directive. This makes project setup faster and ensures consistency across the design.

Why Use Reusable Components?

  • Consistency: Ensures the same styles are applied across the project.
  • Faster Development: Reduces the need to write repetitive utility classes.
  • Easier Maintenance: Update styles in one place instead of multiple files.

Step-by-Step Guide to Creating Tailwind Components

1. Install Tailwind CSS (If Not Already Installed)

If you haven’t set up Tailwind in your project, you can install it via npm:

npm install -D tailwindcss
Enter fullscreen mode Exit fullscreen mode

2. Define Custom Classes in globals.css or tailwind.css

In your CSS file, use the @layer components directive to create reusable classes.

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .h1 {
    @apply text-4xl font-bold text-gray-900;
  }

  .h2 {
    @apply text-3xl font-semibold text-gray-800;
  }

  .p {
    @apply text-lg text-gray-600;
  }

  .btn {
    @apply px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg hover:bg-blue-700 transition;
  }

  .btn-outline {
    @apply px-4 py-2 border border-blue-600 text-blue-600 rounded-lg hover:bg-blue-600 hover:text-white transition;
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Use Custom Classes in Your HTML

Once the custom classes are defined, you can use them in your HTML files.

<h1 class="h1">Main Heading</h1>
<h2 class="h2">Subheading</h2>
<p class="p">This is a paragraph using Tailwind custom classes.</p>
<button class="btn">Primary Button</button>
<button class="btn-outline">Outline Button</button>
Enter fullscreen mode Exit fullscreen mode

4. Benefits of Using @apply

  • Reduces duplication of Tailwind utility classes.
  • Keeps your HTML cleaner and easier to read.
  • Allows for centralized style updates.

Conclusion

By leveraging Tailwind’s @apply directive, you can create scalable, reusable components without compromising flexibility. This approach speeds up development and ensures design consistency throughout your project. Start using custom Tailwind components today and experience a more efficient workflow! 🚀

Dynatrace image

Observability should elevate – not hinder – the developer experience.

Is your troubleshooting toolset diminishing code output? With Dynatrace, developers stay in flow while debugging – reducing downtime and getting back to building faster.

Explore Observability for Developers

Top comments (0)

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay