Including a font in Angular.js (.ttf)

All we need is an easy explanation of the problem, so here it is.

I have a .ttf font file that I need to use in my Angular.js application. I don’t know how to import it and access it in my css files.

Could someone give me some direction in using this font file with Angular/CSS?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Including a font has nothing to do with angularjs. You have to declare it in a CSS file:

Take this chunk of my own as an example, declared in a stylesheet:

@font-face {
font-family: 'Durant';
src: url('../fonts/DurantBold.eot'); /* IE9 Compat Modes */
src: url('../fonts/DurantBold.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
     url('../fonts/DurantBold.otf') format('opentype'), /* Legacy iOS */
     url('../fonts/DurantBold.svg#Durant-Bold') format('svg'), /* Legacy iOS */
     url('../fonts/DurantBold.ttf')  format('truetype'), /* Safari, Android, iOS */
     url('../fonts/DurantBold.woff') format('woff');
font-weight: bold;
}

Remember that the paths are relative to the css file.

Today, most of the file formats are supported by most browsers – I don’t know, concretely, the incompatibilities among browsers and fonts. This style is somewhat old.

Besides, I have all of those files (1 file per font, per format, per weight variation, per style variation – highly frustrating). You will not include configs for files you don’t have.

If you have only .ttf files you only need this snippet in the .css file:

@font-face {
font-family: 'Durant';
src: url('../fonts/DurantBold.ttf')  format('truetype');
font-weight: bold;
}

remember to actually include the css file where you declared this chunk, in the page where you will use it. If you use states (ngRouter / ui.router), then include the font in the MAIN page, not in the partials.

remember to have the font files (.ttf) in a location accessible by the declaration in this css file either being:

  • Absolute URL / CDN: this needs no explanation.
  • relative-to-site url: a path starting with / refers the path being relative to document root, as always.
  • relative url: a path not starting with / is relative to the current css file.

I know I wrote that many times but it always causes headaches when forgotten.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply