Local Fallback for Google Web Fonts

后端 未结 3 458
無奈伤痛
無奈伤痛 2020-12-28 20:18

The HTML5 Boilerplate uses a script for loading a local copy of jQuery if, for whatever reason, the Google CDN version fails to load:



        
相关标签:
3条回答
  • 2020-12-28 21:04

    I've just had this problem and thought of a solution just after I came to this page:

    @import url(http://fonts.googleapis.com/css?family=Ubuntu:400,400italic,700,700italic);
    
    @font-face {
        font-family: "UbuntuFallback";
        font-style: normal;
        font-weight: normal;
        src: url("/webfonts/ubuntu/ubuntu-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-webfont.ttf") format("truetype");
    }
    @font-face {
        font-family: "UbuntuFallback";
        font-style: normal;
        font-weight: bold;
        src: url("/webfonts/ubuntu/ubuntu-bold-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-bold-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-bold-webfont.ttf") format("truetype");
    }
    @font-face {
        font-family: "UbuntuFallback";
        font-style: italic;
        font-weight: normal;
        src: url("/webfonts/ubuntu/ubuntu-italic-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-italic-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-italic-webfont.ttf") format("truetype");
    }
    @font-face {
        font-family: "UbuntuFallback";
        font-style: italic;
        font-weight: bold;
        src: url("/webfonts/ubuntu/ubuntu-bolditalic-webfont.eot?#iefix") format("embedded-opentype"), url("/webfonts/ubuntu/ubuntu-bolditalic-webfont.woff") format("woff"), url("/webfonts/ubuntu/ubuntu-bolditalic-webfont.ttf") format("truetype");
    }
    
    body 
    {
        font-family: Ubuntu, UbuntuFallback, Tahoma, Sans-Serif;
    }
    

    So I wanted to use the Ubuntu font, however our website is run on a localhost and won't necessarily be connected to the internet. I created some @font-face declarations to the Ubuntu font, called it something else ("UbuntuFallback") and just put it in the font-family style list.

    Voilá!

    0 讨论(0)
  • 2020-12-28 21:04

    A client-side fallback calling a css file containing the call different fonts (here Tangerine font):

    (function test($){
        var $span = $('<span style="display:none;font-family:Tangerine"></span>').appendTo('body'); // span test creation
        if ($span.css('fontFamily') !== 'Tangerine'){
            $('head').append('<link href="./Styles/Public/Fonts.css" rel="stylesheet">'); // fallback link
        }
        $span.remove(); // span test remove
    })(jQuery);
    
    0 讨论(0)
  • 2020-12-28 21:05

    If you download all the necessary webfonts and store it e.g. in a local file Webfonts.css you could do something like this:

    <script type="text/javascript">
    if (window.jQuery) {
        document.write(unescape('%3Clink href="http://fonts.googleapis.com/css?family=OFL+Sorts+Mill+Goudy+TT|Yanone+Kaffeesatz|Tangerine" rel="stylesheet" type="text/css"%3E'));
    }
    else {
       document.write(unescape('%3Clink href="css/WebFonts.css"    rel="stylesheet" type="text/css"%3E'));
       document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')
    }
    </script>
    

    Note: This is assuming that the cdn's fonts.googleapis.com and ajax.googleapis.com fail at the same time..

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