Published on

How to Fix the 'border-border' Class Error in Tailwind CSS

Authors
  • Name
    Ripal & Zalak
    Twitter

Resolving the 'border-border' Class Error in Tailwind CSS

If you've encountered the error The border-border class does not exist. If border-border is a custom class, make sure it is defined within a @layer directive while working on a React-Next.js application with Tailwind CSS, you're not alone. Here's a step-by-step guide to understand and resolve this issue.

Problem

This error typically occurs when you attempt to use the @apply directive for a class that Tailwind does not recognize, either because it is not predefined in Tailwind or is not explicitly defined in your custom configuration.

For example, consider the following CSS snippet in globals.css:

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

@layer base {
  * {
    @apply border-border; // Error occurs here
  }
  body {
    @apply bg-background text-foreground;
  }
}

The error arises because border-border is not a standard Tailwind utility class.

Solution

1. Check for Redundant tailwind.config Files

One common cause of this issue is having both tailwind.config.js and tailwind.config.ts files in your project root. Tailwind will get confused about which file to use.

Fix:

  • Delete the redundant tailwind.config.js file if you're using tailwind.config.ts, or vice versa.
rm tailwind.config.js

2. Define border-border in a Custom Layer

If border-border is a custom class, you need to define it explicitly. You can achieve this by adding it within a @layer directive in your CSS:

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

@layer base {
  .border-border {
    border-width: 1px; /* Replace with your desired styles */
    border-color: #000; /* Example color */
  }
}

3. Extend Tailwind Configuration

Another approach is to define the border-border class in tailwind.config.js or tailwind.config.ts:

module.exports = {
  theme: {
    extend: {
      borderWidth: {
        border: '1px', // Adds a 'border-border' utility
      },
      borderColor: {
        border: '#000', // Example color
      },
    },
  },
  plugins: [],
}

4. Check for Prefix in Tailwind Configuration

If your project uses a prefix for Tailwind classes (e.g., tw-), you must include the prefix when using custom classes:

module.exports = {
  prefix: 'tw-',
  theme: {
    extend: {},
  },
  plugins: [],
}

In this case, the class should be tw-border-border.

5. Ensure Correct Syntax for Tailwind 3.x

If you're using Tailwind 3.x and relying on CSS variables, make sure you follow the correct syntax:

:root {
  --color-primary: 6deg 37% 38.6%; /* Example HSL */
}

.theme {
  primary: 'hsl(var(--color-primary) / <alpha-value>)';
}

Frequently Asked Questions (FAQs)

1. Why does Tailwind not recognize border-border?

border-border is not a default utility class in Tailwind. To use it, you need to define it either in a custom layer or extend it in the Tailwind configuration.

2. Can I use both tailwind.config.js and tailwind.config.ts?

No, having both files in your project root causes conflicts. Retain only one file.

3. What happens if I forget to add a prefix to custom classes?

If you've configured a prefix in your Tailwind configuration, you must use it consistently. Forgetting it will lead to errors like "class not found."

4. How do I debug such Tailwind errors?

  • Check your tailwind.config file for syntax or structural issues.
  • Ensure all custom classes are defined explicitly.
  • Remove conflicting configurations or files.