Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading custom fonts in Nuxt/Tailwind Project

Hi everybody and sorry my english.

I have created a nuxt.js project with Tailwind. I´d like to custom my font family, so I downloaded some font files from Google Fonts. I have been reading Tailwind docs, but i can´t understand where do i have to place the font files and how to config Tailwind for loading the files.

I´d be very gratefull if somebody could help me.

like image 412
Grijander Avatar asked Apr 25 '20 11:04

Grijander


People also ask

How do I use custom fonts in tailwind?

Using the font-face rule, we specify the font-family value of our custom font and then specify the path to the font file in our project via the src property. And that's it! We are now using a locally installed font in the project.

How do I add fonts to Tailwind config?

Visit Google Fonts and search for the specific font you like. Select the style variants from thin (100) to bold (900). Copy the import statement in between the style tags. Import the new font in the CSS file where you import tailwind classes, i.e., styles.


1 Answers

I'm assuming you're using the module @nuxtjs/tailwindcss.

  1. First, you'll have to run npm run build, so that tailwind files are created:

    • ~/tailwind.config.js
    • ~/assets/css/tailwind.css
  2. Create a folder fonts under assets and place the fonts you've downloaded.

  3. Include your fonts in ~/css/tailwind.css, as such:

@include font-face( KapraNeuePro, '~/assets/fonts/KapraNeueProFamily/Kapra-Neue-Pro-Regular', 400, normal, otf);
@include font-face( KapraNeuePro, '~/assets/fonts/KapraNeueProFamily/Kapra-Neue-Pro-Medium', 600, medium, otf);

etc.

  1. Check out tailwind's docs on how to add font families in tailwind.config.js, and which configuration better suits your needs: (the following one is a quick working example)
module.exports = {
  theme: {
    fontFamily: {
      sans: ["KapraNeuePro"],
      serif: ["KapraNeuePro"],
      mono: ["KapraNeuePro"],
      display: ["KapraNeuePro"],
      body: ["KapraNeuePro"]
    },
    variants: {},
    plugins: []
  }
};
  1. Dont' forget to remove from your layout and page all the default CSS related to font-family
like image 91
ramigs Avatar answered Oct 20 '22 19:10

ramigs