How do I use .woff fonts for my website?

前端 未结 2 874
囚心锁ツ
囚心锁ツ 2020-12-12 11:59

Where do you place fonts so that CSS can access them?

I am using non-standard fonts for the browser in a .woff file. Let\'s say its \'awesome-font\' stored in a file

2条回答
  •  自闭症患者
    2020-12-12 12:24

    After generation of woff files, you have to define font-family, which can be used later in all your css styles. Below is the code to define font families (for normal, bold, bold-italic, italic) typefaces. It is assumed, that there are 4 *.woff files (for mentioned typefaces), placed in fonts subdirectory.

    In CSS code:

    @font-face {
        font-family: "myfont";
        src: url("fonts/awesome-font.woff") format('woff');
    }
    
    @font-face {
        font-family: "myfont";
        src: url("fonts/awesome-font-bold.woff") format('woff');
        font-weight: bold;
    }
    
    @font-face {
        font-family: "myfont";
        src: url("fonts/awesome-font-boldoblique.woff") format('woff');
        font-weight: bold;
        font-style: italic;
    }
    
    @font-face {
        font-family: "myfont";
        src: url("fonts/awesome-font-oblique.woff") format('woff');
        font-style: italic;
    }
    

    After having that definitions, you can just write, for example,

    In HTML code:

    this will be written with awesome-font-bold.woff
    this will be written with awesome-font-boldoblique.woff
    this will be written with awesome-font-oblique.woff
    this will be written with awesome-font.woff

    In CSS code:

    .mydiv {
        font-family: myfont
    }
    

    The good tool for generation woff files, which can be included in CSS stylesheets is located here. Not all woff files work correctly under latest Firefox versions, and this generator produces 'correct' fonts.

提交回复
热议问题