When do web-fonts load and can you pre-load them?

后端 未结 1 1708
萌比男神i
萌比男神i 2020-12-08 20:21

I\'ve noticed when using web fonts that they can initially can take a second to come up; like if you create a drop down nav menu, when you hover over the menu for the first

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 20:40

    When are webfonts downloaded?

    Paul Irish made a simple page to test this: http://dl.getdropbox.com/u/39519/webfontsdemo/loadtest.html

    It shows that most browsers download fonts when they're used in a page rather than when they're declared in CSS. I believe IE is the exception but I don't have it running to test right now.

    The reason for downloading on usage rather than on declaration is to reduce unnecessary network traffic, e.g. if a font is declared but not used.

    Can font downloading be avoided?

    You're right that if fonts are already installed they shouldn't need to be downloaded. As @Patrick said above, this can be done using local(). It's placed before url() in the CSS and takes the name of the font (the PostScript name is subsequently needed for Safari on Mac OS X). Try out the following change to your CSS:

    @font-face {
        font-family: 'Roboto';
        src: url('../fonts/Roboto-Regular-webfont.eot');
        src: local(Roboto Regular), local(Roboto-Regular),
             url('../fonts/Roboto-Regular-webfont.eot?#iefix') format('embedded-opentype'),
             url('../fonts/Roboto-Regular-webfont.woff') format('woff'),
             url('../fonts/Roboto-Regular-webfont.ttf') format('truetype'),
             url('../fonts/Roboto-Regular-webfont.svg#RobotoRegular') format('svg');
        font-weight: normal;
        font-style: normal;
    }
    

    Finally, to reduce font download times, you could make sure you're doing the following:

    • Putting CSS before JavaScript
    • Adding far-future Expires headers for the fonts (so the browser caches them)
    • GZipping the fonts

    Here's a good summary of dealing with font display delays: http://paulirish.com/2009/fighting-the-font-face-fout/

    0 讨论(0)
提交回复
热议问题