tianma3798

  @font-faceCSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体就不用再为只能使用Web安全字体烦恼了!肯定会有人问,这样的东西IE能支持吗?我告诉大家@font-face这个功能其实早在IE4就支持了,你肯定会感到惊讶。如果你看到一些英文网站或blog看到一些很漂亮的自定义Web字体,比如说首页的Logo,Tags以及页面中的手写英文体,一句话这些都是@font-face实现的,为了能让更多的朋友知道如何使用他,今天我主要把自己的一点学习过程贴上来和大家分享。

首先我们一起来看看@font-face的语法规则:

@font-face {
       font-family: <YourWebFontName>;
       src: <source> [<format>][,<source> [<format>]]*;
       [font-weight: <weight>];
       [font-style: <style>];
     }

取值说明

  1. YourWebFontName:此值指的就是你自定义的字体名称,最好是使用你下载的默认字体(下载回来的叫什么字体,这里就填什么字体名),他将被引用到你的Web元素中的font-family。如“font-family:"YourWebFontName";”
  2. source:此值指的是你自定义的字体的存放路径,可以是相对路径也可以是绝路径;
  3. format:此值指的是你自定义的字体的格式,主要用来帮助浏览器识别,其值主要有以下几种类型:truetype,opentype,truetype-aat,embedded-opentype,svg等;
  4. weight和style:这两个值大家一定很熟悉,weight定义字体是否为粗体,style主要定义字体样式,如斜体。

为了使@font-face达到更多的浏览器支持,Paul Irish写了一个独特的@font-face语法叫Bulletproof @font-face:

   @font-face {
    font-family: \'YourWebFontName\';
    src: url(\'YourWebFontName.eot?#iefix\') format(\'eot\');/*IE*/
    src:url(\'YourWebFontName.woff\') format(\'woff\'), url(\'YourWebFontName.ttf\') format(\'truetype\');/*non-IE*/
   }

但为了让各多的浏览器支持,你也可以写成:

   @font-face {
    font-family: \'YourWebFontName\';
    src: url(\'YourWebFontName.eot\'); /* IE9 Compat Modes */
    src: url(\'YourWebFontName.eot?#iefix\') format(\'embedded-opentype\'), /* IE6-IE8 */
             url(\'YourWebFontName.woff\') format(\'woff\'), /* Modern Browsers */
             url(\'YourWebFontName.ttf\')  format(\'truetype\'), /* Safari, Android, iOS */
             url(\'YourWebFontName.svg#YourWebFontName\') format(\'svg\'); /* Legacy iOS */
   }

说了这么多空洞的理论知识,大家一定想自己动手试试的,实例demo:

HTML Code:

<h2 class="neuesDemo">Neues Bauen Demo</h2>

通过@font-face来定义自己的Web Font:

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

我在这里采用的是相对路径,当然大家也可以使用绝路径。到这里我们就需要把定义好的字体应用到我们实际页面中去:

h2.neuesDemo {
       font-family: \'NeuesBauenDemo\'
    }

获取特殊字体:

我平时都是到Google Web Fonts和Dafont.com寻找自己需要的字体,当然网上也还有别的下载字体的地方,如:Webfonts,Typekit,Kernest,Google Web Fonts,Kernest,Dafont,Niec Web Type,不然你点这里将有更多的免费字体。前面几个链接是帮助你获取一些优美的怪异的特殊字体!

Web字体库转换工具:

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-17
  • 2021-12-12
  • 2021-05-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-23
  • 2022-03-06
  • 2021-08-27
  • 2021-08-20
  • 2022-12-23
相关资源
相似解决方案