DEV Community

Cover image for You've Been Using @layer in Tailwind CSS Wrong This Whole Time
Werliton Silva
Werliton Silva

Posted on

5 1 1 1 1

You've Been Using @layer in Tailwind CSS Wrong This Whole Time

If you're using Tailwind CSS with Next.js or other modern frameworks, you've likely come across directives like @layer base, @layer components, and @layer utilities. But what's the difference between them, and how should they be used?

confused

This article explains each of them with practical examples to help you better structure your styles using Tailwind.


@layer base

For global resets and initial styles

This is the first layer loaded. Ideal for:

  • Resetting styles
  • Setting global fonts
  • Basic element styles like body, h1, button, etc.
@layer base {
  h1 {
    @apply text-3xl font-bold text-gray-900;
  }

  body {
     @apply bg-background text-foreground font-sans;
  }
}
Enter fullscreen mode Exit fullscreen mode

@layer components

For reusable components (buttons, cards, badges, etc.)

Loaded after base and before utilities. Perfect for blocks of combined utility classes:

@layer components {
  .btn {
    @apply px-4 py-2 bg-purple-600 text-white rounded-md;
  }

  .card {
    @apply shadow-md rounded-lg p-4 bg-white;
  }
}
Enter fullscreen mode Exit fullscreen mode

@layer utilities

For custom utilities

The last layer to load. Best used for specific, override-style utilities:

@layer utilities {
  .text-shadow {
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  }

  .scrollbar-none {
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
}
Enter fullscreen mode Exit fullscreen mode

Load order summary

@layer base        <- Global styles
@layer components  <- Reusable components
@layer utilities   <- Specific utilities (highest priority)
Enter fullscreen mode Exit fullscreen mode

Practical tip

Use each like this:

  • base: for tag resets and global defaults
  • components: for .btn, .card, .input, etc.
  • utilities: for .text-shadow, .scroll-smooth, etc.

Practical example with menu UI

@layer components {
  .promo-price {
    @apply text-purple-600 font-bold;
  }

  .add-button {
    @apply px-4 py-1 bg-gray-200 rounded text-sm font-medium;
  }
}
Enter fullscreen mode Exit fullscreen mode

surprised

Have you been using them correctly? Share your thoughts! 🚀

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (8)

Collapse
 
ansellmaximilian profile image
Ansell Maximilian

Nice summary. Thanks for sharing!

Collapse
 
werliton profile image
Werliton Silva

Thankss!!

Collapse
 
fstrube profile image
Franklin Strube

Thanks for posting!

Collapse
 
werliton profile image
Werliton Silva

wow. thanks, man.

Collapse
 
michael_liang_0208 profile image
Michael Liang

Awesome, I learned new one!

Collapse
 
werliton profile image
Werliton Silva

wow. thanks, man.

Collapse
 
parag_nandy_roy profile image
Parag Nandy Roy

Super clear breakdown....this definitely clears up the mental model for using @layer properly....more of us needed to hear this honestly...

Collapse
 
werliton profile image
Werliton Silva

Thank you for you feedback.

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay