How to add fonts to a nuxt app
In this tutorial, we are going to learn about how to add custom fonts to a nuxt app that is created using create-nuxt-app.
Adding local fonts
-
Open the nuxt app in your favorite code editor.
-
Download the fonts locally and place them inside the
assetsfolder. -
Create a new file called
global.cssinside thelayoutsfolder and include the downloaded font by referencing the path.
@font-face {
font-family: "Oxygen";
src: local("Oxygen"), url(~assets/Oxygen/Oxygen-Regular.ttf) format("truetype");}
html {
font-family: "Oxygen";
}In the above code, I have added an Oxygen font.
- Navigate to the
nuxt.config.jsfile and add theglobal.cssfile inside thecssarray.
css: [
"~layouts/global.css",
],- Now, this font is used by all pages in our nuxt app.
Adding Google fonts
We can also use google fonts (API) instead of local fonts by adding it to the link array inside our nuxt.config.js file.
export default {
head: {
link: [
{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
{ rel: "stylesheet", href: "https://fonts.googleapis.com/css2?family=Oxygen&display=swap", }, ],
},
}Now, you can use this font inside your nuxt pages like this.
<style>
html {
font-family: "Oxygen";
}
</style>or you can include it to all pages, by adding it to the layouts/default.vue file.
<style>
html {
font-family: "Oxygen";
}
</style>

