Published on

Fix Background Opacity Issues in Tailwind CSS and ShadCN UI

Authors
  • Name
    Ripal & Zalak
    Twitter

Fix Background Opacity Issues in Tailwind CSS and ShadCN UI

When working with Tailwind CSS and ShadCN UI, you might encounter issues applying background opacity to custom colors defined in HSL values. This guide will help you troubleshoot and resolve such issues to ensure your styles work as expected.

The Issue

Suppose you define a custom color in globals.css as:

--primary: 348, 76%, 64%;

When using this color in a Tailwind class like:

<button class="hover:bg-primary/90">Button</button>

you might find that the opacity setting does not work as intended, leaving your button's background unchanged.


Why This Happens

This issue typically arises because of the way HSL colors are defined and handled in Tailwind CSS. Legacy HSL formats using commas (,) instead of spaces can cause opacity calculations to fail. Additionally, improper configuration in the Tailwind theme or global styles can contribute to this problem.


Solution: Fixing the Configuration

1. Use Space-Delimited HSL Values

Update your HSL definitions in globals.css to use spaces instead of commas:

--primary: 348 76% 64%;

2. Update Tailwind Config for Alpha Support

In your tailwind.config.ts, ensure you use hsla for colors that require opacity:

module.exports = {
  theme: {
    extend: {
      colors: {
        primary: {
          DEFAULT: 'hsla(var(--primary))',
          foreground: 'hsla(var(--primary-foreground))',
        },
      },
    },
  },
}

This allows Tailwind to calculate alpha values properly.

3. Define Colors with Alpha Value

If you want to include opacity directly in the custom color, use the / <alpha-value> format:

module.exports = {
  theme: {
    extend: {
      colors: {
        primary: 'hsl(var(--primary) / <alpha-value>)',
      },
    },
  },
}

4. Verify Component and CSS Integration

  • Make sure globals.css is correctly imported in your app/layout.tsx:
import '../styles/globals.css'
  • Ensure your component files are within the directories specified in the content array of tailwind.config.ts:
content: [
  './pages/**/*.{ts,tsx}',
  './components/**/*.{ts,tsx}',
  './app/**/*.{ts,tsx}',
  './src/**/*.{ts,tsx}',
],

Additional Tips

  • Use developer tools to inspect the computed styles and confirm if the custom properties are applied correctly.
  • Restart your development server after modifying the configuration to ensure the changes take effect.

FAQs

Why is the opacity not working on my custom colors?

Ensure your HSL values use spaces instead of commas and update your Tailwind config to use hsla for colors.

Can I apply opacity directly in CSS instead of Tailwind?

Yes, you can use background-color: hsla(var(--primary), 0.9); directly in your styles if necessary.

Do I need to update Tailwind if I already use HSL colors?

Yes, if you're facing issues, updating to hsla and ensuring correct formatting in globals.css can resolve opacity-related problems.


By following these steps, you can effectively resolve background opacity issues in Tailwind CSS when using ShadCN UI, ensuring your components look and function as intended.