Add custom font with tailwind plugin
Hello guys, i'm struggling for 4 hours now on trying to import custom fonts on my project.
I try to add the font with a tailwind plugin (so i can just copy the plugin and change the fonts on my diferents projects)
But i can't make it work, the font class is created, but the font familly is not recognized.
I added all woff and woff2 files in my /asset/fonts/font_name/ folder
also i defined my font in tw theme:
then i tried several methods to add the font.
in the theme:
didn't worked,
with css :
didn't worked too.
I'm lost, it's patethic to be lost on "Font customization"
5 Replies
Hi @Hugo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To add a custom font with Tailwind CSS in a Nuxt 3 project, you can follow these steps:
1. First, place your font files in the
public/fonts
directory of your Nuxt project. This ensures they are accessible as static assets.
2. Create a CSS file (e.g., assets/css/fonts.css
) to define your custom font:
3. In your nuxt.config.ts
file, add this CSS file to the global styles:
4. In your Tailwind configuration file (tailwind.config.js
), extend the font family:
5. Now you can use the font in your components with the font-primary
class:
This approach is based on the information from Nuxt documentation on styling (https://nuxt.com/docs/getting-started/styling#working-with-fonts) and Stack Overflow post on changing default Tailwind font-family in Nuxt 3 (https://stackoverflow.com/questions/77970841).
Make sure your font files are in the correct location and that the paths in your CSS @font-face
declaration are correct. The key is to use the public
directory for static assets like fonts in Nuxt 3.
If you're still having issues, double-check that your Tailwind and Nuxt configurations are correct and that there are no conflicting styles overriding your font declarations.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@122807566475591680> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Didn't worked @kapa.ai
Let me check the knowledge base for you again. One moment! :books:
----------------------
I apologize that the previous solution didn't work for you. Let's try to troubleshoot this issue further. Since you mentioned that the font class is created but the font family is not recognized, there might be a few things we can check:
1. First, make sure your font files are in the correct location. In Nuxt 3, they should be in the
public/fonts
directory, not in assets/fonts
as mentioned in the Nuxt documentation (https://nuxt.com/docs/getting-started/styling#working-with-fonts).
2. Double-check your @font-face
declaration. It should look something like this:
Note the path starts with /fonts/
instead of ./assets/fonts/
.
3. Make sure this CSS is loaded globally. You can do this by adding it to your nuxt.config.ts
file:
4. In your Tailwind configuration, make sure you're extending the theme correctly:
This will set 'Space Grotesk' as your default sans-serif font.
5. If you're using the @nuxtjs/tailwindcss
module, make sure it's properly registered in your nuxt.config.ts
:
If you've done all of these steps and it's still not working, there might be an issue with how the font is being loaded or applied. In that case, it would be helpful to check your browser's developer tools to see if the font files are being loaded correctly and if there are any CSS conflicts.
If the problem persists, it might be worth reporting it as a potential bug (https://nuxt.com/docs/community/reporting-bugs) to the Nuxt community, as there could be an underlying issue with how fonts are being handled in your specific setup.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@122807566475591680> Kapa.ai is still learning and improving, please let me know how I did by reacting below.