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
assets
folder. -
Create a new file called
global.css
inside thelayouts
folder 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.js
file and add theglobal.css
file inside thecss
array.
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>