How to add a custom font in Cassiopeia

How to add a custom font in Cassiopeia

After breaking Joomla with the remove of Font Awesome from Cassiopeia, I've decided to show you how to break the entire Web by adding a new font in Cassiopeia. #PrivateJoke

More seriously, none of the tips given in my articles will break Joomla or the Web. The solutions provided in my articles are always tested and controlled before posted.

Moreover, editing a child template can only break the design of your site, not the CMS. But and because it's always better to be serious when it come to Joomla, I strongly recommand you to take a backup before any edition / modification on your website.

Last tip before starting the article: if you're not confident enough to apply these changes yourself, do not hesitate to contact me!

Usual recommendations

  • Take a backup of your site before any modification, and test your backup
  • Work on a child template, not on your default template
  • If you don't understand what you read, do not apply it

The default font scheme in Cassiopeia

By default, Cassiopeia lets you choosing different options for the fonts scheme of your website:

  • System fonts, with various options for the body and for the headings;
  • Roboto from a local folder in your template (/media/vendor/roboto-fontface/fonts);
  • Google Fonts form the Web (external server);
  • none
Fonts Scheme Cassiopeia parameters
Font Scheme in Cassiopeia parameters

At this point, it's useful to keep in mind this warning displayed in Casssiopeia's parameters:

System fonts are the most performant although there may be small differences between operating systems.
Loading fonts from external sources might be against privacy regulations in some countries.
Loading fonts from a local folder might have a performance impact on your site.

How to add a custom font in Cassiopeia?

