我想在我的网站上使用谷歌的Roboto字体,我正在遵循这个教程:

http://www.maketecheasier.com/use-google-roboto-font-everywhere/2012/03/15

我下载了一个文件夹结构如下所示的文件:

现在我有三个问题:

我有css在我的media/css/main.css url。我需要把文件夹放在哪里呢? 我需要提取所有的eot,svg等从所有子文件夹,并放在字体文件夹? 我需要创建css文件fonts.css和包括在我的基本模板文件?

他的例子是这样的

@font-face {
    font-family: 'Roboto';
    src: url('Roboto-ThinItalic-webfont.eot');
    src: url('Roboto-ThinItalic-webfont.eot?#iefix') format('embedded-opentype'),
         url('Roboto-ThinItalic-webfont.woff') format('woff'),
         url('Roboto-ThinItalic-webfont.ttf') format('truetype'),
         url('Roboto-ThinItalic-webfont.svg#RobotoThinItalic') format('svg'); (under the Apache Software License). 
    font-weight: 200;
    font-style: italic;
}

我的url应该是什么样的,如果我想有dir结构像:

/media/fonts/roboto


当前回答

有两种方法,你可以采取在你的网页上使用授权网页字体:

1. 字体托管服务,如Typekit, Fonts.com, Fontdeck等。

这些服务为设计人员提供了一个简单的界面来管理所购买的字体,并生成指向提供字体的动态CSS或JavaScript文件的链接。谷歌甚至免费提供这项服务(这里是你要求的Roboto字体的一个例子)。

JS字体加载器,如谷歌和Typekit使用的字体加载器(即WebFont加载器)提供CSS类和回调来帮助管理可能发生的FOUT,或下载字体时的响应超时。

<head>
  <!-- get the required files from 3rd party sources -->
  <link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
  <!-- use the font -->
  <style>
    body {
      font-family: 'Roboto', sans-serif;
      font-size: 48px;
    }
  </style>
</head>

2. DIY方法

这包括获得字体的网络使用许可,并(可选地)使用FontSquirrel的生成器(或一些软件)等工具来优化文件大小。然后,使用标准的@font-face CSS属性的跨浏览器实现来启用字体。

这种方法可以提供更好的加载性能,因为您可以更细粒度地控制要包含的字符,从而控制文件大小。

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

/* use the font */
body {
  font-family: 'Roboto', sans-serif;
  font-size: 48px;
}

TLDR;

在你的网站上嵌入自定义字体有两种主要方法。使用字体托管服务和@font-face声明可以在整体性能、兼容性和可用性方面提供最佳输出。

来源:https://www.artzstudio.com/2012/02/web-font-performance-weighing-fontface-options-and-alternatives/(链接无效)


更新

Roboto:谷歌的签名字体现在是开源的。您现在可以手动生成Roboto字体使用说明,可以在这里找到。

其他回答

花了一个小时,解决了字体问题。

相关答案-以下是React.js网站:

Install the npm module: npm install --save typeface-roboto-mono import in .js file you want to use one of the below: import "typeface-roboto-mono"; // if import is supported require('typeface-roboto-mono') // if import is not supported For the element you can use one of the below: fontFamily: "Roboto Mono, Menlo, Monaco, Courier, monospace", // material-ui font-family: Roboto Mono, Menlo, Monaco, Courier, monospace; /* css */ style="font-family:Roboto Mono, Menlo, Monaco, Courier, monospace;font-weight:300;" /*inline css*/

希望这能有所帮助。

你看过压缩文件里的How_To_Use_Webfonts.html了吗?

在阅读之后,似乎每个字体子文件夹都有一个已经创建的.css,你可以通过包括以下内容来使用:

<link rel="stylesheet" href="stylesheet.css" type="text/css" charset="utf-8" />

用css:

