Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 74

Styling with Tailwind's @layer and File Conventions

Recommended resources

Hello! In our last lesson, we explored how to use the @apply directive to create reusable classes, and we had a crucial discussion about its trade-offs. You learned that while @apply can clean up your HTML, the best way to reuse complex UI is by creating Vue components. We ended with a look at @layer components, which ensures your custom classes can be overridden by utilities.

This lesson dives deep into that @layer directive. You'll learn how to properly structure all of your custom CSS within a Tailwind project. This is a key skill for building scalable and maintainable applications, directly addressing your goal of understanding industry best practices beyond just syntax.

We will cover the three fundamental layers in Tailwind—base, components, and utilities—and learn when and how to use each. We'll also implement a clean file structure to keep your styles organized as your project grows.


Part 1: Why Layers Matter in CSS

Before we start writing code, let's understand why Tailwind uses a layer system. It all comes down to managing the CSS cascade and specificity. In CSS, when two rules have the same specificity, the one that appears later in the stylesheet wins.

Tailwind needs to ensure that utility classes (like mt-4 or bg-red-500) always win over component classes (like our .btn-primary from last lesson). To manage this, it organizes styles into three distinct layers, which are output in a specific order:

  1. base: Global styles and resets.
  2. components: Custom component classes.
  3. utilities: Tailwind's utility classes and your own custom utilities.

This order means a utility class will always override a component style, which is exactly the behavior we want.

To get a foundational understanding, please read the following section from the official documentation.

Adding Custom Styles - Using CSS and @layer

This section of the Tailwind documentation explains the concept of layers and why managing declaration order is so important for a utility-first framework.

Read the section titled 'Using CSS and @layer'. Focus on the explanation for 'Why does Tailwind group styles into “layers”?' and the descriptions of the base, components, and utilities layers.



Part 2: The Three Layers in Practice

Now that you understand the "why," let's see the "what" and "how." The following video gives an excellent practical demonstration of using @layer to define styles for the base, components, and utilities layers.

Tailwind CSS v4 Full Course 2025 | Master Tailwind in One Hour

This clip from a JavaScript Mastery course provides a fantastic walkthrough of how to use @layer for different purposes, from styling base elements to creating new reusable utilities.

Watch the segment from 00:38:22 to 00:45:52. Pay close attention to how the presenter creates styles for each of the three layers and the different use cases they demonstrate.

To summarize the video and our readings, here’s when you should use each layer:

  • @layer base: For setting default styles on plain HTML elements. This is great for establishing your project's typographic scale or basic link styles.
    • Example: Making all h2 elements bold and a specific size.
  • @layer components: For your custom, reusable component classes like .btn, .card, or .badge. This is where the .btn-primary class we created last lesson belongs.
    • Example: Defining a .card class with a border, padding, and shadow.
  • @layer utilities: For creating your own new, single-purpose utility classes that Tailwind doesn't provide out-of-the-box. These classes should have the highest priority.
    • Example: A .flex-center class that combines flex, justify-center, and items-center.


Part 3: Hands-On: Organizing Your VueMart Styles

Let's apply this knowledge to your VueMart project. We'll add custom styles to each of the three layers.

Task 1: Add a Base Style

Let's give all h2 headings a consistent look.

  1. Open your project in VS Code and go to src/style.css.

  2. Add the following code below your @tailwind directives:

    @layer base {
      h2 {
        @apply text-2xl font-bold mb-4 text-gray-800;
      }
    }
    
  3. Go to ProductDetailView.vue and find the <h2> tag for the product name. You can now remove any sizing, weight, or color utilities from its class list, as they are now applied by default from our base layer.

Task 2: Add a Custom Utility

The video you watched showed a great example of a useful custom utility: flex-center. Let's create it.

  1. In src/style.css, add the following code:

    @layer utilities {
      .flex-center {
        @apply flex items-center justify-center;
      }
    }
    
  2. Now you can use this new utility anywhere in your project! For example, if you had a div where you wanted to perfectly center its children, you could simply write <div class="flex-center">...</div>.

You now have your custom styles neatly organized into the correct layers within src/style.css.



Part 4: Scaling Up with File Conventions

For a small project, keeping all your custom styles in style.css is fine. But as an application grows, this file can become large and difficult to manage. A common best practice is to split your custom styles into separate files based on their layer.

Let's refactor your project to use this more scalable structure.

Mastering Tailwind CSS Architecture for Beginners

This article provides a clear recommendation for organizing your Tailwind styles into a dedicated folder, which is a great pattern for maintainable projects.

Read the section titled 'Organizing Your Tailwind Styles', focusing on the part 'What About Separating Styles into Multiple Files?'. This will show you the file structure we are about to implement.

Task 3: Refactor Your Styles into Separate Files

Let's implement the structure described in the article.

  1. In your src directory, create a new folder named styles.

  2. Inside src/styles, create two new files:

    • base.css
    • components.css
    • utilities.css
  3. Open src/style.css and cut the @layer base { ... } block. Paste it into src/styles/base.css.

  4. Cut the @layer components { ... } block (containing your .btn-primary class) and paste it into src/styles/components.css.

  5. Cut the @layer utilities { ... } block (with .flex-center) and paste it into src/styles/utilities.css.

  6. Finally, update src/style.css to import these new files. It should now look like this:

    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    @import './styles/base.css';
    @import './styles/components.css';
    @import './styles/utilities.css';
    

Your application should look and behave exactly the same, but your CSS is now much better organized for future growth. Modern build tools like Vite (which you are using) are typically smart enough to handle these @import statements correctly.



Part 5: A Critical Note for Vue Developers

There's a very important rule when using frameworks like Vue: do not use the @layer directive inside a component's <style> block.

Adding Custom Styles - Layers and per-component CSS

The official documentation has a specific warning about this. Understanding this limitation is crucial for avoiding errors and frustration.

Read the final section, 'Layers and per-component CSS'. It explains exactly why @layer doesn't work in component files and reinforces the recommended approach.

As the documentation explains, tools like Vue process each component's <style> block in isolation. When Tailwind processes the CSS inside a component, it doesn't see the @tailwind directives in your main CSS file, so it doesn't know where to move the layered styles.

This reinforces the core workflow we've been building:

  • Use utility classes directly in your HTML templates.
  • For abstractions, create reusable Vue components.
  • Place any truly global custom styles (@layer rules) in your main CSS entry file (or organized partials, as we just did).


Conclusion

You've now mastered a fundamental aspect of building large-scale applications with Tailwind CSS. By understanding and using layers correctly, you can write clean, predictable, and highly organized custom CSS that works in harmony with the framework, not against it.

Key Takeaways:

  • Tailwind organizes styles into base, components, and utilities layers to control the CSS cascade and ensure utilities have priority.
  • Use @layer base for global element styles, @layer components for reusable component classes (like .btn), and @layer utilities for creating new, atomic helpers.
  • For better maintainability, split your layered styles into separate files (e.g., base.css, components.css) and import them into your main stylesheet.
  • Crucially, never use the @layer directive inside a Vue component's <style> block; all layer definitions belong in your global CSS files.

Next Lesson: We will put this organization to use by learning how to extend Tailwind's capabilities further. We'll install and configure an official Tailwind plugin, @tailwindcss/forms, to apply beautiful, consistent default styles to form elements.