2

I've been given a directory full of font files (woff2, svg, ttf file types). Is there a way to get these fonts to load when a page is rendered in Drupal? Is there a specific directory I have to put them in or a module that handles custom fonts?

I took a look at the contrib fontyourface module, but it seems to require using external providers instead of being able to upload your own font files.

3
  • CSS has a method to load font files, and as fonts belong to the theming layer, it is ok that they are loaded as the style sheets are loaded. Font are always kinda clunky in my experience... Commented Feb 27, 2019 at 21:22
  • This also works well with SASS/LESS workflows, and the fonts will never be called if the style sheets are not loaded. Commented Feb 27, 2019 at 21:29
  • Ship it with the theme, no module needed. Have CSS load them.
    – Kevin
    Commented Feb 28, 2019 at 1:43

1 Answer 1

8

Do you have a custom theme? If so, you can implement it like this:

Place your fonts in your custom theme, following something similar to this folder structure:

themes
  custom
    my_custom_theme
      fonts
        my_font.woff2
        my_font.ttf
        my_font.svg

And then in your CSS file (it might be in themes/custom/my_custom_theme/css/style.css for example) add the following:

@font-face {
  font-family: "my_font;
  src: url('/fonts/my_font.woff');
  src: url('/fonts/my_font.ttf');
  src: url('/fonts/my_font.svg');
}
2
  • Please explain in detail.
    – Pupil
    Commented Jul 4, 2019 at 4:55
  • Do you not need the format section? url('../fonts/my_font.tff') format('tff'); Commented Mar 12, 2020 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.