- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
Master TailwindCSS @apply for Custom Styles

Using TailwindCSS’s @apply for Custom Reusable Styles
If you’ve been working with TailwindCSS, you’ve probably found yourself repeating the same combinations of utility classes across different elements. While Tailwind’s utility-first approach is powerful, there are times when you need to extract these common patterns into reusable styles. That’s where the @apply directive comes in – your secret weapon for maintaining clean, maintainable code while keeping the flexibility of Tailwind.

Understanding @apply
Think of @apply as your style consolidator. It allows you to extract those lengthy chains of utility classes into custom CSS classes while maintaining Tailwind’s utility-based approach. Instead of repeating class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition duration-200" everywhere, you can create a clean, reusable solution.
The Basic Syntax
Here’s how you can start using @apply in your CSS:
.custom-button { @apply px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition duration-200;}Best Practices and Tips
- Component-Specific Styles
When creating components, use
@applyto define base styles that will be consistent across all instances:
.card-base { @apply bg-white rounded-lg shadow-md p-6 transition duration-300 ease-in-out;}
- Responsive Design
You can combine
@applywith responsive modifiers:
.responsive-container { @apply px-4 md:px-6 lg:px-8 max-w-screen-xl mx-auto;}- State Variations Handle different states elegantly:
.interactive-element { @apply transform transition duration-200;}
.interactive-element:hover { @apply scale-105 shadow-lg;}When to Use @apply
- Component Libraries: When building reusable components that maintain consistent styling
- Complex Elements: For elements with many utility classes that appear frequently
- Brand Styles: When implementing company-specific design patterns
- Animation Combinations: For standardizing transition and animation patterns
Remember, while @apply is powerful, it shouldn’t be overused. The beauty of Tailwind lies in its utility-first approach. Use @apply strategically when it genuinely improves your code’s maintainability and readability.
Pro Tips
- Keep your
@applyrules focused and specific - Document complex combinations for team reference
- Use semantic class names that clearly indicate their purpose
- Consider creating a style guide for when to use
@applyin your project

By mastering the @apply directive, you’ll strike the perfect balance between Tailwind’s utility-first philosophy and the need for reusable, maintainable styles. Start small, be consistent, and you’ll develop an intuition for when and where to use this powerful feature effectively.
สร้างเว็บไซต์ 1 เว็บ ต้องใช้งบเท่าไหร่? เจาะลึกทุกองค์ประกอบ website development cost อยากสร้างเว็บไซต์แต่ไม่มั่นใจในเรื่องของงบประมาณ อ่านสรุปเจาะลึกตั้งแต่ดีไซน์, ฟังก์ชัน และการดูแล พร้อมตัวอย่างงบจริงจาก Till it’s done ที่แผนชัด งบไม่บานปลายแน่นอน
Next.js สอน 14 ขั้นตอนเบื้องต้น: สร้างโปรเจกต์แรกใน 30 นาที เริ่มต้นกับ Next.js ใน 14 ขั้นตอนเพียงแค่ 30 นาที พร้อม SSR/SSG และ API Routes ด้วยตัวอย่างโค้ดง่าย ๆ อ่านต่อเพื่อสร้างโปรเจ็กต์แรกได้ทันทีที่นี่
วิธีสมัคร Apple Developer Account เพื่อนำแอปขึ้น App Store ทีละขั้นตอน อยากปล่อยแอปบน App Store ระดับโลก มาอ่านคู่มือสมัคร Apple Developer Account พร้อมเคล็ดลับ TestFlight และวิธีอัปโหลดที่ง่ายในบทความเดียวนี้ได้เลย
TypeScript Interface คืออะไร? อธิบายพร้อมวิธีใช้และข้อแตกต่างจาก Type เรียนรู้วิธีใช้ TypeScript Interface เพื่อสร้างโครงสร้างข้อมูลที่ปลอดภัยและเข้าใจง่าย พร้อมเปรียบเทียบข้อดีข้อแตกต่างกับ Type ที่คุณต้องรู้ ถูกรวมเอาไว้ในบทความนี้แล้ว
Material-UI (MUI) คืออะไร อยากสร้าง UI สวยงามและเป็นมืออาชีพในเวลาอันรวดเร็วใช่ไหม มาทำความรู้จักกับ Material-UI (MUI) ที่ช่วยให้คุณพัฒนาแอปพลิเคชันบน React ได้ง่ายและดูดีในทุกอุปกรณ์
เปรียบเทียบ 3 วิธีติดตั้ง install node js บน Ubuntu: NVM vs NodeSource vs Official Repo แบบไหนดีที่สุด? เรียนรู้วิธีติดตั้ง Node.js บน Ubuntu ด้วย NVM, NodeSource หรือ Official Repo เลือกวิธีที่เหมาะกับความต้องการของคุณ พร้อมเปรียบเทียบ เพื่อการพัฒนาที่มีประสิทธิภาพ! พูดคุยกับซีอีโอ
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.