html - 导入 html 字体及其样式变体?

标签 html css fonts cross-browser

我正在我的网站中使用名为 Roboto 的谷歌字体。

我需要 2 种类型:boldregular

enter image description here

所以我检查了两种类型并按下了下载按钮:

enter image description here

但是在下载的 rar 文件中我得到了所有的字体样式(还有那些我没有选择的):

enter image description here

无论如何,我想测试常规字体:(字体现在在我的站点中,而不是从谷歌加载)。

(我使用字体转换器 (http://www.font2web.com/) 获得了其他扩展类型(eot、woff、svg))

所以我做到了:

 <style type="text/css">

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


   body  { font-family: 'Roboto', sans-serif; }

    </style>

问题:

假设我想将 Roboto bold 样式应用于 div

我应该这样做吗:

 div  { 
       font-family: 'Roboto', sans-serif;
       font-weight:bold
      }

或者我应该这样做(重新开始...)

@font-face {
    font-family: 'Roboto-bold';
    src: url('/font-face/Regular/Roboto-bold.eot'); /* IE9 Compat Modes */
    ...
    }

然后

 div  { font-family: 'Roboto-bold', sans-serif; }

最佳答案

这是你必须做的:

@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: /* links to the Regular files */;
}
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 700;
  src: /* links to the Bold files */;
}

请注意如何在两个 @font-face 规则中使用相同的字体名称。现在浏览器知道字体“Roboto”有两种变体。浏览器会根据您的 CSS 自动选择最佳变体。所以,例如:

div {
    font-family: Roboto, sans-serif;
    font-weight: bold;
}

浏览器在这里选择了 Bold 字体文件。都是自动的。您只需确保正确设置了 @font-face 规则。

关于html - 导入 html 字体及其样式变体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17166060/

相关文章:

html - 很棒的字体在 firefox 中不显示一个图标,但在 chrome 中运行完美

html - 复合背景边框的奇怪行为

html - 表单(方法 ='get' )适用于 Node.js,但状态 : pending in dev-tools

css - 如何将我的导航栏放在图像上?

html - 具有指定高度(在 CSS 中)的 div 可以自动缩放吗?

css - 行高问题

css - SVG 和带有斜线的稀有特殊字符( Angular 删除线?)

html - 按钮的静态宽度,textarea 的静态宽度

PHP - 如何用另一个短语替换一个短语?

javascript - 如何设置图像宽度/高度