576

Is there a way to add some custom font on a website without using images, Flash or some other graphics?

For example, I was working on a wedding website, and I found a lot of nice fonts for that subject. But I can't find the right way to add that font on the server. And how do I include that font with CSS into the HTML? Is this possible to do without graphics?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
vaske
  • 9,332
  • 11
  • 50
  • 69
  • 20
    Since this question was asked, `@font-face` has become much more widely supported and is recommended for general use. You just have to be aware that IE requires fonts in a different format to other browsers. See http://stackoverflow.com/questions/2219916/is-font-face-usable-now – David Johnstone Apr 15 '10 at 03:27
  • 16
    Make sure you have the right to distribute the font! – Viet May 31 '10 at 03:33
  • 2
    Visit http://typekit.com/, or for the cheap http://www.fontsquirrel.com/ – bjudson Sep 22 '10 at 02:51
  • 1
    related http://stackoverflow.com/questions/8050640/how-does-iefix-solve-web-fonts-loading-in-ie6-ie8 – Adriano Mar 27 '14 at 13:04
  • 3
    www.fontsquirrel.com seems great to generate the required font files & create a good bullet-proof @fontface syntax (same as the recommended http://www.fontspring.com/blog/fixing-ie9-font-face-problems) – Adriano Mar 27 '14 at 13:06

21 Answers21

558

This could be done via CSS:

<style type="text/css">
@font-face {
    font-family: "My Custom Font";
    src: url(http://www.example.org/mycustomfont.ttf) format("truetype");
}
p.customfont { 
    font-family: "My Custom Font", Verdana, Tahoma;
}
</style>
<p class="customfont">Hello world!</p>

It is supported for all of the regular browsers if you use TrueType-Fonts (TTF), the Web Open Font Format (WOFF) or Embedded Opentype (EOT).

corn on the cob
  • 2,093
  • 3
  • 18
  • 29
hangy
  • 10,765
  • 6
  • 43
  • 63
  • Note: Looks like the font-face tag also is a part of CSS3 and not just CSS2. See http://www.css3.info/webkit-has-web-fonts-support/ for more information. – hangy Sep 20 '08 at 11:48
  • 66
    It's not useless since it's standard : the more you implement it, the more it will likely be implemented by browsers. That does not mean you should not find another way to compensate, but using this as a complement seems good to me. – Bite code Sep 22 '08 at 09:14
  • 10
    Firefox 3.1 does support @font-face, though. – Ms2ger Feb 26 '09 at 15:20
  • 2
    @brendanjerwin: updated version of your link - http://brendanjerwin.com/development/web/2009/03/03/embedding-fonts.html – Matt Ball Sep 21 '10 at 15:00
  • 1
    @brendanjerwin: updated the updated version of your link -http://brendanjerwin.com/blog/2009/03/03/embedding-fonts/ – Loren Sep 25 '12 at 00:58
  • 1
    @e-satis: "It's not useless since it's standard : the more you implement it, the more it will likely be implemented by browsers." We are facing something of a large-scale (philosopher's) "prisoner's dilemma" here. If the web as a whole adopts a proposed standard, it is more likely that browsers comply. However the individual best optimization for inconsistent support is often not to just do what it would be best if everyone did, and that's even without taking into account "embrace and extend" and like menaces. – Christos Hayward Oct 10 '12 at 18:37
  • i have added same but cant adding the font ? or may have i added wrong way ? – Bajrang Apr 09 '13 at 10:06
  • 1
    Couldn't we dispense with the broken links and just put the pertinent info here? Create the .eot file by downloading MS Web Embedding Font Tool (WEFT) @font-face { font-family: "Vineta BT"; src: url(http://MyServer/MyFontLocation/VINETAB0.eot); } – B H Nov 22 '13 at 17:12
  • see the bulletproof standard @font-face syntax http://www.fontspring.com/blog/fixing-ie9-font-face-problems – Adriano Mar 27 '14 at 13:07
  • 72
    Hey guys, guess what? It's now almost 2016! It's now supported widely! Yay! Glad, I found this answer this late. Haha. – jessica Dec 17 '15 at 22:43
  • 1
    How to link on a local path? – Black Jan 25 '16 at 11:53
  • @EdwardBlack Just leave out the `//`. – Mr Lister Jun 15 '16 at 18:01
  • NOTE: If the font file name contains space, you must use quotes, like this: `url('the font.ttf')` or `url("the font.ttf")` – Bene Laci Aug 02 '23 at 14:02
140

You can add some fonts via Google Web Fonts.

Technically, the fonts are hosted at Google and you link them in the HTML header. Then, you can use them freely in CSS with @font-face (read about it).

For example:

In the <head> section:

 <link href=' http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>

Then in CSS:

h1 { font-family: 'Droid Sans', arial, serif; }

The solution seems quite reliable (even Smashing Magazine uses it for an article title.). There are, however, not so many fonts available so far in Google Font Directory.

alex
  • 479,566
  • 201
  • 878
  • 984
Michał Pękała
  • 2,359
  • 5
  • 22
  • 32
  • 1
    In July 2014, google has expanded the fonts and included Google Noto fonts which support most languages https://www.google.com/get/noto/#/ . Abobe has supported this open font initiative supporting all languages http://blog.typekit.com/2014/07/15/introducing-source-han-sans/ – vsingh Apr 01 '15 at 15:07
  • it seems (Google web fonts) doesn't support .eot and .woff and .svg versions of fonts. it just supports .ttf version of fonts! – Mahdi Jazini Sep 04 '18 at 07:38
  • Note, that at this time, it does not work with type='text/css' for me but only with type='text/html'. – ChrisoLosoph Aug 22 '23 at 09:47
83

The way to go is using the @font-face CSS declaration which allows authors to specify online fonts to display text on their web pages. By allowing authors to provide their own fonts, @font-face eliminates the need to depend on the limited number of fonts users have installed on their computers.

Take a look at the following table:

enter image description here

As you can see, there are several formats that you need to know about mainly due to cross-browser compatibility. The scenario in mobile devices isn't much different.

Solutions:

1 - Full browser compatibility

This is the method with the deepest support possible right now:

@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 */
}

2 - Most of the browser

Things are shifting heavily toward WOFF though, so you can probably get away with:

@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 */
}

3 - Only the latest browsers

Or even just WOFF.
You then use it like this:

body {
  font-family: 'MyWebFont', Fallback, sans-serif;
}

References and Further reading:

That's mainly what you need to know about implementing this feature. If you want to research more on the subject I'll encourage to take a look at the following resources. Most of what I put here is extracted from the following

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
Javier Cadiz
  • 12,326
  • 11
  • 55
  • 76
  • 2
    The extremely handy [Font Squirrel tool](https://www.fontsquirrel.com/tools/webfont-generator) can generate the CSS needed for full-browser support. It even lets you upload the font as any one of these formats, converts it to all the other formats, and lets you download them all. It's a life saver when it comes to custom fonts. – Jacob Stamm Oct 08 '16 at 03:55
  • What does `svgFontName` stand for? Should I change it to my font family name or leave it as is? – Gherman Nov 19 '16 at 17:15
18

If by non standard font, you mean custom font of a standard format, here's how I do it, and it works for all browsers I've checked so far:

@font-face {
    font-family: TempestaSevenCondensed;
    src: url("../fonts/pf_tempesta_seven_condensed.eot") /* EOT file for IE */
}
@font-face {
    font-family: TempestaSevenCondensed;
    src: url("../fonts/pf_tempesta_seven_condensed.ttf") /* TTF file for CSS3 browsers */
}

so you'll just need both the ttf and eot fonts. Some tools available online can make the conversion.

But if you want to attach font in a non standard format (bitmaps etc), I can't help you.

BiAiB
  • 12,932
  • 10
  • 43
  • 63
  • 1
    Online tool example: http://www.kirsle.net/wizards/ttf2eot.cgi Offline tool example: http://code.google.com/p/ttf2eot/wiki/Demo - it should also be possible to use [WOFF](http://en.wikipedia.org/wiki/Woff) fonts. – Wilf Feb 22 '14 at 19:15
11

I've found that the easiest way to have non-standard fonts on a website is to use sIFR

It does involve the use of a Flash object that contains the font, but it degrades nicely to standard text / font if Flash is not installed.

The style is set in your CSS, and JavaScript sets up the Flash replacement for your text.

Edit: (I still recommend using images for non-standard fonts as sIFR adds time to a project and can require maintenance).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 935
  • 6
  • 16
  • 25
    What a terrible idea ! I have flash in my browser but flashblock too. On that website, my browser display an horrible defaced page. Flash should stay something that you use for big animations or movies. Too much flash is wery anoying and visualy, my my... – Bite code Sep 22 '08 at 09:17
  • 2
    @e-satis: How so? Personally, I never notice the difference between a page that uses sIFR and one that doesn't, aside from the subtle text rendering differences. I think it's a great idea, honestly, although `@font-face` is much better where supported. – Sasha Chedygov Apr 15 '10 at 03:14
  • "Edit: (I still recommend using images for non-standard fonts as sIFR adds time to a project and can require maintenance)." very well said. sIFR is a really cool technique but it seems to require a lot to get the EXACT look you want. If you have the time to learn and master this technique I'd highly recommend it. But if you are looking for a quick and easy font-replacement technique I'd use image replacement or even the @font-face css property – Derek Adair Jul 09 '10 at 16:00
  • Adobe Flash is dead; no longer a good idea. – Daniel Griscom May 24 '23 at 19:10
11

The article Font-face in IE: Making Web Fonts Work says it works with all three major browsers.

Here is a sample I got working: http://brendanjerwin.com/test_font.html

More discussion is in Embedding Fonts.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brendanjerwin
  • 1,381
  • 2
  • 11
  • 25
10

Typeface.js and Cufon are two other interesting options. They are JavaScript components that render special font data in JSON format (which you can convert from TrueType or OpenType formats on their web sites) via the new <canvas> element in all newer browsers except Internet Explorer and via VML in Internet Explorer.

The main problem with both (as of now) is that selecting text does not work or at least works only quite awkwardly.

Still, it is very nice for headlines. Body text... I don't know.

And it's surprisingly fast.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thomas
  • 356
  • 4
  • 11
8

Or you could try sIFR. I know it uses Flash, but only if available. If Flash isn't available, it displays the original text in its original (CSS) font.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Casper
  • 1,242
  • 1
  • 11
  • 12
4

The technique that the W3C has recommended for do this is called "embedding" and is well described by the three articles here: Embedding Fonts. In my limited experiments, I have found this process error-prone and have had limited success in making it function in a multi-browser environment.

Steve Moyer
  • 5,663
  • 1
  • 24
  • 34
4

Safari and Internet Explorer both support the CSS @font-face rule, however they support two different embedded font types. Firefox is planning to support the same type as Apple some time soon. SVG can embed fonts but isn't that widely supported yet (without a plugin).

I think the most portable solution I've seen is to use a JavaScript function to replace headings etc. with an image generated and cached on the server with your font of choice -- that way you simply update the text and don't have to stuff around in Photoshop.

zobier
  • 1,579
  • 2
  • 13
  • 11
  • This does not have to be done with JavaScript. I know that a lot of people like to use JavaScript for quite a few stuff nowadays, but a standard CSS technique may be more appropriate here. See http://www.sitepoint.com/article/header-images-css-xhtml/ – hangy Sep 20 '08 at 11:50
  • I'm sorry if there's confusion, the technique refers not to the method of image replacement which, as you say, can be CSS. It is about generating graphics representing a given string in a particular font/style, on the fly, at the server -- obviating the need to create these manually. – zobier Sep 22 '08 at 05:02
4

If you use ASP.NET, it's really easy to generate image based fonts without actually having to install (as in adding to the installed font base) fonts on the server by using:

PrivateFontCollection pfont = new PrivateFontCollection();
pfont.AddFontFile(filename);
FontFamily ff = pfont.Families[0];

and then drawing with that font onto a Graphics.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mattlant
  • 15,384
  • 4
  • 34
  • 44
  • 1
    well, yes... And you user can forget about copying and pasting. And zooming too, in old browsers. I don't even talk about bandwidth issues ! – Bite code Sep 22 '08 at 09:19
  • 2
    Yeah, ok, and the question specified that it was going to be using it for content text? No it didnt, it was a general question, and this was a general answer. Small snippets of text, headers, etc, dont take up lots of bandwidth. Sheesh, get a grip! – mattlant Sep 22 '08 at 12:36
3

It looks like it only works in Internet Explorer, but a quick Google search for "html embed fonts" yields http://www.spoono.com/html/tutorials/tutorial.php?id=19

If you want to stay platform-agnostic (and you should!) you'll have to use images, or else just use a standard font.

James Muscat
  • 454
  • 3
  • 5
3

I did a bit of research and dug up Dynamic Text Replacement (published 2004-06-15).

This technique uses images, but it appears to be "hands free". You write your text, and you let a few automated scripts do automated find-and-replace on the page for you on the fly.

It has some limitations, but it is probably one of the easier choices (and more browser compatible) than all the rest I've seen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kent Fredric
  • 56,416
  • 14
  • 107
  • 150
3

It is also possible to use WOFF fonts - example here

@font-face {
font-family: 'Plakat Fraktur';
src: url('/resources/fonts/plakat-fraktur-black-modified.woff') format('woff');
font-weight: bold;
font-style: normal;
 }
Wilf
  • 713
  • 1
  • 11
  • 26
3

Just simply provide the link to actual font like this and you will be good to go

<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Montserrat'   rel='stylesheet'>
<style>
body {
font-family: 'Montserrat';font-size: 22px;
}
</style>
</head>
<body>

<h1>Montserrat</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>


</body>
</html>
  • A good simple alternative. And you're sure to have the rights to use it. See https://developers.google.com/fonts/docs/getting_started – Tim Erickson May 25 '18 at 21:42
2

See the article 50 Useful Design Tools For Beautiful Web Typography for alternative methods.

I have only used Cufon. I have found it reliable and very easy to use, so I've stuck with it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blair
  • 3,671
  • 4
  • 29
  • 40
2

Typeface.js JavaScript Way:

With typeface.js you can embed custom fonts in your web pages so you don't have to render text to images

Instead of creating images or using flash just to show your site's graphic text in the font you want, you can use typeface.js and write in plain HTML and CSS, just as if your visitors had the font installed locally.

http://typeface.neocracy.org/

bakkal
  • 54,350
  • 12
  • 131
  • 107
2

If you have a file of your font, then you will need to add more formats of that font for other browsers.

For this purpose I use font generator like Fontsquirrel it provides all the font formats & its @font-face CSS, you will only need to just drag & drop it into your CSS file.

saadeez
  • 1,588
  • 3
  • 11
  • 17
1
@font-face {
font-family: "CustomFont";
src: url("CustomFont.eot");
src: url("CustomFont.woff") format("woff"),
url("CustomFont.otf") format("opentype"),
url("CustomFont.svg#filename") format("svg");
}
Hello Hack
  • 107
  • 5
  • 1
    Please explain why/how this solves the problem. See [How To Answer](https://stackoverflow.com/help/how-to-answer) – jasie Sep 30 '20 at 05:50
0

easy solution is to use @fontface in css

@font-face {
font-family: myFirstFont;
src: url(fileLocation);} 

div{
    font-family: myfirstfont;}
Ajay Sahu
  • 186
  • 1
  • 7
  • Please ensure, that your answer adds **new insights** or **refined approaches** based on previous answers. If your answer contains a subtle but relevant benefit – please, **elaborate** on it. If it doesn't please consider to remove your answer. – herrstrietzel Dec 02 '22 at 00:52
0

You can use @import url(url) to import web fonts. You must replace url with the font source (full web source).