@font-face {
  font-family: 'Roboto';
  src: url('../font/Roboto-Regular.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
}

/* etc, etc. */

萨斯:

  @font-face
    font-family: 'Roboto'
    src: local('Roboto'), local('Roboto-Regular'), url('../fonts/Roboto-Regular.ttf') format('truetype')
    font-weight: normal
    font-style: normal

  @font-face
    font-family: 'Roboto'
    src: local('Roboto Bold'), local('Roboto-Bold'), url('../fonts/Roboto-Bold.ttf') format('truetype')
    font-weight: bold
    font-style: normal

  @font-face
    font-family: 'Roboto'
    src: local('Roboto Italic'), local('Roboto-Italic'), url('../fonts/Roboto-Italic.ttf') format('truetype')
    font-weight: normal
    font-style: italic

  @font-face
    font-family: 'Roboto'
    src: local('Roboto BoldItalic'), local('Roboto-BoldItalic'), url('../fonts/Roboto-BoldItalic.ttf') format('truetype')
    font-weight: bold
    font-style: italic

  @font-face
    font-family: 'Roboto'
    src: local('Roboto Light'), local('Roboto-Light'), url('../fonts/Roboto-Light.ttf') format('truetype')
    font-weight: 300
    font-style: normal

  @font-face
    font-family: 'Roboto'
    src: local('Roboto LightItalic'), local('Roboto-LightItalic'), url('../fonts/Roboto-LightItalic.ttf') format('truetype')
    font-weight: 300
    font-style: italic

  @font-face
    font-family: 'Roboto'
    src: local('Roboto Medium'), local('Roboto-Medium'), url('../fonts/Roboto-Medium.ttf') format('truetype')
    font-weight: 500
    font-style: normal

  @font-face
    font-family: 'Roboto'
    src: local('Roboto MediumItalic'), local('Roboto-MediumItalic'), url('../fonts/Roboto-MediumItalic.ttf') format('truetype')
    font-weight: 500
    font-style: italic

/* Roboto-Regular.ttf       400 */
/* Roboto-Bold.ttf          700 */
/* Roboto-Italic.ttf        400 */
/* Roboto-BoldItalic.ttf    700 */
/* Roboto-Medium.ttf        500 */
/* Roboto-MediumItalic.ttf  500 */
/* Roboto-Light.ttf         300 */
/* Roboto-LightItalic.ttf   300 */

/* https://fonts.google.com/specimen/Roboto#standard-styles */

有两种方法,你可以采取在你的网页上使用授权网页字体:

1. 字体托管服务,如Typekit, Fonts.com, Fontdeck等。

这些服务为设计人员提供了一个简单的界面来管理所购买的字体,并生成指向提供字体的动态CSS或JavaScript文件的链接。谷歌甚至免费提供这项服务(这里是你要求的Roboto字体的一个例子)。

JS字体加载器,如谷歌和Typekit使用的字体加载器(即WebFont加载器)提供CSS类和回调来帮助管理可能发生的FOUT,或下载字体时的响应超时。

<head>
  <!-- get the required files from 3rd party sources -->
  <link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
  <!-- use the font -->
  <style>
    body {
      font-family: 'Roboto', sans-serif;
      font-size: 48px;
    }
  </style>
</head>

2. DIY方法

这包括获得字体的网络使用许可,并(可选地)使用FontSquirrel的生成器(或一些软件)等工具来优化文件大小。然后,使用标准的@font-face CSS属性的跨浏览器实现来启用字体。

这种方法可以提供更好的加载性能,因为您可以更细粒度地控制要包含的字符,从而控制文件大小。

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

/* use the font */
body {
  font-family: 'Roboto', sans-serif;
  font-size: 48px;
}

TLDR;

在你的网站上嵌入自定义字体有两种主要方法。使用字体托管服务和@font-face声明可以在整体性能、兼容性和可用性方面提供最佳输出。

来源:https://www.artzstudio.com/2012/02/web-font-performance-weighing-fontface-options-and-alternatives/(链接无效)


更新

Roboto:谷歌的签名字体现在是开源的。您现在可以手动生成Roboto字体使用说明,可以在这里找到。

老帖子了,我知道。

这也可以使用CSS @import url:

@import url(http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300ita‌​lic,400italic,500,500italic,700,700italic,900italic,900);
html, body, html * {
  font-family: 'Roboto', sans-serif;
}