Nuxt UI v3: Custom theme colours
In the latest Nuxt UI v3, how can I add custom colours to be used as
primary
and secondary
that are not TailwindCSS colours?
What I mean is that I want to define a palette different from TailwindCSS palette.8 Replies
Hi @AerodynamicPotato. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Nuxt UI v3, you can define custom colors for your theme that differ from the default Tailwind CSS palette. There are two main approaches to achieve this:
Using CSS Variables with @theme Directive
With Nuxt UI v3, you can use the CSS-first approach with the
@theme
directive in your CSS file:
This approach is recommended in Nuxt UI v3 as mentioned in the UI Pro Docs Migration guide.
Setting Primary and Secondary Colors
After defining your custom colors, you can set them as your primary and secondary colors in your app.config.ts
:
Note that in Nuxt UI v3, the configuration structure has changed from v1. The colors are now defined under ui.colors
instead of directly under ui
as shown in the UI Pro Docs Migration guide.
Important Considerations
When creating custom colors, make sure to:
1. Define all shades from 50
to 950
as most of them are used in the components configuration.
2. You can use tools like uicolors.app to generate a complete color palette.
3. Avoid naming conflicts - don't use primary
as a color name in your Tailwind config as it would conflict with the module-defined primary color.
The colors you define can then be used in your components with classes like text-primary-500 dark:text-primary-400
or the shorthand text-primary
which automatically adapts to light/dark mode.
Remember that your custom colors need to be properly safelisted if you're using them dynamically, as explained in the Smart Safelisting documentation.
Let me know if you need more specific guidance on implementing your custom color palette!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@336887743366037504> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai
I added the following colours to my
@theme
in main.css
And configured my app.config.ts
for ui
to use fun-blue
as a primary colour, but it's not working, and the CSS variables are not defined when running the project.
Let me check the knowledge base for you again. One moment! :books:
----------------------
I see the issue with your custom colors implementation. There are a couple of things that need to be fixed to make your custom colors work properly in Nuxt UI v3.
Correct @theme Implementation
Your CSS variables look correct, but the
@theme
directive needs to properly wrap them. Here's how your main.css
should be structured:
Make sure you have the imports and that the CSS variables are properly wrapped in the @theme
directive.
CSS Import in Nuxt Config
Ensure that your CSS file is properly imported in your nuxt.config.ts
:
App Config Structure
For Nuxt UI v3, your app.config.ts
structure looks correct, but there's a slight difference in how colors are configured compared to v1. Here's the correct format:
Safelist Custom Colors
You might also need to safelist your custom colors in your nuxt.config.ts
:
This ensures that Tailwind generates the necessary CSS classes for your custom colors, as explained in the Smart Safelisting documentation.
Additional Troubleshooting
If you're still having issues:
1. Make sure your CSS file is in the correct location (typically in assets/css/main.css
)
2. Check that your imports are correct and that the file is being loaded
3. Verify that there are no errors in your browser console
4. Try clearing your browser cache or restarting your Nuxt development server
These steps should help you get your custom colors working properly in Nuxt UI v3.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@336887743366037504> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai the missing thing was the
static
keyword in main.css
as follows:
Let me check the knowledge base for you again. One moment! :books:
----------------------
Thank you for sharing the solution! You're absolutely right - the
static
keyword is crucial when defining custom colors in Nuxt UI v3 with the @theme
directive.
The correct syntax for defining custom colors in your CSS file should be:
The static
keyword in the @theme
directive tells Tailwind CSS that these are static theme values that should be available at build time. Without this keyword, the custom color variables might not be properly recognized by Tailwind CSS when generating the necessary utility classes.
This is an important detail when working with custom colors in Nuxt UI v3, as it ensures your custom color palette is properly integrated with the Tailwind CSS system. Thanks for sharing this solution, as it will likely help others who are trying to implement custom colors in their Nuxt UI v3 projects!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@336887743366037504> Kapa.ai is still learning and improving, please let me know how I did by reacting below.