Search code examples
next.jstailwind-css

Tailwind css classes not applying in my component, till i take it to my page.js


tailwind is not working properly on my nextjs project, I have checked all my configurations but nothing checks out.
I found out it starts working when I carry my code from my navbar component that appears to not work and put it in my page.js, the classes would apply, and if I go back to my navbar component to add more styles, everything would seem fine for some time then stop again, I have to carry my navbar code to page.js again for it to start working again.

MY NAVBAR COMPONENT

    import Link from 'next/link'
import React from 'react'
import { BiSolidFoodMenu } from "react-icons/bi";
import { RiEBike2Line } from "react-icons/ri";
import { TbInfoOctagon } from "react-icons/tb";
import { TiContacts } from "react-icons/ti";
const Navbar = () => {
  return (
        <div className=' flex  items-center   w-full justify-around space-x-3'>
            <div>
                <h1 className='style '>
                    LUIGIE HOUSE
                </h1>
            </div>
            <div className='flex  space-x-3'>
                    <div className='flex flex-row items-center space-x-2'>
                         <Link href='#'>Home</Link>
                         <BiSolidFoodMenu />
                    </div>
                    <div className='flex flex-row items-center space-x-2'>
                         <Link href='#'>About</Link>
                         <RiEBike2Line />
                    </div>
                    <div className='flex flex-row items-center'>
                         <Link href='#'>Delivery</Link>
                         <TbInfoOctagon />
                    </div>
                    <div className='flex flex-row items-center'>
                         <Link href='#'>Contact</Link>
                         <TiContacts />
                    </div>
            </div>
            <div>
                        Buy
            </div>
        </div>
  )
}

export default Navbar

my tailwind config.js

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
 
    
  ],
  jit: true,
  theme: {
    extend: {},
  },
  plugins: [],
}

my issue is that my tailwind classes don't apply on my project,until I take the code out of my navbar component,and put it on my page.js, then it works.Then i find out my classes are working now, so i go delete the code on my page.js and go back to my navbar component, only for my classes to stop working again after some minutes.

my folder structure enter image description here


Solution

  • -Check that you have the correct import statements in your global styles or wherever you include Tailwind CSS:

       @import 'tailwindcss/base';
       @import 'tailwindcss/components';
       @import 'tailwindcss/utilities';
    

    NB*:Ensure these imports are at the beginning of your main CSS file.

    -If you're still facing issues with certain classes not being applied, you can try explicitly including them in the safelist property. This ensures that Tailwind doesn't purge these classes during the optimization process:

        module.exports = {
      // ...
      jit: true,
      content: [
        // ... (as before)
      ],
      safelist: [
        // Add the classes that are not being applied in your Navbar
        'your-safelisted-class-1',
        'your-safelisted-class-2',
        // ...
        //Replace 'your-safelisted-class-1', 'your-safelisted-class-2', etc., 
        with  the actual classes that are not working as expected.
    
      ],
      // ...
    };
    

    After making these modifications, restart your development server and check if the issue persists.