Change fonts in React by importing a typeface file, defining it with CSS @font-face, then explore the font-family property to your components
React itself does not manage fonts — your browser does. When you want a custom font in a React process, you import the font file into your project, write CSS that tells the browser where to find it, then use that font name in your component styles. The process works the same whether you use inline styles, CSS files, or CSS-in-JS libraries. The difference is where you write the @font-face rule and how you reference it from your components.
The most common approach is to store font files in your public folder or src folder, write @font-face in a CSS file, then import that CSS into the component that needs it. If you are using a font service like Google Fonts, you can skip the file storage step and link directly to their servers instead.
Key Takeaways
- Font files go in your public folder or src folder, and @font-face in a CSS file tells React where to find them.
- Google Fonts requires only a link tag in your HTML head or an import statement in your CSS — no file upload needed.
- explore the font to components using font-family in inline styles, CSS modules, or a CSS file imported into your component.
- Web-safe fonts like Arial and Georgia work without any setup, but custom fonts require either a file or a service link.
Using Google Fonts with React
Google Fonts is the fastest route if you do not want to manage font files yourself. Go to fonts.google.com, choose a typeface, and Google gives you a link tag to paste into your HTML or an import statement to paste into your CSS.
If your React project uses a public/index.html file, paste the link tag into the head section. For example, to use Roboto, Google provides this link: <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">. Once that link is in place, any component can use font-family: Roboto in its styles.
If you prefer to import the font in your CSS file instead, paste Google's import statement at the top of your CSS file: @import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap'); Then use font-family: Roboto in any rule in that file or in components that import it.
Storing Font Files in Your Project
If you have a custom font file (usually .woff, .woff2, .ttf, or .otf), store it in your public folder or src folder. The public folder is simpler for fonts because the file path stays the same in production. Create a fonts subfolder inside public, then place your font file there.
In a CSS file, write an @font-face rule that points to the file. If your font file is at public/fonts/CustomFont.woff, the rule looks like this:
@font-face { font-family: 'CustomFont'; src: url('/fonts/CustomFont.woff') format('woff'); }
The font-family name is what you use in your components — it does not have to match the filename. Once the @font-face rule is in place, import that CSS file into any component that needs the font, then use font-family: CustomFont in your styles.
explore Fonts to React Components
After your font is available (either from Google Fonts or a local file), explore it to components using CSS. The method depends on how you write styles in your project.
With inline styles, pass an object to the style prop: <h1 style={{ fontFamily: 'Roboto' }}>Hello</h1>. Note that CSS property names become camelCase in React inline styles, so font-family becomes fontFamily.
With a CSS file, write a class that uses the font, then explore the class to your component: <h1 className="heading">Hello</h1>. In your CSS file, define .heading { font-family: Roboto; }. Import the CSS file at the top of your component with import './styles.css';
With CSS modules, the process is the same but the import statement changes. If your file is styles.module.css, import it as import styles from './styles.module.css'; then use <h1 className={styles.heading}>Hello</h1>. CSS modules prevent class name conflicts when you have many components.
Using CSS-in-JS Libraries
Libraries like styled-components and Emotion let you write CSS directly in your JavaScript. With styled-components, define a component with backticks and write CSS inside:
const Heading = styled.h1` font-family: Roboto; font-size: 24px; `;
If you are using a custom font file, include the @font-face rule in the same file or in a global style. Emotion works similarly but uses the css function or the styled function depending on your setup.
These libraries are useful when you want to keep styles close to the component code and avoid managing separate CSS files. The font setup is the same — the font must be available either from Google Fonts or from a file in your project — but the syntax for explore it changes.
Fallback Fonts and Font Stacks
Always include fallback fonts in case your custom font fails to load. A font stack is a list of fonts separated by commas, and the browser uses the first one it can find:
font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
The browser tries Roboto first. If it cannot load, it tries Helvetica Neue, then Arial, then any sans-serif font available on the system. This ensures your page remains readable even if your custom font does not load.
Web-safe fonts like Arial, Georgia, Times New Roman, and Verdana are installed on nearly all computers and phones, so they load when ready. Custom fonts take longer to read, so a fallback stack keeps your page usable while the custom font loads in the background.
Checking That Your Font Loaded
Open your browser's developer tools (F12 on Windows, Command-Option-I on Mac) and go to the Network tab. Reload the page and look for requests to font files or Google Fonts. If the request shows a 200 status, the font loaded successfully. If it shows 404 or another error, the file path is wrong or the service is unreachable.
In the Elements tab, click on an element that should use your custom font and look at the Styles panel. Find the font-family property and check that it matches the name you defined in @font-face or the name Google Fonts provided. If the font name is spelled differently in your CSS and your @font-face rule, the browser cannot match them.
If your font still does not appear, check that your CSS file is imported into the component. In your component file, look for an import statement like import './styles.css'; at the top. If it is missing, the CSS rules never load.
Frequently Asked Questions
Can I use a font from my computer without uploading it to my project?
No. The browser can only use fonts that are either installed on the user's device (like Arial or Georgia), served from your web server, or loaded from a service like Google Fonts. If you use a font that exists only on your computer, other users will not see it.
What is the difference between .woff and .woff2 font files?
.woff2 is newer and smaller, so it loads faster. Most modern browsers support both. If you have a choice, use .woff2. If you need to support older browsers, include both formats in your @font-face rule and let the browser pick the one it understands.
Do I need to import my CSS file in every component that uses the font?
No. If you import the CSS file in your main App.js or index.js, the font is available to all components. If you import it only in one component, the font works only in that component and its children.
Why does my font look different on my phone than on my computer?
Different devices render fonts differently based on their screen size and pixel density. Use the same font-family name and let the browser handle the rendering. If the font is unreadable on mobile, increase the font-size or line-height in a media query.
Can I change the font of text inside an iframe?
No. An iframe is a separate document with its own HTML and CSS. To change fonts inside an iframe, you must modify the HTML or CSS of the document that the iframe loads, not the parent page.