December 28, 2020
Estimated Post Reading Time ~

3 Different Ways to Embed Custom Fonts in AEM Sites

In this article, we will go through three different ways how you can embed custom fonts in an AEM site.

1. Embed Custom Font using Google Fonts, CDN
Embed a custom font is by using Google Fonts. Google Fonts can be quickly installed into your project by using the @import CSS rule. Utilizing the out of the box setup, you are prescribed to Google Font’s CDN. The lifecycle management of the fonts embedded onto your AEM site will be dependant on Google Font’s CDN servers.

There are hundreds of free fonts offered by Google. Simply visit the Google Font’s website to request which fonts you wish to use on your website. Next, obtain the @import CSS configuration that is offered by the webpage.

Embedding font via @import URL:
<style type="text/css">

@import URL("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;1,100&display=swap");

div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td, .tab-item, input,
button, select, textarea {
font-family: "Roboto", sans-serif;
font-size: 1rem;
font-weight: 300;
}
</style>


2. Embed Custom Font using Adobe Fonts, CDN
Embed a custom font is by using Adobe Fonts, where fonts can be quickly installed into your project by using the @import CSS rule. After logging into Adobe Font’s, you can create a new project where you can select different fonts from Adobe Font’s collection to be used in your website. Retrieve the “embed code” CDN path from your Adobe Font’s project, and you are well on your way.



Next, obtain the @import CSS configuration that is offered by the webpage.

Embedding font via @import URL:
<style type="text/css">

@import URL("https://use.typekit.net/mqs3rg.css");

div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td, .tab-item, input,
button, select, textarea {
font-family: "Roboto", sans-serif;
font-size: 1rem;
font-weight: 300;
}
</style>


3. Embed Font via AEM Client Library Static Asset
The most common way to embed a custom font for an AEM project is to store web-safe font files directly into a client library. The fonts are then referenced by the CSS stylesheet, where the font file paths are the proxied client libraries URI. Using the CSS rule, @font-face, we are able to configure custom fonts for an AEM project.



Embedding font via @font-face URL:
<style type="text/css">
@font-face {
font-family: "Roboto";
src: url("/etc.clientlibs/sourcedcode/clientlibs/clientlib-site/resources/fonts/roboto-bold.otf") format("truetype");
font-style: normal;
font-weight: 700;
}

@font-face {
font-family: "Roboto";
src: url("/etc.clientlibs/sourcedcode/clientlibs/clientlib-site/resources/fonts/roboto-medium.otf") format("truetype");
font-style: normal;
font-weight: 500;
}

@font-face {
font-family: "Roboto";
src: url("/etc.clientlibs/sourcedcode/clientlibs/clientlib-site/resources/fonts/roboto-regular.otf") format("truetype");
font-style: normal;
font-weight: 300;
}

div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td, .tab-item, input,
button, select, textarea {
font-family: "Roboto", sans-serif;
font-size: 1rem;
font-weight: 300;
}
</style>



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.