Method 1: Manually Add Google Fonts to Nuxt JS
Let's say you want a font called "Roboto", to add this font you can select the font styles from the Google Font website and copy the link. For example below is the Roboto font with a weight of 300, 400, 500, and, 700.
https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap
{ head: { link: [ { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }, { rel: 'stylesheet', href: 'httpshttps://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap&display=swap' } ], }
<style> body { font-family: 'Roboto', sans-serif; } </style>
Method 2: Using nuxt/google-fonts Package
The send method is to use the nuxt/google-fonts package provided by the community. The package providers the following by default:
- Specify fonts by name/variant
- Parse head links to Google Fonts
- Creates only an external link to Google Fonts
- Support CSS API v2
- Add dns-prefetch
- Add preconnect
- Add preload
- Download css/fonts to local project (No need external resources)
yarn add --dev @nuxtjs/google-fonts npm install --save-dev @nuxtjs/google-fonts
{ buildModules: [ '@nuxtjs/google-fonts' ], }
googleFonts: { families: { Roboto: true, 'Josefin+Sans': true, Lato: [100, 300], Raleway: { wght: [100, 400], ital: [100] }, } }
Leave a reply