Adding Google Fonts to your NuxtJS site
Some time ago I found out that some of my fonts werenāt loading in some browsers. I was using Google Fonts imported in my stylesheet using @import
. I couldnāt immediately pinpoint the issue, so I searched for an alternative way to add the fonts I needed to my Nuxt site.
@nuxtjs/google-fonts
In my search on Google Fonts in Nuxt, I almost immediatly found out about the Nuxt module called @nuxtjs/google-fonts. It works like a charm and is very versatile. Hereās a little guide on how you can use it.
Installing the module
Installing a module in Nuxt is the easiest thing youāll come across. Itās nothing more than a simple NPM package install. Hereās how you can install the google-fonts module:
yarn add -D @nuxtjs/google-fonts
After the install, weāll add the module to our nuxt.config.js
file:
// nuxt.config.js
{
buildModules: [
'@nuxtjs/google-fonts'
],
}
That should work!
Adding fonts
Adding fonts to your NuxtJS configuration is very easy. You just have to add them in the nuxt.config.js
file. Thereās a lot of configurable parts included with the module, but to keep things easy, Iāll only go into the basics here.
Every option or font is added via the googleFonts
property in nuxt.config.js
To add a font to your config. Just add the name to the module in nuxt.config.js
:
// nuxt.config.js
googleFonts: {
families: {
// a simple name
Roboto: true,
// a name with spaces
'Josefin+Sans': true,
// specific font weights
Lato: [100, 300],
}
}
If you need a little more advanced fonts, like italic or bold, thereās a specific property:
// nuxt.config.js
googleFonts: {
families: {
// basic
Lato: [100, 300],
// advanced
Raleway: {
// weights
wght: [100, 400],
// italic
ital: [100]
},
}
}
Using fonts in CSS
After installing and configuring the module and adding the fonts. The fonts are just usable in your CSS.
Keep in mind that the font you specify in the CSS-file should ofcourse be installed first via the nuxt.config.js
file.
p {
font-family: Rubik, sans-serif;
font-weight: 400;
}
Using with TailwindCSS
Since Iām using TailwindCSS on my website, I also had to find out how to use the fonts in my custom Tailwind configuration. Turns out I just had to add it by using simple old skool CSS, since thereās no way (yet) to add it in an @apply
rule.
p.title {
font-family: Rubik, sans-serif;
@apply text-lg text-center text-black font-semibold;
}
More info is available on the Official Documentation of the module.
Written by Elian Van Cutsem
← Back to blog