How to use @next/font globally

how-to-use-@next/font-globally

The @next/font package can download Google Fonts at build time and self-host them as a static asset. This is useful because no requests are made to Google by the client to request fonts used on the page.

However, it wasn’t obvious to me how to apply the font globally. This can be achieved as follows.

app.js

import "@/styles/globals.css";

import { DM_Sans } from "@next/font/google";

const dm_sans = DM_Sans({
  display: "swap",
  subsets: ["latin"],
  weight: ["400", "500", "700"],
});

export default function App({ Component, pageProps }) {
  return (
    <>
      <style jsx global>{`
        :root {
          --dm-font: ${dm_sans.style.fontFamily};
        }
      `}</style>
      <Component {...pageProps} />
    </>
  );
}

And then use the CSS variable in your stylesheet.

global.css

body {
  font-family: var(--dm-font);
}
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
text-typing-effect-using-html-css-only

Text Typing Effect Using HTML CSS Only

Next Post
passwordless.id-–-screenshots-️

Passwordless.ID – Screenshots 🖥️

Related Posts