sodcalculator

10 Tech How-To

How to Use Custom Fonts in CSS

Guide 10-57091 · Updated · Step by step

Custom fonts let you break free from the limited set of system fonts and give your website a unique, branded look. This guide walks you through the two main methods—using the @font-face rule to host font files yourself and linking to a font service like Google Fonts. You’ll learn the exact CSS syntax, how to format multiple font weights, and how to avoid common pitfalls like layout shifts or missing characters.

What You’ll Need

  • A text editor (VS Code, Sublime, etc.)
  • Basic familiarity with HTML and CSS
  • Font files in a web-friendly format (WOFF2, WOFF, TTF) OR a link to an online font service
  • If self-hosting, the font files should be in a folder accessible to your CSS (e.g., fonts/ next to your stylesheet)

Step-by-Step

  1. Choose your font. If you’re using a free service like Google Fonts, browse and select the font family and weights you need. If you have a custom font file (from a designer or a paid license), make sure you have the rights to use it on the web and that it’s in a format like WOFF2 (best for modern browsers) or WOFF (fallback).
  2. Decide on hosting method. Self-hosting gives you full control and better privacy, but requires managing files. Using a CDN like Google Fonts is simpler but requires an internet connection and may slow down first load.
  3. If using Google Fonts (or similar): Get the embed code. On Google Fonts, select your font and weights, then copy the <link> tag from the “Get embed code” section. Paste it in the <head> of your HTML, before your own CSS. For example: <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">.
  4. If self-hosting: Place your font files (e.g., myfont.woff2) in a folder, say fonts/, inside your project.
  5. Define the font with @font-face. In your CSS (or in a separate @font-face block), add the following for each font file and weight:
  6. @font-face {
      font-family: 'MyCustomFont';
      src: url('fonts/myfont.woff2') format('woff2'),
           url('fonts/myfont.woff') format('woff');
      font-weight: normal;
      font-style: normal;
      font-display: swap;
    }
  7. Apply the font to your elements. Use the font-family property in your CSS, just like you would with a system font. For example:
  8. body {
      font-family: 'MyCustomFont', Arial, sans-serif;
    }
  9. Add fallbacks. Always include a fallback font (like sans-serif or serif) after your custom font, in case the font fails to load or the user’s browser doesn’t support it.
  10. Use multiple weights and styles. If you need bold, italic, or other weights, add separate @font-face rules for each, with the appropriate font-weight and font-style values. For example:
  11. @font-face {
      font-family: 'MyCustomFont';
      src: url('fonts/myfont-bold.woff2') format('woff2');
      font-weight: 700;
      font-style: normal;
    }
    @font-face {
      font-family: 'MyCustomFont';
      src: url('fonts/myfont-italic.woff2') format('woff2');
      font-weight: normal;
      font-style: italic;
    }
  12. Test your site. Open your HTML in a browser and inspect the text to ensure the custom font is applied. Use the browser’s developer tools (right-click → Inspect) to check the computed font-family and see if the font file loads without errors in the Network tab.

Common Mistakes

  • Incorrect file path: The url() in @font-face is relative to the CSS file, not the HTML file. If your CSS is in css/style.css and fonts are in fonts/ at the project root, the path should be ../fonts/myfont.woff2.
  • Missing font-weight matching: If you only define a normal weight, but use font-weight: bold in your CSS, the browser will try to synthesize bold (which can look ugly) or fall back to a system font. Always define the weights you use.
  • Forgetting font-display: swap: Without it, the browser might block text rendering until the font loads, causing a blank page for a moment. swap shows fallback text immediately, then swaps when the font is ready.
  • Using too many font files: Each weight and style is a separate file, which increases page load time. Limit to a few weights (e.g., 400 and 700) and use the font-synthesis property to allow the browser to fake missing weights if needed.
  • Not providing fallbacks: If the custom font fails, the browser will use the next font in the stack. Without a fallback, it might use a default serif, which can break your design.

Tips

  • Use WOFF2 for modern browsers. It’s smaller and faster. Provide WOFF as a fallback for older browsers.
  • Subset your fonts. If you only need Latin characters, use a subset to reduce file size. Many font services offer this automatically.
  • Preload your font files. Add <link rel="preload" href="myfont.woff2" as="font" type="font/woff2" crossorigin> in your HTML to speed up loading.
  • Use font-display: swap to avoid invisible text. This is a good default for most sites.
  • Check licensing. Some free fonts are only for personal use. Always verify the license before using on a commercial site.
  • Test on different devices. Some fonts render differently on Windows vs. Mac. Use a service like BrowserStack or just test on multiple devices.

Troubleshooting

The font isn’t applying at all. First, check the browser’s developer tools (F12) and look at the Network tab for any 404 errors on the font file. If the file loads, inspect the computed style of the text to see if font-family is set correctly. Make sure your CSS rule isn’t overridden by a more specific selector.

The font loads but looks like a fallback. This often happens when the font-weight or font-style doesn’t match what you defined. For example, if you use font-weight: 300 but only defined 400, the browser will use the fallback. Add the missing weight or adjust your CSS.

Text is invisible for a moment (FOIT). This is the default behavior of font-display: auto. Change it to swap in your @font-face rule or in the Google Fonts link (add &display=swap).

Cross-origin issues. If you’re self-hosting and the font is on a different domain (e.g., a CDN), you may need to set the Access-Control-Allow-Origin header on the server. For local development, this rarely happens.

Font looks blurry or pixelated. This can happen if the font is being scaled by the browser. Ensure you’re using the correct font size and that the font file has proper hinting. Also, check that you’re not using a very small font size that forces the browser to scale.

It works in one browser but not another. Different browsers have different font format support. Make sure you provide multiple formats (WOFF2, WOFF, TTF) in your src list. For Google Fonts, the service handles this automatically.

Frequently Asked Questions

Can I use any font I download from the internet?

Only if the license allows it. Many free fonts are for personal use only. For commercial projects, look for fonts with an open license like SIL Open Font License or purchase a commercial license.

What is the difference between @font-face and Google Fonts?

@font-face is a CSS rule that lets you define a font from a file you host. Google Fonts is a service that hosts fonts for you and provides a <link> tag to include them. Self-hosting gives you more control and privacy, while Google Fonts is easier to set up.

How many font weights should I include?

As few as possible. Each weight adds a file request. Typically, 2-3 weights (regular, bold, maybe italic) are enough. If you need more, consider using a variable font, which can cover a range of weights in one file.

Why is my custom font not showing on mobile?

Mobile browsers may have stricter CORS or caching rules. Ensure your server sends the correct headers, and try adding crossorigin to the preload link. Also, check that the font file is not too large (over 500KB) as some mobile browsers may block it.

Can I use custom fonts in email newsletters?

Yes, but support is limited. Many email clients (like Gmail) don’t support web fonts. It’s safer to use system fonts or provide a fallback like Arial.

What is a variable font and should I use it?

A variable font contains multiple weights and styles in a single file, allowing you to interpolate between them. This reduces load time and gives you more flexibility. If you need many weights, it’s a great option, but support is not universal in older browsers.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →