Why aren't @font-face fonts loading in Sanity Studio custom branding CSS?

9 replies
Last updated: Feb 18, 2022
Anyone know why
@font-face
loaded fonts are not taking when adding custom branding CSS to a studio?
AI Update

I understand the frustration with custom fonts not loading in Studio. The issue is that Sanity Studio v3 doesn't have a built-in theme.fonts configuration option in the config file.

For custom fonts in Studio v3, you need to load them through custom CSS injected via a plugin. Here's what's likely happening and how to fix it:

The Solution: Custom CSS Plugin

You'll need to create a plugin that injects your CSS (including @font-face declarations) into the Studio. Here's the approach:

1. Create a plugin file (e.g., plugins/customStyles.ts):

import {definePlugin} from 'sanity'

export const customStyles = definePlugin({
  name: 'custom-styles',
  studio: {
    components: {
      layout: (props) => {
        // Inject your styles
        if (typeof document !== 'undefined') {
          const styleId = 'custom-studio-styles'
          if (!document.getElementById(styleId)) {
            const style = document.createElement('style')
            style.id = styleId
            style.textContent = `
              @font-face {
                font-family: 'YourCustomFont';
                src: url('/static/fonts/your-font.woff2') format('woff2');
                font-weight: normal;
                font-style: normal;
              }
              
              /* Apply to Studio UI */
              body, .sanity-root {
                font-family: 'YourCustomFont', sans-serif !important;
              }
            `
            document.head.appendChild(style)
          }
        }
        return props.renderDefault(props)
      }
    }
  }
})

2. Add the plugin to your config:

import {defineConfig} from 'sanity'
import {customStyles} from './plugins/customStyles'

export default defineConfig({
  // ... other config
  plugins: [customStyles()]
})

Why @font-face Might Not Be Working

The most common issues are:

  1. Font file paths are wrong - Make sure your font files are in your public or static folder and the paths in your @font-face declarations are correct
  2. CSS isn't being injected - Studio v3 doesn't automatically load custom CSS files; you need a plugin to inject them
  3. Specificity issues - Studio's default styles might be overriding yours; use !important if needed

Alternative: Use Web Fonts

If you're using Google Fonts or another web font service, you can import them in your plugin:

style.textContent = `
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
  
  body, .sanity-root {
    font-family: 'Inter', sans-serif !important;
  }
`

What About buildLegacyTheme?

The official Studio theming approach using buildLegacyTheme is primarily for color palette customization, not fonts. The Themer tool can help you visualize color schemes, but for custom fonts, you'll need the CSS injection approach described above.

Make sure your font files are properly served and the paths in your @font-face declarations match where they're actually located in your project structure.

Show original thread
9 replies
1. I’ve got my
skin.css
being included via
sanity.json
, which is loading properly.2. I’ve added my fonts to the
static
folder, in a
fonts
sub-folder.3. I’m importing them in my
skin.css
at the top, like this:
@font-face {
  font-family: 'Sang Bleu';
  src: url('/fonts/SangBleuSans-RegularItalic.woff') format('woff');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}
4. And I’m referencing them in my CSS like this:

.pt-editable * {
  font-family: 'Sang Bleu', Georgia, Times, serif !important;
}
But no matter what, it’s falling back to
Georgia
no errors loading the fonts, no CORS issues, etc.
no idea why this isn’t working
can also confirm that the fonts are loading in just fine:
and the CSS is there:
I admit that I often cheat and use google’s imported format as a working example personally. Perhaps try woff2 format with your local font and or swap in a different font file locally to see if it renders. Alternatively, try testing with a public google font to try to isolate the issue to either a stylesheet configuration or font-file problem or a build problem.
Here is a clip from google’s roboto import sheet:


/* latin-ext */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 100;
  font-display: swap;
  src: url(<https://fonts.gstatic.com/s/roboto/v29/KFOkCnqEu92Fr1MmgVxGIzIXKMnyrYk.woff2>) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 100;
  font-display: swap;
  src: url(<https://fonts.gstatic.com/s/roboto/v29/KFOkCnqEu92Fr1MmgVxIIzIXKMny.woff2>) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

I am not sure off the top of my head if explicitly defined unicode ranges are necessary, but this may be another thing to try in your troubleshooting steps.
I am not sure off the top of my head if explicitly defined unicode ranges are necessary, but this may be another thing to try in your troubleshooting steps.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?