Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧭 What You’ll Learn
In this chapter, you’ll learn how to:
⚙️ What Are Reusable Components?
A reusable component is a self-contained block of UI
that can be used in multiple places in your application—either via HTML
partials, component-based frameworks (React, Vue, Svelte), or even in
server-rendered apps.
In Tailwind CSS, we use utility classes directly in markup
to style these components—but to keep things DRY, we can also extract them
using:
🔨 Component Design
Principles
When building Tailwind components:
🧱 Folder Structure for
Components
bash
src/
├── components/
│ ├──
Button.vue / Button.jsx
│ ├──
Card.vue / Card.jsx
│ ├──
Modal.vue / Modal.jsx
│ └── Navbar.vue /
Navbar.jsx
├── styles/
│ └── components.css
(for @apply utilities)
🔹 Using @apply to Create
Reusable Class Styles
styles/components.css
css
.btn
{
@apply px-4 py-2 rounded bg-blue-600
text-white font-semibold hover:bg-blue-700;
}
.btn-outline
{
@apply px-4 py-2 rounded border
border-blue-600 text-blue-600 hover:bg-blue-600 hover:text-white;
}
Then use:
html
<button
class="btn">Submit</button>
<button
class="btn-outline">Cancel</button>
🧩 Example 1: Reusable
Button
html
<!--
Primary Button -->
<button
class="px-4 py-2 bg-indigo-600 text-white rounded
hover:bg-indigo-700">
Submit
</button>
<!--
Secondary Button -->
<button
class="px-4 py-2 bg-gray-200 text-gray-800 rounded
hover:bg-gray-300">
Cancel
</button>
Use @apply or convert these into React/Vue components for
true reusability.
🧩 Example 2: Card
Component
html
<div
class="bg-white rounded-lg shadow-md overflow-hidden">
<img
src="https://via.placeholder.com/300" alt="Image"
class="w-full h-48 object-cover">
<div class="p-4">
<h2 class="text-xl font-bold
mb-2">Card Title</h2>
<p
class="text-gray-600">Short description for the card
component.</p>
<button class="mt-4 bg-blue-600
text-white py-2 px-4 rounded">Learn More</button>
</div>
</div>
Use @apply for .card, .card-title, etc., or componentize in
Vue/React.
🧩 Example 3: Responsive
Navbar
html
<nav
class="bg-white shadow px-6 py-4 flex items-center
justify-between">
<h1 class="text-2xl font-bold
text-blue-600">MyApp</h1>
<div class="hidden md:flex
space-x-6">
<a href="#"
class="text-gray-600 hover:text-blue-600">Home</a>
<a href="#"
class="text-gray-600 hover:text-blue-600">Features</a>
<a href="#"
class="text-gray-600 hover:text-blue-600">Pricing</a>
</div>
</nav>
For mobile, use a hamburger menu or headlessui + Vue/React
transitions.
🧩 Example 4: Modal Dialog
Box
html
<div
class="fixed inset-0 bg-black bg-opacity-50 flex justify-center
items-center z-50">
<div class="bg-white p-6 rounded-lg
w-96 shadow-lg">
<h2 class="text-xl font-bold
mb-4">Are you sure?</h2>
<p class="text-gray-600
mb-6">This action cannot be undone.</p>
<div class="flex justify-end
space-x-4">
<button
class="btn-outline">Cancel</button>
<button
class="btn">Confirm</button>
</div>
</div>
</div>
Show/hide modal with JS logic or framework state.
🧩 Example 5: Form with
Validation UI
html
<form
class="space-y-6 max-w-md mx-auto">
<div>
<label class="block text-gray-700
font-medium">Email</label>
<input type="email"
class="w-full mt-1 p-2 border border-gray-300 rounded focus:outline-none
focus:ring-2 focus:ring-blue-500" />
</div>
<div>
<label class="block text-gray-700
font-medium">Password</label>
<input type="password"
class="w-full mt-1 p-2 border border-gray-300 rounded focus:outline-none
focus:ring-2 focus:ring-blue-500" />
</div>
<button type="submit"
class="btn w-full">Login</button>
</form>
Add error indicators using border-red-500, text-red-600,
etc.
🛠️ Responsive &
Variant Utilities
Tailwind utilities support state & breakpoint modifiers:
html
<button
class="bg-blue-600 hover:bg-blue-700 active:scale-95 sm:px-2
md:px-4">
Click Me
</button>
Modifier |
Use Case |
hover: |
Mouse hover styles |
focus: |
Input focus
styles |
md: |
Medium screens ≥768px |
dark: |
Dark mode
styles |
group-hover: |
Group interactivity |
🧠 Best Practices
📊 Component Class Example
Table
Component |
Key Classes Used |
Button |
px-4 py-2 rounded bg-*
text-* hover:* |
Card |
bg-white p-4
shadow-md rounded-lg |
Navbar |
flex justify-between
items-center px-6 py-4 |
Modal |
fixed inset-0
bg-opacity-50 flex items-center |
Form Input |
border p-2 rounded
focus:outline-none focus:ring-* |
✅ Summary
Tailwind CSS makes it simple to build reusable components
that are:
In the next chapter, we’ll explore optimizing your
Tailwind project and preparing for production.
Answer:
Tailwind CSS is a utility-first CSS framework that provides low-level
utility classes (like p-4, bg-blue-500, etc.) to build designs directly in your
HTML. Unlike Bootstrap, it doesn't offer pre-designed components—giving you full
design control and less opinionated styling.
Answer:
It’s helpful but not required. Tailwind abstracts much of the traditional CSS
syntax into class utilities. However, understanding how CSS works (box model,
flexbox, etc.) will make you far more effective with Tailwind.
Answer:
At first, it may seem cluttered, but Tailwind encourages component-based
design. In frameworks like Vue or React, you can extract reusable
components and even define utility groups using @apply in .css files for
cleaner markup.
Answer:
You can override or extend Tailwind’s design system by modifying the
tailwind.config.js file. You can define your own color palette, breakpoints,
font families, and more under the theme.extend object.
Answer:
Yes, Tailwind is built with mobile-first responsive design in mind. You
can easily apply different styles at various breakpoints using prefixes like
sm:, md:, lg:, xl:, and 2xl:.
Answer:
Use the CLI or PostCSS setup to build Tailwind with your source files. Enable purge
(now content in Tailwind v3+) to remove unused CSS and keep your final build
lightweight. Tailwind is production-ready and widely used in real-world apps.
Answer:
Absolutely. Tailwind integrates seamlessly with React, Vue, Angular,
Svelte, and other frontend frameworks. Simply install it as part of your build
process and apply classes in your component templates.
Answer:
In tailwind.config.js, set darkMode: 'class'. Then toggle a dark class on the
<html> or <body> element to switch themes. Tailwind provides dark:
variants for any utility (e.g., dark:bg-gray-900).
Answer:
Yes. Tailwind only affects styling, not content structure. As long as you write
semantic HTML and manage content correctly, your site remains SEO-friendly.
Just ensure your site loads fast and is accessible.
Answer:
Yes. You can use Tailwind alongside your own CSS rules. You can also use the
@apply directive in your CSS to combine Tailwind utilities into custom class
names for reuse and clarity.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(1)