Skip to content

Commit 0a2ae76

Browse files
author
Hannes Bornö
authored
docs: Add examples of using multiple weights and styles (vercel#43031)
Add examples of using multiple weights and styles for google and local fonts. Although it's in the [API reference](https://nextjs.org/docs/api-reference/next/font) it's probably a good idea to have examples since it's a common use case. Also added subets to google font examples to make them more 'copayable'. cc @leerob ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 397894b commit 0a2ae76

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

docs/basic-features/font-optimization.md

+47-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ To use the font in all your pages, add it to [`_app.js` file](https://nextjs.org
3333
import { Inter } from '@next/font/google'
3434

3535
// If loading a variable font, you don't need to specify the font weight
36-
const inter = Inter()
36+
const inter = Inter({ subsets: ['latin'] })
3737

3838
export default function MyApp({ Component, pageProps }) {
3939
return (
@@ -52,6 +52,7 @@ import { Roboto } from '@next/font/google'
5252

5353
const roboto = Roboto({
5454
weight: '400',
55+
subsets: ['latin'],
5556
})
5657

5758
export default function MyApp({ Component, pageProps }) {
@@ -63,6 +64,16 @@ export default function MyApp({ Component, pageProps }) {
6364
}
6465
```
6566

67+
You can specify multiple weights and/or styles by using an array:
68+
69+
```js
70+
const roboto = Roboto({
71+
weight: ['400', '700'],
72+
style: ['normal', 'italic'],
73+
subsets: ['latin'],
74+
})
75+
```
76+
6677
#### Apply the font in `<head>`
6778

6879
You can also use the font without a wrapper and `className` by injecting it inside the `<head>` as follows:
@@ -71,7 +82,7 @@ You can also use the font without a wrapper and `className` by injecting it insi
7182
// pages/_app.js
7283
import { Inter } from '@next/font/google'
7384

74-
const inter = Inter()
85+
const inter = Inter({ subsets: ['latin'] })
7586

7687
export default function MyApp({ Component, pageProps }) {
7788
return (
@@ -95,7 +106,7 @@ To use the font on a single page, add it to the specific page as shown below:
95106
// pages/index.js
96107
import { Inter } from '@next/font/google'
97108

98-
const inter = Inter()
109+
const inter = Inter({ subsets: ['latin'] })
99110

100111
export default function Home() {
101112
return (
@@ -156,6 +167,35 @@ export default function MyApp({ Component, pageProps }) {
156167
}
157168
```
158169

170+
If you want to use multiple files for a single font family, `src` can be an array:
171+
172+
```js
173+
const roboto = localFont({
174+
src: [
175+
{
176+
path: './Roboto-Regular.woff2',
177+
weight: '400',
178+
style: 'normal',
179+
},
180+
{
181+
path: './Roboto-Italic.woff2',
182+
weight: '400',
183+
style: 'italic',
184+
},
185+
{
186+
path: './Roboto-Bold.woff2',
187+
weight: '700',
188+
style: 'normal',
189+
},
190+
{
191+
path: './Roboto-BoldItalic.woff2',
192+
weight: '700',
193+
style: 'italic',
194+
},
195+
],
196+
})
197+
```
198+
159199
View the [Font API Reference](/docs/api-reference/next/font.md#nextfontlocal) for more information.
160200

161201
## With Tailwind CSS
@@ -190,7 +230,10 @@ const { fontFamily } = require('tailwindcss/defaultTheme')
190230

191231
/** @type {import('tailwindcss').Config} \*/
192232
module.exports = {
193-
content: ['./app/**/*.{js,ts,jsx,tsx}'],
233+
content: [
234+
'./pages/**/*.{js,ts,jsx,tsx}',
235+
'./components/**/*.{js,ts,jsx,tsx}',
236+
],
194237
theme: {
195238
extend: {
196239
fontFamily: {

0 commit comments

Comments
 (0)