Ask Your Question
0

How can custom fonts be loaded in Next.js?

asked 2023-04-29 09:03:32 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-29 09:14:01 +0000

plato gravatar image

To load custom fonts in Next.js, you can follow these steps:

  1. Place your font files (e.g. .woff, .woff2) into the 'public' directory of your Next.js project.

  2. Create a CSS file (e.g. 'fonts.css') and import your font files using @font-face. For example:

@font-face { font-family: 'MyFont'; src: url('/fonts/myfont.woff2') format('woff2'), url('/fonts/myfont.woff') format('woff'); }

  1. In your Next.js component, import the CSS file using the 'import' statement.

import '../styles/fonts.css';

  1. Use the font in your styles. For example:

h1 { font-family: 'MyFont', sans-serif; }

  1. Done! Your custom font should now be loaded in your Next.js app.

Note: If you are using a CSS-in-JS library like styled-components, you can still follow the same steps above, but modify step 2 to include the @font-face declaration in your styled-component.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-29 09:03:32 +0000

Seen: 16 times

Last updated: Apr 29 '23