[Solved] Primeng not working correctly after adding Tailwindcss to Project – Css

by
Ali Hasan
angular cssgrid primeng react-typescript tailwind-css

Quick Fix: Update your style.css file to correctly import Tailwind CSS and PrimeNG stylesheets in the following order: 1. Import Tailwind CSS base, components, and utilities. 2. Import the PrimeNG theme CSS file. 3. Finally, import the PrimeNG CSS file. This ensures proper styling for both Tailwind CSS and PrimeNG components.

The Problem:

I’m trying to integrate Primeng and Tailwindcss in an Angular project. However, after importing Tailwind, the Primeng styles cease to function. Even with the ‘prefix’ option applied to Tailwind, Primeng styles remain broken. I isolated the issue by creating a new project with just Tailwindcss and Primeng, but the problem persists. The styles.css file includes the appropriate Primeng imports, but as soon as I add @tailwind/base, Primeng’s styles break again.

The Solutions:

Solution 1: Import TailwindCSS files using the layer() function

In your style.css file, modify the way you import TailwindCSS files. Instead of using the traditional @tailwind base, @tailwind components, and @tailwind utilities directives, import them using the layer() function.

Here’s an example of the updated style.css file:

@import "tailwindcss/base" layer(tailwindcss);
@import "tailwindcss/components" layer(tailwindcss);
@import "tailwindcss/utilities" layer(tailwindcss);
@import "primeng/resources/themes/lara-light-blue/theme.css";
@import "primeng/resources/primeng.css";

Using the layer() function ensures that the TailwindCSS styles are applied correctly and do not interfere with the Primeng styles.

Solution 2: Disable preflight classes

In your `tailwind.config.js` file, add the following line:

corePlugins: { preflight: false }

This will disable the generation of preflight classes, which are used to reset styles for certain elements. This can cause conflicts with the styles provided by PrimeNG. By disabling preflight classes, you can ensure that the PrimeNG styles are applied correctly.

Solution 3: Prioritize CSS Layers

Primeng offers a specific solution for this issue in their documentation.

By reordering the CSS layers, you can ensure that Tailwind’s preflight system doesn’t override Primeng’s styles. Here’s how you can do it:

@layer tailwind-base, primeng, tailwind-utilities;

@layer tailwind-base {
    @tailwind base;
}

@layer tailwind-utilities {
    @tailwind components;
    @tailwind utilities;
}

This approach will prevent Tailwind’s preflight system from interfering with Primeng’s styles and ensure that your Primeng components are styled correctly.

Solution 4: Using CSS Layers

Primeng provides a clean way to style your components using CSS Layers. Here’s the updated `styles.css` file:

@layer tailwind-base, primeng, tailwind-utilities;
@import "primeng/resources/themes/lara-light-blue/theme.css";
@import "primeng/resources/primeng.css";

@layer tailwind-base {
  @tailwind base;
}

@layer tailwind-utilities {
  @tailwind components;
  @tailwind utilities;
}
  • The @layer rule defines the order in which CSS rules are applied. In this case, we’ve added three layers: tailwind-base, primeng, and tailwind-utilities.
  • The primeng layer imports the Primeng CSS files, ensuring that its styles are applied before Tailwind’s.
  • Inside the tailwind-base and tailwind-utilities layers, we import the Tailwind base and utilities, respectively.

With this setup, Primeng’s styles take precedence over Tailwind’s, while Tailwind’s utilities and components are still available for use.

Q&A

How to fix the conflict between Tailwindcss and Primeng styles?

Use @layer to set the order of CSS imports and manage precedence of styles.

What should be the layer order to resolve the styling issue?

Use the following order: tailwind-base, primeng, tailwind-utilities.

Video Explanation:

The following video, titled "From Figma to Tailwind CSS (Design Process + Code) - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Learn what makes a great user interface, tips for working with Figma, and how to translate designs into Figma to real code with Tailwind CSS ...