210

I want to include a global font for my page. I downloaded a .ttf file, and included it in my CSS, but my font won't change.

Here's my code:

@font-face {
    font-family: 'oswald';
    src: url('/font/oswald.regular.ttf');
}

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    font-size:100%;
    font:inherit;
    font-family: oswald;
    vertical-align:baseline
} 

Where did I go wrong?

Asker
  • 1,299
  • 2
  • 14
  • 31
Jerielle
  • 7,144
  • 29
  • 98
  • 164

5 Answers5

304

Only providing .ttf file for webfont won't be good enough for cross-browser support. The best possible combination at present is using the combination as :

@font-face {
    font-family: 'MyWebFont';
    src: url('webfont.eot'); /* IE9 Compat Modes */
    src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
         url('webfont.woff') format('woff'), /* Modern Browsers */
         url('webfont.ttf')  format('truetype'), /* Safari, Android, iOS */
         url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
}

This code assumes you have .eot , .woff , .ttf and svg format for you webfont. To automate all this process , you can use : Transfonter.org.

Also , modern browsers are shifting towards .woff font , so you can probably do this too :

@font-face {
    font-family: 'MyWebFont';
    src: url('myfont.woff') format('woff'), /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
         url('myfont.ttf') format('truetype'); /* Chrome 4+, Firefox 3.5, Opera 10+, Safari 3—5 */
}  

Read more here : http://css-tricks.com/snippets/css/using-font-face/


Look for browser support : Can I Use fontface

Stefa168
  • 35
  • 2
  • 9
Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
  • and if you're using GFonts check out https://stackoverflow.com/a/30585464/1727470 – Samie Bencherif Jul 02 '17 at 05:24
  • 1
    For those like me who get confused easily - paths are with forward slashes `url('path/to/font.woff')` – wunth May 30 '18 at 04:06
  • I had an issue with comments in the font-face rule. Moving the comments to the end of the rule fixed the problem. – Eric D'Souza Jul 31 '18 at 15:11
  • I found that the font I wanted to generate a web font for was blacklisted from Font Squirrel. However, https://transfonter.org/ worked for me. – HotN Jan 09 '20 at 23:39
  • I also had trouble with Font Squirrel. success with font-converter.net – chantey Jun 21 '20 at 05:24
  • AFAIK there's no reason to have both `woff` and `ttf`, since they're both the same format except `woff` includes compression - and all browsers that support `ttf` also support `woff`. Either use one or the other. – Abhi Beckert May 10 '21 at 06:10
  • I have EOT only and tranfosrter.org usage is "click the Add font(s) button, select the TTF, OTF, WOFF, WOFF2 or SVG fonts on your computer and click Convert." – Konrad Grzyb Nov 25 '21 at 19:38
60

Did you try format?

@font-face {
  font-family: 'The name of the Font Family Here';
  src: URL('font.ttf') format('truetype');
}

Read this article: http://css-tricks.com/snippets/css/using-font-face/

Also, might depend on browser as well.

Alicia Sykes
  • 5,997
  • 7
  • 36
  • 64
user3883419
  • 701
  • 4
  • 3
28

You can use font face like this:

@font-face {
  font-family:"Name-Of-Font";
  src: url("yourfont.ttf") format("truetype");
}
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
INTODAN
  • 521
  • 1
  • 6
  • 14
17

I know this is an old post but this solved my problem.

@font-face{
  font-family: "Font Name";
  src: url("../fonts/font-name.ttf") format("truetype");
}

notice src:url("../fonts/font-name.ttf"); we use two periods to go back to the root directory and then into the fonts folder or wherever your file is located.

hope this helps someone down the line:) happy coding

BlakeWebb
  • 479
  • 5
  • 11
2

Check the console to make sure the resource is loaded. Depending on file and folder structure, your url may be wrong. Notice the difference between:

src: url('/VT323/VT323-Regular.ttf') format('truetype');

src: url('VT323/VT323-Regular.ttf') format('truetype');

src: url('../VT323/VT323-Regular.ttf') format('truetype');