Question
Answer and Explanation
To use a custom font with NextUI, you'll need to integrate it into your project and then configure NextUI to use it. Here's a comprehensive guide:
1. Include the Custom Font:
- Using Google Fonts: If your custom font is available on Google Fonts, you can include it in your project by adding a link tag in the <head>
section of your HTML or in your main layout file. For example:
<link href="https://fonts.googleapis.com/css2?family=YourCustomFont&display=swap" rel="stylesheet">
- Using a Local Font File: If you have a local font file (e.g., .ttf, .woff, .woff2), you'll need to include it using the @font-face
rule in your CSS. Create a CSS file (e.g., styles.css
) and add the following:
@font-face {
font-family: 'YourCustomFont';
src: url('/fonts/your-custom-font.woff2') format('woff2'),
url('/fonts/your-custom-font.woff') format('woff');
font-weight: normal;
font-style: normal;
}
- Make sure to adjust the paths and formats according to your font files. Then, include this CSS file in your project.
2. Configure NextUI Theme:
- NextUI allows you to customize the theme, including the font family. You can do this by creating a custom theme object and passing it to the NextUIProvider
. Here's how:
- In your main application file (e.g., _app.js
or _app.tsx
), import createTheme
and NextUIProvider
from @nextui-org/react
:
import { createTheme, NextUIProvider } from "@nextui-org/react";
- Create a custom theme object:
const customTheme = createTheme({
theme: {
fonts: {
sans: 'YourCustomFont, sans-serif',
mono: 'YourCustomFont, monospace',
},
},
});
- Wrap your application with NextUIProvider
and pass the custom theme:
function MyApp({ Component, pageProps }) {
return (
<NextUIProvider theme={customTheme}>
<Component {...pageProps} />
</NextUIProvider>
);
}
3. Apply the Font:
- After setting up the custom theme, NextUI components will automatically use the specified font family. You can also apply the font to specific elements using CSS if needed.
4. Troubleshooting:
- If the font is not applied, double-check the font file paths, the @font-face
rule, and the theme configuration. Ensure that the font family name in the theme matches the one defined in your CSS or Google Fonts link.
By following these steps, you can successfully integrate and use a custom font with NextUI, enhancing the visual appeal of your application.