6

So I'm building a website with NuxtJS using Tailwind CSS for my styles. I'm using the @nuxtjs/tailwindcss module.

The issue is that my fonts don't seem to be loading on the browser. The correct font-family is still applied by the CSS as you can see in the devtools screenshot, but the browser still renders my text with Times New Roman.

--Devtools Screenshot

My fonts files are .ttf files stored in a /assets/fonts/ folder in my project's root directory.

My tailwind.css file looks like this

@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

@font-face {
  font-family: 'Montserrat';
  font-weight: 400;
  src: url('../fonts/Montserrat-Regular.ttf') format('ttf');
}

@font-face {
  font-family: 'Montserrat';
  font-weight: 700;
  src: url('../fonts/Montserrat-Bold.ttf') format('ttf');
}

@font-face {
  font-family: 'Montserrat';
  font-weight: 900;
  src: url('../fonts/Montserrat-Black.ttf') format('ttf');
}

and my tailwind.config.js looks like this

module.exports = {
  theme: {
    fontFamily: {
      sans: ['Montserrat'],
      serif: ['Montserrat'],
      mono: ['Montserrat'],
      display: ['Montserrat'],
      body: ['Montserrat']
    },
    // Some more irrelevant theme customization
 },
  variants: {},
  plugins: []
}

I wanted to completly override Tailwind's base fonts so I didn't use extend and I plan on cleaning this up and using an other font for some texts once I figure out how to properly do this.

My guts tell me that Tailwind is not the problem here since the Devtools actually show Montserrat as the computed font, and the webpack build does not throw any error.

I've tried both answers featured in this related question, the accepted one actually being my implementation, but no good result so far.

I'd be very grateful if somebody could help me !

EDIT : I created a Github repo reproducing the issue, it can be found here and all steps to reproduce are in the README.MD

Kerunix
  • 113
  • 1
  • 1
  • 7
  • Is font in production directory? Is loaded by browser? – chojnicki May 23 '20 at 20:11
  • As explained in my post (maybe nor clearly enough, my bad then), my font files are located in `/assets/fonts` at the root of my project directory. I don't know how to check if the fonts are actually loaded by the browser, all I know is that my screenshot shows that the right font shows in the "computed" panel but the browser still renders using Times New Roman, which would lead me to believe that the font is actually not loaded. – Kerunix May 23 '20 at 20:32
  • 1. I believe you are talking about sources root, but I'm asking about files after build (dist directory) to check if webpack for some reason is ignoring them. 2. You can check if files are loaded in browser in Network tab in devtools. 3. Build app and check in css source if there still are @font-face with your font present. 4. It would be best if You could provide some demo in any sandbox. – chojnicki May 23 '20 at 20:39
  • Oh my bad, I though you where talking about font sources. I have no CSS in my build output though, just two folders (client and server, seems normal using Nuxt), and the client one contains a fonts folder containing my built font. I'm a bit new to server side rendering stuff, especially in dev mode, but I'm a bit surprised to see that my network tab shows no download of either a CSS stylesheet or a font file . I'll try to put up some demo but I don't know of any sandbox that allows to recreate a SSR environment. I'll provide a github link soon. Thx for the help ! – Kerunix May 23 '20 at 21:08
  • codesandbox.io I think fastest way is to find any existing nuxt demo and just change fonts like you did. But git source will do too. – chojnicki May 23 '20 at 21:10
  • I updated my question with a link to the Github repo. Contains the bare minimum needed. NuxtJS app serving a single index.vue file with TailwindCSS for styles, loading custom fonts. – Kerunix May 23 '20 at 21:26
  • Ok I will answer soon – chojnicki May 23 '20 at 21:48

1 Answers1

16

It is not an issue with Tailwind, Vue, or Nuxt - just in CSS.

You have wrong the format value in @font-face src. "ttf" is an extension, not a format name. It should be "truetype" instead. Actually, for woff or svg, the extension is the same as the format name, so that's why it can be confusing with "ttf" and "truetype".

So just replace:

src: url('../fonts/Montserrat-Regular.ttf') format('ttf');

With:

src: url('../fonts/Montserrat-Regular.ttf') format('truetype');

Or remove format because it will work without it.

src: url('../fonts/Montserrat-Regular.ttf');

WOFF

Also, it would be a good idea to use newer and smaller formats: woff and woff2.

src:
  url('../fonts/Montserrat-Regular.ttf') format('truetype'),
  url('../fonts/Montserrat-Regular.woff2') format('woff2'),
  url('../fonts/Montserrat-Regular.woff') format('woff')

I personally use only woff and woff2, since it is supported in all major browsers. Based on caniuse coverage, for now is > 98%, so in my opinion, there is no reason to use TTF anymore.

src:
  url('../fonts/Montserrat-Regular.woff2') format('woff2'),
  url('../fonts/Montserrat-Regular.woff') format('woff')
tony19
  • 125,647
  • 18
  • 229
  • 307
chojnicki
  • 3,148
  • 1
  • 18
  • 32
  • I'm actually not suprised this was so simple... I am confused because I basically used the same 'ttf' format on an angular project of mine which worked perfectly fine. Thanks for the woff advice as well, I'll take a look at it ! Anyway, this works. Thank you very much for your help ! – Kerunix May 23 '20 at 22:01
  • If "ttf" worked in other project then maybe font that was used in project was actually installed locally in system, so font-face was not used and you could missed it. Also it would work without using "format('xxx')". – chojnicki May 23 '20 at 22:05
  • 1
    PS if you are planning to use this repo, you have missing @nuxtjs/apollo dependency in package.json – chojnicki May 23 '20 at 22:08
  • I'm not planning on using the repo, it's just a copy of the main one. I tried to remove as much dependencies as possible to make it easier on you. Guess this one sliped through. Thanks again. – Kerunix May 23 '20 at 22:12