【转】CSS3属性 @font-face 整理

时间:2023-03-09 09:48:03
【转】CSS3属性 @font-face 整理

原文: http://www.w3cplus.com/content/css3-font-face

出自: w3cplus.com

一、语法规则

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

  1.取值说明:
  YourWebFontName是你自定义的字体名称;
  source是你自定义的字体存放的路径,可以使用相对路径也可以是使用绝对路径;
  format是你自定义的字体的格式,主要用来帮助浏览器识别,其值主要有以下几种类型:truetype,opentype,truetype-aat,embedded-opentype,avg等;

  2.实例:

   /*定义*/
@font-face
{
font-family: myFirstFont;
src: url('Sansation_Light.ttf'),
url('Sansation_Light.eot');
}
   /*使用*/
div{
font-family:myFirstFont;
}

二、浏览器支持

  Firefox、Chrome、Safari 以及 Opera 支持 .ttf (True Type Fonts) 和 .otf (OpenType Fonts) 类型的字体。
  Internet Explorer 9+ 支持新的 @font-face 规则,但是仅支持 .eot 类型的字体 (Embedded OpenType)。
  注释:Internet Explorer 8 以及更早的版本不支持新的 @font-face 规则。

三、format()格式

  1.TureTpe(.ttf)格式:
    .ttf字体是Windows和Mac的最常见的字体,是一种RAW格式,因此它不为网站优化;支持这种字体的浏览器有【IE9+,Firefox3.5+,Chrome4+,Safari3+,Opera10+,iOS Mobile Safari4.2+】;
  2.OpenType(.otf)格式:
    .otf字体被认为是一种原始的字体格式,其内置在TureType的基础上,所以也提供了更多的功能;支持这种字体的浏览器有【Firefox3.5+,Chrome4.0+,Safari3.1+,Opera10.0+,iOS Mobile Safari4.2+】;
  3.Web Open Font Format(.woff)格式:
  .woff字体是Web字体中最佳格式,他是一个开放的TrueType/OpenType的压缩版本,同时也支持元数据包的分离,支持这种字体的浏览器有【IE9+,Firefox3.5+,Chrome6+,Safari3.6+,Opera11.1+】;
  4.Embedded Open Type(.eot)格式:
    .eot字体是IE专用字体,可以从TrueType创建此格式字体,支持这种字体的浏览器有【IE4+】;
  5.SVG(.svg)格式:
    .svg字体是基于SVG字体渲染的一种格式,支持这种字体的浏览器有【Chrome4+,Safari3.1+,Opera10.0+,iOS Mobile Safari3.2+】。

  这就意味着在@font-face中我们至少需要.woff,.eot两种格式字体,甚至还需要.svg等字体达到更多种浏览版本的支持。

四、实践

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

 @font-face {
font-family: 'YourWebFontName';
src: url('YourWebFontName.eot?') 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 */
font-weight: normal;
font-style: normal;
}

五、字体下载网址   

  http://www.dafont.com/
  https://fonts.google.com/ (需*使用)