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

后端 未结 19 1864
时光说笑
时光说笑 2020-11-22 02:17

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

19条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 03:01

    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

    • Using Font Face (Very recommended)
    • Bulletproof @font-face syntax
    • Can i use @font-face web fonts ?
    • How to archive Cross-Browser @font-face support
    • @font-face at the CSS Mozilla Developer Network

提交回复
热议问题