htmlcssfontsfont-face

How to add some non-standard font to a website?


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?


Solution

  • This could be done via CSS @font-face:

    @font-face is supported by all modern browsers and allows multiple format fallback references as a comma-separated list defined in the src property:

    @font-face {
        font-family: "Custom Font";
        src: url("mycustomfont.woff2") format("woff2"),
        url("mycustomfont.woff") format("woff"),
        url("mycustomfont.ttf") format("truetype");
    }
    

    Multiple format fallbacks

    Browsers load the first valid font file candidate specified in the src property. The fallback font-stack should therefore prioritize the most modern (and compact) – woff2 (in 2025)

    Obsolete formats

    These formats became deprecated or obsolete

    Quote enclosing

    In short: Prefer enclosing.
    Single or double quotes can be omitted for:

    Format identifiers

    Most browsers can apply a font without format identifiers such as format("woff2"). However, it's recommended to add these identifiers to facilitate the font parsing process. Syntax errors can break the rule/font-mapping!

    Common typos are related to legacy formats where extensions don't match the identifier:

    Identifiers for "experimental" features

    You may encounter format identifiers in documentations like format("woff2-variations") - these identifiers for variable fonts served a similar purpose as browser-prefixes. If most browser supports a certain feature like variable fonts – these extra identifiers are not required.

    Further reading