by Sai gowtham

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

  1. Open the nuxt app in your favorite code editor.
  2. Download the fonts locally and place them inside the assets folder.
  3. Create a new file called global.css inside the layouts folder and include the downloaded font by referencing the path.
global.css
@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.

  1. Navigate to the nuxt.config.js file and add the global.css file inside the css array.
nuxt.config.js
css: [
    "~layouts/global.css",
],
  1. 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.

nuxt.config.js
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.

index.vue
<style>
html {
   font-family: "Oxygen";
}
</style>

or you can include it to all pages, by adding it to the layouts/default.vue file.

layouts/default.vue
<style>
html {
  font-family: "Oxygen";
}
</style>

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course