If none of these natives options meets your requirements, select None as fonts scheme in Cassiopeia parameters. So, if you want to add a custom font in Cassiopeia, here is a solution for you.

  1. Open an online font service like FontSquirrel or similar.
  2. Search for your custom font. Let's say: Cantarell.
  3. Open the tab "Webfont Kit".
  4. Choose a subset (The subsetting reduces the number of glyphs in the font to generate a smaller file. If the font supports a particular language, it will appear in the dropdown).
  5. Choose your font formats: .woff (for modern browsers) and .ttf (for Safari, Android, iOS) are recommanded.
  6. Click the button Download @FontFace Kit to dowload the archive cantarell-fontfacekit.zip on your computer.
  7. Cantarell webfont kit
    Cantarell webfont kit on FontSquirrel
  8. Unzip the zip archive you've just downloaded and open the folder web fonts
  9. In the sub folders, you'll find the 2 following files:
    • Cantarell-Regular-webfont.ttf
    • Cantarell-Regular-webfont.woff
  10. Open your SFTP client (FileZilla or similar)
  11. Log to your server and access to the folders: /media/templates/site/cassiopeia of your site
  12. Create a new folder fonts: /media/templates/site/cassiopeia/fonts
  13. Upload in your new folder fonts, the Cantarell-Regular-webfont.woff and the Cantarell-Regular-webfont.ttf files seen previoulsy
  14. In your Joomla backend, open Site template from your admin dashboard
  15. Select your Cassiopeia child template
  16. Click on the CSS folder to open your user.css file in the editor (create one if you still don't have one)
  17. Copy/paste the following lines at the top of your user.css file:
  18. @font-face {
    font-family: 'Cantarell';
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: local(''),
    url('../fonts/Cantarell-Regular-webfont.woff') format('woff'), 
    url('../fonts/Cantarell-Regular-webfont.ttf') format('truetype'), 
    }
  19. Click the "Save" button
  20. Clear your caches (Joomla and browser) to check your new font is now loaded and displayed as expected.

Explanations about font-display: swap - The easiest way to avoid showing invisible text while custom fonts load is to temporarily show a system font. By including font-display: swap in your @font-face style, you can avoid a flash of invisible text (FOIT) in most modern browsers.

Let's get further with preload !

Last part but not the least, I'll show you now how to preload your web fonts using rel="preload" to remove any flash of unstyled text (FOUT) and to to improve their loading speed.

Adapt these codes to your fonts and add these links elements at the <head> of the index-php file of your Cassiopeia child template.

<link rel="preload" href="/media/templates/site/cassiopeia/fonts/mycustomfont.woff" as="font" type="font/woff" crossorigin>
<link rel="preload" href="/media/templates/site/cassiopeia/fonts/mycustomfont.ttf" as="font" type="font/ttf" crossorigin>

Explanations

The as="font" type="font/woff" attributes tell the browser to download this resource as a font and helps in prioritization of the resource queue.

The crossorigin attribute indicates whether the resource should be fetched with a CORS request as the font may come from a different domain. Without this attribute, the preloaded font is ignored by the browser.

With preload, the browser knows that it needs to download this file earlier. It is important to note that if not used correctly, preload can harm performance by making unnecessary requests for resources that are not used.

Recommended readings

Here are some trustworthy sources for anyone interested in learning more about fonts and CSS.

Conclusion

With this solution, you have changed your default font scheme in Cassiopeia (without breaking the entire Web) and improved their loading by the browsers. This solution is also GDPR compliant, as the fonts are stored in your website, not on a distant server.

Thanks for reading and for sharing.

After breaking Joomla with the remove of Font Awesome from Cassiopeia, I've decided to show you how to break the entire Web by adding a new font in Cassiopeia. #PrivateJoke

More seriously, none of the tips given in my articles will break Joomla or the Web. The solutions provided in my articles are always tested and controlled before posted.

Moreover, editing a child template can only break the design of your site, not the CMS. But and because it's always better to be serious when it come to Joomla, I strongly recommand you to take a backup before any edition / modification on your website.

Last tip before starting the article: if you're not confident enough to apply these changes yourself, do not hesitate to contact me!

Usual recommendations

  • Take a backup of your site before any modification, and test your backup
  • Work on a child template, not on your default template
  • If you don't understand what you read, do not apply it

The default font scheme in Cassiopeia

By default, Cassiopeia lets you choosing different options for the fonts scheme of your website:

  • System fonts, with various options for the body and for the headings;
  • Roboto from a local folder in your template (/media/vendor/roboto-fontface/fonts);
  • Google Fonts form the Web (external server);
  • none
Fonts Scheme Cassiopeia parameters
Font Scheme in Cassiopeia parameters

At this point, it's useful to keep in mind this warning displayed in Casssiopeia's parameters:

System fonts are the most performant although there may be small differences between operating systems.
Loading fonts from external sources might be against privacy regulations in some countries.
Loading fonts from a local folder might have a performance impact on your site.

How to add a custom font in Cassiopeia?

If none of these natives options meets your requirements, select None as fonts scheme in Cassiopeia parameters. So, if you want to add a custom font in Cassiopeia, here is a solution for you.

  1. Open an online font service like FontSquirrel or similar.
  2. Search for your custom font. Let's say: Cantarell.
  3. Open the tab "Webfont Kit".
  4. Choose a subset (The subsetting reduces the number of glyphs in the font to generate a smaller file. If the font supports a particular language, it will appear in the dropdown).
  5. Choose your font formats: .woff (for modern browsers) and .ttf (for Safari, Android, iOS) are recommanded.
  6. Click the button Download @FontFace Kit to dowload the archive cantarell-fontfacekit.zip on your computer.
  7. Cantarell webfont kit
    Cantarell webfont kit on FontSquirrel
  8. Unzip the zip archive you've just downloaded and open the folder web fonts
  9. In the sub folders, you'll find the 2 following files:
    • Cantarell-Regular-webfont.ttf
    • Cantarell-Regular-webfont.woff
  10. Open your SFTP client (FileZilla or similar)
  11. Log to your server and access to the folders: /media/templates/site/cassiopeia of your site
  12. Create a new folder fonts: /media/templates/site/cassiopeia/fonts
  13. Upload in your new folder fonts, the Cantarell-Regular-webfont.woff and the Cantarell-Regular-webfont.ttf files seen previoulsy
  14. In your Joomla backend, open Site template from your admin dashboard
  15. Select your Cassiopeia child template
  16. Click on the CSS folder to open your user.css file in the editor (create one if you still don't have one)
  17. Copy/paste the following lines at the top of your user.css file:
  18. @font-face {
    font-family: 'Cantarell';
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: local(''),
    url('../fonts/Cantarell-Regular-webfont.woff') format('woff'), 
    url('../fonts/Cantarell-Regular-webfont.ttf') format('truetype'), 
    }
  19. Click the "Save" button
  20. Clear your caches (Joomla and browser) to check your new font is now loaded and displayed as expected.

Explanations about font-display: swap - The easiest way to avoid showing invisible text while custom fonts load is to temporarily show a system font. By including font-display: swap in your @font-face style, you can avoid a flash of invisible text (FOIT) in most modern browsers.

Let's get further with preload !

Last part but not the least, I'll show you now how to preload your web fonts using rel="preload" to remove any flash of unstyled text (FOUT) and to to improve their loading speed.

Adapt these codes to your fonts and add these links elements at the <head> of the index-php file of your Cassiopeia child template.

<link rel="preload" href="/media/templates/site/cassiopeia/fonts/mycustomfont.woff" as="font" type="font/woff" crossorigin>
<link rel="preload" href="/media/templates/site/cassiopeia/fonts/mycustomfont.ttf" as="font" type="font/ttf" crossorigin>

Explanations

The as="font" type="font/woff" attributes tell the browser to download this resource as a font and helps in prioritization of the resource queue.

The crossorigin attribute indicates whether the resource should be fetched with a CORS request as the font may come from a different domain. Without this attribute, the preloaded font is ignored by the browser.

With preload, the browser knows that it needs to download this file earlier. It is important to note that if not used correctly, preload can harm performance by making unnecessary requests for resources that are not used.

Recommended readings

Here are some trustworthy sources for anyone interested in learning more about fonts and CSS.

Conclusion

With this solution, you have changed your default font scheme in Cassiopeia (without breaking the entire Web) and improved their loading by the browsers. This solution is also GDPR compliant, as the fonts are stored in your website, not on a distant server.

Thanks for reading and for sharing.

Daniel Dubois - auteur à web-eau.net

About Daniel

Passionate about the Web since 2007, Daniel defends the widow and the orphan of the Web by creating W3C-compliant sites. With his experience, he shares his knowledge in an open source mindset. Very involved in favor of the Joomla CMS since 2014, he is the founder of the Joomla User Group Breizh and a speaker in Joomla events.

Website Facebook LinkedIn Twitter Joomla E-mail

Related Articles

web-eau.net

France - 29800 Landerneau

+33 674 502 799

daniel@web-eau.net

Quick links