在CSS中使用多个@ font-face规则

Tim*_*Tim 66 html css fonts webfonts font-face

我怎样才能@font-face在CSS中使用超过规则?

我已将其插入到样式表中:

body {
    background: #fff url(../images/body-bg-corporate.gif) repeat-x;
    padding-bottom: 10px;
    font-family: 'GestaRegular', Arial, Helvetica, sans-serif;
}

@font-face {
    font-family: 'GestaReFogular';
    src: url('gestareg-webfont.eot');
    src: local('?'),
         url('gestareg-webfont.woff') format('woff'),
         url('gestareg-webfont.ttf') format('truetype'),
         url('gestareg-webfont.svg#webfontg8dbVmxj') format('svg');
}
Run Code Online (Sandbox Code Playgroud)

这目前仅适用于网站上的整个文本.但是,我想指定h1使用不同的字体.我怎样才能做到这一点?

Jar*_*ish 86

注意,您可能还对以下内容感兴趣:

自定义Web字体在IE9中不起作用

其中包括您在下面看到的CSS的更具描述性的细分(并解释了使其在IE6-9上更好地工作的调整).


@font-face {
  font-family: 'Bumble Bee';
  src: url('bumblebee-webfont.eot');
  src: local('?'), 
       url('bumblebee-webfont.woff') format('woff'), 
       url('bumblebee-webfont.ttf') format('truetype'), 
       url('bumblebee-webfont.svg#webfontg8dbVmxj') format('svg');
}

@font-face {
  font-family: 'GestaReFogular';
  src: url('gestareg-webfont.eot');
  src: local('?'), 
       url('gestareg-webfont.woff') format('woff'), 
       url('gestareg-webfont.ttf') format('truetype'), 
       url('gestareg-webfont.svg#webfontg8dbVmxj') format('svg');
}

body {
  background: #fff url(../images/body-bg-corporate.gif) repeat-x;
  padding-bottom: 10px;
  font-family: 'GestaRegular', Arial, Helvetica, sans-serif;
}

h1 {
  font-family: "Bumble Bee", "Times New Roman", Georgia, Serif;
}
Run Code Online (Sandbox Code Playgroud)

以及你的后续问题:

问:我想使用诸如"大黄蜂"之类的字体.如何@font-face在用户的计算机上使用该字体?

请注意,我不知道您的Bumble Bee字体或文件的名称是什么,因此请相应地进行调整,并且字体 - 面部声明应该在您使用它之前(之前),如上所示.

问:我还可以使用其他@font-face字体"GestaRegular"吗?我可以在同一样式表中使用它们吗?

正如我在我的例子中所示,将它们列在一起.没有理由你不能同时宣布这两个.所有这一切@font-face都是指示浏览器下载并使字体系列可用.请参阅:http://iliadraznin.com/2009/07/css3-font-face-multiple-weights


Sur*_*een 9

@font-face {
    font-family: Kaffeesatz;
    src: url(YanoneKaffeesatz-Thin.otf);
    font-weight: 200;
}
@font-face {
    font-family: Kaffeesatz;
    src: url(YanoneKaffeesatz-Light.otf);
    font-weight: 300;
}
@font-face {
    font-family: Kaffeesatz;
    src: url(YanoneKaffeesatz-Regular.otf);
    font-weight: normal;
}
@font-face {
    font-family: Kaffeesatz;
    src: url(YanoneKaffeesatz-Bold.otf);
    font-weight: bold;
}
h3, h4, h5, h6 {
    font-size:2em;
    margin:0;
    padding:0;
    font-family:Kaffeesatz;
    font-weight:normal;
}
h6 { font-weight:200; }
h5 { font-weight:300; }
h4 { font-weight:normal; }
h3 { font-weight:bold; }
Run Code Online (Sandbox Code Playgroud)


小智 6

可以通过更改 @font-face 规则的 font-weight 和 src 属性来声明字体系列的多种变体。

/* Regular Weight */
@font-face {
    font-family: Montserrat;
    src: url("../fonts/Montserrat-Regular.ttf");
}

/* SemiBold (600) Weight */
@font-face {
    font-family: Montserrat;
    src: url("../fonts/Montserrat-SemiBold.ttf");
    font-weight: 600;
}

/* Bold Weight */
@font-face {
    font-family: Montserrat;
    src: url("../fonts/Montserrat-Bold.ttf");
    font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)

可以通过以下方式使用声明的规则

/* Regular */
font-family: Montserrat;


/* Semi Bold */
font-family: Montserrat;
font-weght: 600;

/* Bold */
font-family: Montserrat;
font-weight: bold;
Run Code Online (Sandbox Code Playgroud)