Using Fonts in Next.js (Google Fonts, Local Fonts, Tailwind CSS)

Поделиться
HTML-код
  • Опубликовано: 9 янв 2025

Комментарии • 223

  • @mmsrp
    @mmsrp Год назад +53

    On the OFFICIAL DOCS they point to this, then: *Your project has `@next/font` installed as a dependency, please use the built-in `next/font` instead.*. Please update the docs or pin an updated comment here.

    • @leerob
      @leerob  2 месяца назад +2

      Sorry about this! The pains of recording point-in-time videos. Hopefully that error message got you down the right path, but pinning this comment for others. Maybe I can re-record at some point.

  • @caleblovell
    @caleblovell 2 года назад +6

    Wow, this looks amazing. I hate seeing the CLS from font fallbacks on sites. Now I want to upgrade to Next 13 just for this!

  • @maxproske1950
    @maxproske1950 2 года назад +24

    Thanks Lee for putting out these high value-per-square-minute videos. Getting to watch in-depth explanations of new and experimental features early makes me feel part of the Next.js community. Random question, can `@next/font/local` be used in combination with `turbo`?

    • @leerob
      @leerob  2 года назад +3

      I believe if you start from `npx create-next-app@latest --example with-turbopack` and then add `@next/font/local`, it should work - I haven't verified myself, though. Note this uses Tailwind CSS.

  • @satindar31
    @satindar31 Год назад +19

    Just a heads up, if you created the project with the latest flag, next font is automatically installed so no need to run npm i @next/font :)

  • @mikesoertsz222
    @mikesoertsz222 Год назад +16

    What about if we want to load a secondary font as well? I've not seen an explanation of that anywhere. Good design usually has 2 brand fonts + web-safe fonts.

    • @skyhigheagleer6
      @skyhigheagleer6 10 месяцев назад

      Load them in without next/font and use your own preload tags

    • @chrisauret3785
      @chrisauret3785 6 месяцев назад +1

      I had to add all fonts to the html tag as below
      // poppins was with "next/font/google"
      // rubrik was with "next/font/local"
      /* NextJS14 - layout.tsx*/
      return (
      //

  • @hrazith
    @hrazith 3 месяца назад

    Love the pace. You get to the point! Thank you.

  • @nbo304
    @nbo304 Год назад +10

    This is great but how do i use several different fonts? for example, the title of my hero section uses a different font than the rest of the app. Every tutorial talks about only applying one font to the entire page.

    • @zenovak5177
      @zenovak5177 9 месяцев назад +1

      Dont apply fonts to whole app at the app page via the main tag, instead, import them as the tutorials shown, and apply them selectively on your choosen elements.

    • @chrisauret3785
      @chrisauret3785 6 месяцев назад

      I had to add all fonts to the html tag as below
      // poppins was with "next/font/google"
      // rubrik was with "next/font/local"
      /* NextJS14 - layout.tsx*/
      return (
      //

  • @faizanahmed9304
    @faizanahmed9304 2 года назад +2

    Thank you Lee for in depth explanation!

  • @charliegunthers
    @charliegunthers Год назад +1

    thanks. i felt like an absolute dummy trying to do this reading from the nextjs docs

  • @doylemichael
    @doylemichael Год назад +11

    So how do you add multiple fonts to an app directory? I want to have two different google fonts globally accessible on my site

    • @venicebeachsurfer
      @venicebeachsurfer 11 месяцев назад +10

      ... and of course, no reply from Lee. It's like these NextJS guys think everyone builds hello world apps....

    • @KhourySmith
      @KhourySmith 4 месяца назад

      @@venicebeachsurfer lol

    • @mdrubautreshedchowdhury5788
      @mdrubautreshedchowdhury5788 3 месяца назад

      @@venicebeachsurfer

  • @k2p11
    @k2p11 2 года назад +1

    Wow, thanks this video, Lee! So helpful.

  • @codinginflow
    @codinginflow Год назад +1

    Is there any estimate when the new app directory will become stable and the default?

  • @EliteFailure
    @EliteFailure Год назад +3

    For someone who just started learning next: I don't get what exactly is considered being simple here, or what the real advantage is?! Classic local hosting of the the fonts is easy, and specifying a base font in the CSS (especially with a CSS framework like tailwind or bootstrap) is way more convenient than this orgy. What am I missing?

  • @joseandkris
    @joseandkris Год назад +4

    The only thing that bothers me here is 2x `main` tag, suggesting non-semantic html. + extra html uneeded tag. Also, what if I need 2 or more fonts loaded?

  • @Chris-hr8lt
    @Chris-hr8lt 9 месяцев назад

    the tailwind part was so hard to find, thanks a lot for the video

  • @TeleRocker11
    @TeleRocker11 Год назад

    Thanks Lee, another reason to love Next.js!

  • @hynekss8618
    @hynekss8618 Год назад

    This is neat! It works like a charm right off the bat.

  • @Morimove
    @Morimove 10 месяцев назад

    i landed here from official next js documentaion. btw i already watched it and again i am here.

  • @ErikKoning-t7p
    @ErikKoning-t7p 5 месяцев назад

    Thanks for explaining the tailwind css bit

  • @hrishikesh85
    @hrishikesh85 3 месяца назад

    Thanks a lot! This was much-needed

  • @_ash64
    @_ash64 Год назад

    Thank you so much, this is a very quality channel!

  • @ajamesdev
    @ajamesdev 2 года назад +3

    What is the best way to inject multiple fonts? Should I concatenate the fonts with the classNames library, or inject them as individual jsx global styles?

    • @joseandkris
      @joseandkris Год назад +1

      Have you found a way to do this?

  • @mr.random8447
    @mr.random8447 2 года назад +3

    Isn't self-hosting bad for performance? Pretty much if we use @next/font we have to deploy on Vercel to get that CDN performance?

  • @official.mhm13
    @official.mhm13 2 года назад

    Thanks for detailed explanation Lee Rob ❤️

  • @Kap1tany
    @Kap1tany 2 года назад +12

    Can you load multiple fonts with @next/font?

    • @jrbprice
      @jrbprice 2 года назад +1

      I would like to know this too

    • @ordruker9675
      @ordruker9675 2 года назад +1

      Same question

    • @Christomshack
      @Christomshack 2 года назад +1

      Also curious

    • @maciejwojda2774
      @maciejwojda2774 Год назад

      yes, I am also curious about adding more than one font family

    • @akhamr_
      @akhamr_ Год назад +1

      The answer is yes

  • @raylopro
    @raylopro 2 месяца назад +2

    Starts at 2:29

  • @icewolfy1
    @icewolfy1 2 года назад +5

    Lets say only certain components need this font, how would you reference the font family from within those components? Lets say you are using sass, index.module.scss. The font-family name generated seems to include some numbers so I'm not sure how to use it from certain components

    • @codinginflow
      @codinginflow Год назад +1

      From the docs:
      -Call the font loader function in one shared file
      -Export it as a constant
      -Import the constant in each file where you would like to use this font
      I guess React context would also work

  • @alifranco2528
    @alifranco2528 Год назад

    my english is not the best but i could understand your explanation, really thanks

  • @josepedro3135
    @josepedro3135 Год назад

    Awesome video! Helped a lot, Thank you!

  • @_Faisal_Akbar
    @_Faisal_Akbar 2 года назад +5

    What if I have multiple local font with different font style and different weight ?
    My font css:
    @font-face {
    font-family: "droid-serif";
    font-display: auto;
    font-style: normal;
    font-weight: 400;
    font-stretch: normal;
    src: url(/fonts/DroidSerif.woff2) format("woff2");
    }
    @font-face {
    font-family: "droid-serif";
    font-display: auto;
    font-style: normal;
    font-weight: 700;
    font-stretch: normal;
    src: url(/fonts/DroidSerif-Bold.woff2) format("woff2");
    }
    @font-face {
    font-family: "droid-serif";
    font-display: auto;
    font-style: italic;
    font-weight: 400;
    font-stretch: normal;
    src: url(/fonts/DroidSerif-Italic.woff2) format("woff2");
    }
    @font-face {
    font-family: "droid-serif";
    font-display: auto;
    font-style: italic;
    font-weight: 700;
    font-stretch: normal;
    src: url(/fonts/DroidSerif-BoldItalic.woff2) format("woff2");
    }

    • @keeganburkett7252
      @keeganburkett7252 2 года назад +2

      Same question here, currently trying to figure out how to handle two local fonts with different weights w/ Tailwind. Familiar with the old way (prior to Next 13) but it's unclear how to do this in Next 13

  • @noureddinbakir6065
    @noureddinbakir6065 2 года назад

    Love your work man, keep it up :)

  • @tamilshoutcom
    @tamilshoutcom 2 года назад +3

    If this is for nextjs 13, why set this inside _app.tsx file? Shouldn't it be under layout.jsx at the top level inside the app folder?
    It is not working if I do that btw, resorts to using default font thus asking. Thanks.

    • @amandakoster
      @amandakoster Год назад

      I am using nextjs 13 and this video does not offer a complete tutorial. My fonts loaded but all of my globals broke.

    • @Poolito254
      @Poolito254 2 месяца назад

      @@amandakoster did you fix it?

  • @Gelliny
    @Gelliny Год назад

    Thank you, this was really helpful.

  • @petter9078
    @petter9078 2 года назад

    I love your channel. Ended up so many times back here!

  • @whyfoo
    @whyfoo Год назад

    well explained ! awesome feature!!

  • @keenanreed5341
    @keenanreed5341 Год назад

    Simple and to the point. Thanks!

  • @henriqpohl
    @henriqpohl Год назад +1

    Hi, helpful video, but I still have a problem, when I declare everything in my "layout.tsx" I cant use in another pages so I need to redeclare again, thats correct?

  • @miladafkhamipour5106
    @miladafkhamipour5106 2 месяца назад

    I have a custom font for my website. Should I upload the font files on a cloud storage with CDN cache and a life-time TTL(loaded by good old CSS) or shall I put the font files in the public folder and use next/font/local?

  • @ICOReviewtoken
    @ICOReviewtoken Год назад +1

    Hi bro. I have a custom font that I want to import locally but it doesn't accept it. Also if I want to give users the ability to choose their own fonts when using, for example users will set the font for different title tags and font base, can I do it in nextjs 13? Please give me solution. Thank you

  •  Год назад +2

    I am missing explanations on how to use several fonts in styling and how do we add them to tailwind and use them wherever we want.

  • @acidhauss7018
    @acidhauss7018 Год назад

    Awesome video, thanks for the upload

  • @fiorins
    @fiorins Месяц назад

    how can I update the google font package cause eg for some fonts I have the error "Module '"next/font/google"' has no exported member 'xxx'.ts(2305)"

  • @Jack-ss4re
    @Jack-ss4re Год назад +1

    cool! but isnt CDN faster than hosting the font files in our servers?

    • @notstevelam
      @notstevelam Год назад

      yah i dont get it. this just seems like a ridiculous, convoluted away to essentially host a google font locally, since this method apparently downloads the font into your project anyway?

    • @Jack-ss4re
      @Jack-ss4re Год назад

      @@notstevelam ill search for a way to import fonts from cdn and using Next fonts as fallback, seems to be the best approach

    • @misterbean6674
      @misterbean6674 5 месяцев назад

      @@Jack-ss4re hey! got an optimal approach for that?

  • @wd8fk2iq8l
    @wd8fk2iq8l Год назад

    I was meesing my head around why my font was serif. Then I saw this quick additions of "font-sans" after the curly brackets :P

  • @sjc5
    @sjc5 2 года назад

    After looking through the docs, it looks like Arial and Times New Roman are the only fallback options? Courier for monospace would be nice as well.

  • @appstuff6565
    @appstuff6565 Год назад

    Thanks for this Lee!

  • @DomBarker
    @DomBarker 10 месяцев назад

    thanks! was having issues with mobile safair but this fixed

  • @phucnguyen0110
    @phucnguyen0110 2 года назад +1

    Cool glasses, Lee xD
    And yes, I was able to implement the same thing here on the video through reading the docs - really lovely to have the native support from Next for sure.
    Not really related to Next 13 - but is there a plan to support Analytics out of the box for other frameworks than Next/Nuxt/Gastby as of right now?

  • @metink516
    @metink516 Год назад

    hi thank you for how to video.
    I want to ask. Why didn't they put the next/font install code in the documentation?

  • @isseihyoudou5522
    @isseihyoudou5522 Год назад

    if not specify a subset it will use all of them? or there's a default? and same question for the weights

  • @georgeranch31
    @georgeranch31 Год назад

    Don't know if it's gonna work but Ill try it on my next js 12 project.
    Can upgrade yet. AWS amplify does support 13 yet

  • @mmd1dev
    @mmd1dev Год назад

    Great Lee thanks for the video 👏👏

  • @miletadulovic577
    @miletadulovic577 2 года назад +2

    Cool, BUT, could you do the demo from 1:32 again, but instead of putting link for font in Home page, put it in _document.jsx, as where it should be. Don't do these 'fake' comparisons. Fon't wont flash on page change if you put it in _document.jsx as it will be downloaded only once and used across the project.

  • @balanceandbuild
    @balanceandbuild Год назад

    How would you go about a scenario where you're using multiple local fonts? How to pass and use multiple fonts in the child components as required?

    • @leerob
      @leerob  Год назад +1

      nextjs.org/docs/app/building-your-application/optimizing/fonts#local-fonts

    • @chrisauret3785
      @chrisauret3785 6 месяцев назад

      I had to add them all to the html element as below
      // poppins was with "next/font/google"
      // rubrik was with "next/font/local"
      /* NextJS14 - layout.tsx*/
      return (

      {children}

      );
      /* tailwind.config.ts */
      fontFamily: {
      rubrik: ["var(--font-rubrik)"],
      poppins: ["var(--font-poppins)"],
      },
      Then I could access them all (without "--font-poppins" is not defined errors)
      /* global.css */
      @tailwind base;
      @tailwind components;
      @tailwind utilities;
      body {
      @apply font-poppins;
      }
      h2 {
      @apply text-3xl font-rubrik;
      }

  • @glusa8
    @glusa8 Год назад

    Thanks for the video! So intrigued how you move your cursors seamlessly. And the cursor isn’t blinking! Are you using some kind of extension?

  • @prakashk8984
    @prakashk8984 10 месяцев назад +1

    I'm facing issue Failed to fetch `Roboto` from Google Fonts.

  • @jggabayno
    @jggabayno 2 года назад

    Does RTK still makes sense with NextJS 13 app directory? or any state management you suggest to be use?

  • @antares-the-one
    @antares-the-one Год назад

    does anyone know why all the fonts from the next 13 look fuzzy? Particularly the top part of the character. when I use Google's one by it looks perfect though...

  • @devrsion
    @devrsion Год назад

    Is there a way to inject css vairable to html body? Because all popups, like dialogs, callouts need to be rendered on the body, which is beyond the pages custom app's control

  • @itamar-jr-tech
    @itamar-jr-tech Год назад

    I'm use two fonts. We do need pass in file app and a tag main? This is a little strange.

  • @javadkh9400
    @javadkh9400 Год назад

    I want to use alternative font format in localFont in my next js app. I mean like what we do in @font-face in css that if browser doesn't support first one goes to next one. There is no example for this case In next js document and I don't know how can I do it.Can you help me?

  • @MrMazvaz
    @MrMazvaz Год назад

    How do you go about creating your own fonts using localFont? Say I was creating a designsystem and wanted to support NextJS by creating my own font using localFont and the exporting that as an npm-package the rest of my company could install and use just like the Inter function you get from `next/font/google`.
    E.g:
    `import { CompanyFont } from "@company/font"`. Is this possible?

  • @SaurabhSrivastava-i1q
    @SaurabhSrivastava-i1q Год назад

    What if the font is hosted in some other CDN? Anyway to import them without downloading it?

  • @sidehustler6729
    @sidehustler6729 Год назад

    Hi,
    I want to use @next/font but In my case font family name will come from api side or external data.
    How can I do that with new font lib. I try dynamic loading font package. But it doesn't work. Please help!

  • @shubitoxX
    @shubitoxX Год назад

    Nice, is SvelteKit support possible/planned?

  • @JuangaSdvsf
    @JuangaSdvsf Год назад

    Thanks for the explanation. What if instead of using tailwind I want to use sass?

  • @virtual5754
    @virtual5754 2 года назад

    Awesome, keep it up. We need more tutorials like this for other features.
    If my app uses variable font, but only specific weights (400, 500), is there totally no need to specify them? For when variable fonts are unsupported for some reason

    • @kozie928
      @kozie928 2 года назад

      If you only need 2 weights, it may be worth checking the network tab to see if the variable font isn't bigger than the two fixed weights. I think in most cases, sizewise, variable fonts are worth if you'd use 3+ fixed weights.

  • @NathanBudd
    @NathanBudd Год назад

    Is there a way to install just `next/font/local`? I am planning on just using my woff2 file and don't need to install all of the Google fonts.

  • @BananaCoding
    @BananaCoding 2 года назад

    Hi Lee, thanks for that video!
    Is there a way to use internationalization / i18n with the new app folder?

    • @leerob
      @leerob  2 года назад

      Hey! You can see supported/planned/unplanned features here: beta.nextjs.org/docs/app-directory-roadmap

  • @GavinMcQuienn
    @GavinMcQuienn 10 месяцев назад

    How do we assign specific fonts to specific elements, say.. font-1 assigned to h1, h3 && font-2 assigned to h2 && font-3 assigned to body? TIA

    • @GavinMcQuienn
      @GavinMcQuienn 10 месяцев назад +2

      All good, I figured it out. Pass all required fonts to the , then in your css, apply these variable names to the required elements via the related css variable you specified, eg; h1 { font-family: var(--font-fontName1), sans-serif; } h2 { font-family: var(--font-fontName2), sans-serif; }, etc. - thanks again for this tutorial, it was uber helpful 👍

  • @SolomonManalili
    @SolomonManalili Год назад

    If I have multiple local font (different weights), do I need to declare each .woff2 file per weight?

    • @leerob
      @leerob  Год назад

      nextjs.org/docs/app/building-your-application/optimizing/fonts#:~:text=If%20you%20want,be%20an%20array%3A

  • @iamprincemuel
    @iamprincemuel Год назад

    Hi, awesome tutorial. Please what's the name of this vscode editor theme? Thanks

  • @EzeBranCerati
    @EzeBranCerati Год назад +2

    it doesnt work that way now, 'next/font' is incorporated on nextjs, so you dont need to isntall, and if you are using tailwind, you must to do at another way, is in nextjs docs. so this video is deprecated..

  • @ongptnjp
    @ongptnjp Год назад

    Thanks, but Can I import font from other source except google and local some such as some CDN site ?

  • @yewyewxd
    @yewyewxd 2 года назад

    NextJs team is a beast

  • @vitozdev
    @vitozdev Год назад

    Nice video bro, I wonder if this good practices helps with Headers original paths to configure in advanced next.config.js. Thank's

  • @endaogorman1
    @endaogorman1 Год назад

    i still see a network request for the inter fonts better to download them locally?

  • @rvmelo
    @rvmelo 6 месяцев назад

    How can I set next/font using stitches styling package?

  • @amandakoster
    @amandakoster Год назад

    Thanks for the video! I am using nextjs 13 (+ tailwind + react + typescript) and though my fonts loaded but all of my globals broke. there

  • @williamliu796
    @williamliu796 2 года назад

    Works fine for google fonts, but getting a Cannot read properties of undefined (reading 'fs') for local fonts!

    • @leerob
      @leerob  2 года назад

      Would you mind opening an issue on GitHub with a reproduction? Thank you!

  • @viktormoskalev2269
    @viktormoskalev2269 2 года назад

    How can i set up second font for title by CSS module ? font-family:"inter" doesnot work

  • @ioabhishek
    @ioabhishek 2 года назад

    Hi how i can call the particular variant in any tag if i have added multiple variants inside the src like below
    const proxima = localFont ({
    src: [
    {
    path: '../fonts/ProximaNova-Regular.woff2',
    weight: '400',
    style: 'normal',
    },
    {
    path: '../fonts/ProximaNova-Medium.woff2',
    weight: '500',
    style: 'normal',
    },
    {
    path: '../fonts/ProximaNova-Semibold.woff2',
    weight: '400',
    style: 'normal',
    },
    {
    path: '../fonts/ProximaNova-Bold.woff2',
    weight: '400',
    style: 'normal',
    },
    ]
    })

    • @ioabhishek
      @ioabhishek 2 года назад

      export default function App({ Component, pageProps }) {
      return (






      )
      }
      suppose i ahve to call it inside main then how i can do this for multiple src's

  • @thakurakash321
    @thakurakash321 Год назад

    I just noticed one thing with this after applying next font changes : on local I do not see any css request going for fetching fonts but on production, a request is made with this path "/_next/static/css/694ccb28f5d8ec18.css" which fetches my woff2 fonts. Anyone has idea on why on prod this happens?

  • @nishitsarvaiya1474
    @nishitsarvaiya1474 2 года назад +1

    What font are you using in vscode? is it Fira code?

    • @leerob
      @leerob  2 года назад

      It's just the default!

  • @elgeneee
    @elgeneee 11 месяцев назад

    May i know where u got ur hoodie from? Looks sick 🔥

  • @jazzymichael
    @jazzymichael Год назад

    It looks like it changed with the the new App directory structure that's replacing Pages

  • @hugosimoes6332
    @hugosimoes6332 Год назад

    Thanks, you helped a lot

  • @wshacode7179
    @wshacode7179 Год назад

    what if I wanted to use 3 different fonts for my website ??

  • @ramaniso235
    @ramaniso235 2 месяца назад

    What about the local font how we can use that
    you did not mentioned about it

  • @haveagoodday544
    @haveagoodday544 2 года назад +2

    Hi Lee,
    Great content, thanks. I use mui (previously material-ui) and with that this does not seem to work. It looked very promising, and I hope I am not applying it correctly. If you (or anyone reading) knows how to combine this with mui, it would be great to get some links to content. Mui references working with ThemeProvider, but then I think I am back to square one with getting my google font differently.
    Thanks,

    • @nadavbueno
      @nadavbueno 2 года назад +1

      Bumped into the same issue.
      What I did is set the typography.fontFamily prop in the MUI theme to 'unset' (note that you'll need to create a base theme with this settings and then create your theme using the base theme).
      However it feels like a hack and if there's a proper way to do it I'd be happy to know.

    • @Spectraevil
      @Spectraevil Год назад

      @@nadavbueno Thanks man!

  • @CrownHanKen
    @CrownHanKen Год назад +1

    I got the error "cannot find module 'next/dist/compiled/https-proxy-agent'" when installing next/font. Can anyone give me the solution, thanks so much.

    • @meilyn22
      @meilyn22 Год назад

      Same. Did you figure out a solution?

    • @CrownHanKen
      @CrownHanKen Год назад

      @@meilyn22 I haven't found a way to solve it yet.

    • @meilyn22
      @meilyn22 Год назад

      @@CrownHanKen The version of Next.js and @Next/fonts must be the same. That's what solved my issue.

  • @Renanrr87
    @Renanrr87 Год назад

    anyway to fix inside another ?i'd like to not create a waste tag

  • @chiubaca
    @chiubaca 2 года назад

    Amazing...
    If you're using local font file does next still know how to pair it with a suitable fallback?

    • @leerob
      @leerob  2 года назад +1

      Yes!

    • @chiubaca
      @chiubaca 2 года назад

      @@leerob magic!!

  • @snwdn
    @snwdn 2 года назад

    How can we get it to work in plain CSS or SCSS/SASS?

  • @gabriellazcano256
    @gabriellazcano256 2 года назад

    @next/font/local
    Font loader error:
    Cannot read properties of undefined (reading 'fs')
    Why is this????

    • @leerob
      @leerob  2 года назад

      This was fixed in 13.0.4!

  • @borsaniasushant1
    @borsaniasushant1 Год назад

    What about multiple local fonts with tailwind?

  • @chrisb.8441
    @chrisb.8441 Год назад

    Great video! Thank you

  • @JulianWegner
    @JulianWegner 6 месяцев назад

    Hey. I gave it a shot. doesn't throw an error. Also didn't show up on the website :/ lost…

  • @olehyamhurov3753
    @olehyamhurov3753 Год назад +1

    How do I connect a second font?

  • @zabialy2919
    @zabialy2919 2 года назад +1

    What is this theme?