Search code examples
next.jstailwind-cssradix-ui

tailwind styling failed to work in shadcn ui and next


I am using shadcn ui (https://ui.shadcn.com/) with next.js, a problems occurs that styling the layout doesn't work.

enter image description here

I look into the development tools and found the class of bg-card, card-foreground has no variable linked to them.

enter image description here

this is my configuration

enter image description here

and I added the import '../styles/globals.css' in the app/layout.tsx

enter image description here

tailwind.config.ts

module.exports = {
  darkMode: ["class"],
  content: [
    './pages/**/*.{ts,tsx}',
    './components/**/*.{ts,tsx}',
    './app/**/*.{ts,tsx}',
    './src/**/*.{ts,tsx}',
    ],
  theme: {
    container: {
      center: true,
      padding: "2rem",
      screens: {
        "2xl": "1400px",
      },
    },
    extend: {
      colors: {
        border: "hsl(var(--border))",
        input: "hsl(var(--input))",
        ring: "hsl(var(--ring))",
        background: "hsl(var(--background))",
        foreground: "hsl(var(--foreground))",
        primary: {
          DEFAULT: "hsl(var(--primary))",
          foreground: "hsl(var(--primary-foreground))",
        },
        secondary: {
          DEFAULT: "hsl(var(--secondary))",
          foreground: "hsl(var(--secondary-foreground))",
        },
        destructive: {
          DEFAULT: "hsl(var(--destructive))",
          foreground: "hsl(var(--destructive-foreground))",
        },
        muted: {
          DEFAULT: "hsl(var(--muted))",
          foreground: "hsl(var(--muted-foreground))",
        },
        accent: {
          DEFAULT: "hsl(var(--accent))",
          foreground: "hsl(var(--accent-foreground))",
        },
        popover: {
          DEFAULT: "hsl(var(--popover))",
          foreground: "hsl(var(--popover-foreground))",
        },
        card: {
          DEFAULT: "hsl(var(--card))",
          foreground: "hsl(var(--card-foreground))",
        },
      },
      borderRadius: {
        lg: "var(--radius)",
        md: "calc(var(--radius) - 2px)",
        sm: "calc(var(--radius) - 4px)",
      },
      keyframes: {
        "accordion-down": {
          from: { height: 0 },
          to: { height: "var(--radix-accordion-content-height)" },
        },
        "accordion-up": {
          from: { height: "var(--radix-accordion-content-height)" },
          to: { height: 0 },
        },
      },
      animation: {
        "accordion-down": "accordion-down 0.2s ease-out",
        "accordion-up": "accordion-up 0.2s ease-out",
      },
    },
  },
  plugins: [require("tailwindcss-animate")],
}

so far I don't see anything wrong, no idea why the styling failed to work


Solution

  • I had the same problem, but I managed to solve it. When you init your shadcn config: npx shadcn-ui@latest init, make sure to give the right path and name for your files.

    On my side for:

    • Where is your global CSS file? … app/global.css => I had to change it for src/app/globals.css because I use the src folder in my nextjs
    • Where is your tailwind.config.js located? … tailwind.config.js => I had to change it for tailwind.config.ts because I use typescript instead of javascript.

    I'm pretty sure your error comes from one of these 2 files.

    Also please make sure you have only 1 globals.css (or global.css) file and 1 tailwind.config.ts (or tailwind.config.js) file.

    And if this doesn't fix your problem, like @ahmed said, can you send us your globals.css/global.css + your tailwond.config.ts/.js files?