如何为同一个字体添加多个字体文件?

发布于 2024-08-25 08:09:08 字数 533 浏览 6 评论 0原文

我正在查看 @font-face CSS 规则的 MDC 页面 ,但我不明白一件事。我有粗体斜体粗体+斜体的单独文件。如何将所有三个文件嵌入到一个 @font-face 规则中?例如,如果我有:

@font-face {
    font-family: "DejaVu Sans";
    src: url("./fonts/DejaVuSans.ttf") format("ttf");
}
strong {
    font-family: "DejaVu Sans";
    font-weight: bold;
}

浏览器将不知道使用哪种字体作为粗体(因为该文件是 DejaVuSansBold.ttf),因此它将默认为我可能不想要的字体。如何告诉浏览器某种字体的所有不同变体?

I'm looking at the MDC page for the @font-face CSS rule, but I don't get one thing. I have separate files for bold, italic and bold + italic. How can I embed all three files in one @font-face rule? For example, if I have:

@font-face {
    font-family: "DejaVu Sans";
    src: url("./fonts/DejaVuSans.ttf") format("ttf");
}
strong {
    font-family: "DejaVu Sans";
    font-weight: bold;
}

The browser will not know which font to be used for bold (because that file is DejaVuSansBold.ttf), so it will default to something I probably don't want. How can I tell the browser all the different variants I have for a certain font?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

海螺姑娘 2024-09-01 08:09:08

解决方案似乎是添加多个 @font-face 规则,例如:

@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans.ttf");
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Bold.ttf");
    font-weight: bold;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Oblique.ttf");
    font-style: italic, oblique;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-BoldOblique.ttf");
    font-weight: bold;
    font-style: italic, oblique;
}

顺便说一句,Google Chrome 似乎不知道 format("ttf") 参数,所以你可能想跳过它。

(此答案对于 CSS 2 规范是正确的。CSS3 只允许一种字体-样式而不是逗号分隔的列表。)

The solution seems to be to add multiple @font-face rules, for example:

@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans.ttf");
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Bold.ttf");
    font-weight: bold;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Oblique.ttf");
    font-style: italic, oblique;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-BoldOblique.ttf");
    font-weight: bold;
    font-style: italic, oblique;
}

By the way, it would seem Google Chrome doesn't know about the format("ttf") argument, so you might want to skip that.

(This answer was correct for the CSS 2 specification. CSS3 only allows for one font-style rather than a comma-separated list.)

琉璃梦幻 2024-09-01 08:09:08

从 CSS3 开始,规范已发生变化,仅允许使用单个 font-style。逗号分隔列表(根据 CSS2)将被视为正常,并覆盖任何早期(默认)条目。这将使以此方式定义的字体永久显示为斜体。

@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans.ttf");
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Bold.ttf");
    font-weight: bold;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Oblique.ttf");
    font-style: italic;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-BoldOblique.ttf");
    font-weight: bold;
    font-style: italic;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Oblique.ttf");
    font-style: oblique;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-BoldOblique.ttf");
    font-weight: bold;
    font-style: oblique;
}

在大多数情况下,斜体可能就足够了,如果您仔细定义并坚持使用斜体,则不需要倾斜规则。

As of CSS3, the spec has changed, allowing for only a single font-style. A comma-separated list (per CSS2) will be treated as if it were normal and override any earlier (default) entry. This will make fonts defined in this way appear italic permanently.

@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans.ttf");
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Bold.ttf");
    font-weight: bold;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Oblique.ttf");
    font-style: italic;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-BoldOblique.ttf");
    font-weight: bold;
    font-style: italic;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-Oblique.ttf");
    font-style: oblique;
}
@font-face {
    font-family: "DejaVu Sans";
    src: url("fonts/DejaVuSans-BoldOblique.ttf");
    font-weight: bold;
    font-style: oblique;
}

In most cases, italic will probably be sufficient and oblique rules won't be necessary if you take care to define whichever you will use and stick to it.

红颜悴 2024-09-01 08:09:08

为了使字体变化正常工作,我必须颠倒 CSS 中 @font-face 的顺序。

@font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono-BoldOblique.ttf");
    font-weight: bold;
    font-style: italic, oblique;
}   
@font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono-Oblique.ttf");
    font-style: italic, oblique;
}
@font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono-Bold.ttf");
    font-weight: bold;
}
 @font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono.ttf");
}

To have font variation working correctly, I had to reverse the order of @font-face in CSS.

@font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono-BoldOblique.ttf");
    font-weight: bold;
    font-style: italic, oblique;
}   
@font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono-Oblique.ttf");
    font-style: italic, oblique;
}
@font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono-Bold.ttf");
    font-weight: bold;
}
 @font-face {
    font-family: "DejaVuMono";
    src: url("styles/DejaVuSansMono.ttf");
}
清风不识月 2024-09-01 08:09:08

如今,2017-12-17。
我在 规格
我在 chrome 中进行的测试总是有效,无论顺序如何。

@font-face {
  font-family: 'Font Awesome 5 Free';
  font-weight: 900;
  src: url('#{$fa-font-path}/fa-solid-900.eot');
  src: url('#{$fa-font-path}/fa-solid-900.eot?#iefix') format('embedded-opentype'),
  url('#{$fa-font-path}/fa-solid-900.woff2') format('woff2'),
  url('#{$fa-font-path}/fa-solid-900.woff') format('woff'),
  url('#{$fa-font-path}/fa-solid-900.ttf') format('truetype'),
  url('#{$fa-font-path}/fa-solid-900.svg#fontawesome') format('svg');
}

@font-face {
  font-family: 'Font Awesome 5 Free';
  font-weight: 400;
  src: url('#{$fa-font-path}/fa-regular-400.eot');
  src: url('#{$fa-font-path}/fa-regular-400.eot?#iefix') format('embedded-opentype'),
  url('#{$fa-font-path}/fa-regular-400.woff2') format('woff2'),
  url('#{$fa-font-path}/fa-regular-400.woff') format('woff'),
  url('#{$fa-font-path}/fa-regular-400.ttf') format('truetype'),
  url('#{$fa-font-path}/fa-regular-400.svg#fontawesome') format('svg');
}

nowadays,2017-12-17.
I don't find any description about Font-property-order‘s necessity in spec.
And I test in chrome always works whatever the order is.

@font-face {
  font-family: 'Font Awesome 5 Free';
  font-weight: 900;
  src: url('#{$fa-font-path}/fa-solid-900.eot');
  src: url('#{$fa-font-path}/fa-solid-900.eot?#iefix') format('embedded-opentype'),
  url('#{$fa-font-path}/fa-solid-900.woff2') format('woff2'),
  url('#{$fa-font-path}/fa-solid-900.woff') format('woff'),
  url('#{$fa-font-path}/fa-solid-900.ttf') format('truetype'),
  url('#{$fa-font-path}/fa-solid-900.svg#fontawesome') format('svg');
}

@font-face {
  font-family: 'Font Awesome 5 Free';
  font-weight: 400;
  src: url('#{$fa-font-path}/fa-regular-400.eot');
  src: url('#{$fa-font-path}/fa-regular-400.eot?#iefix') format('embedded-opentype'),
  url('#{$fa-font-path}/fa-regular-400.woff2') format('woff2'),
  url('#{$fa-font-path}/fa-regular-400.woff') format('woff'),
  url('#{$fa-font-path}/fa-regular-400.ttf') format('truetype'),
  url('#{$fa-font-path}/fa-regular-400.svg#fontawesome') format('svg');
}
淤浪 2024-09-01 08:09:08

如果您使用谷歌字体,我会建议以下内容。

如果您希望字体从本地主机或服务器运行,您需要下载文件。

不要下载下载链接中的 ttf 包,而是使用它们提供的实时链接,例如:

http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,300italic,400italic,600italic

将 URL 粘贴到浏览器中,您应该会得到类似于第一个答案的字体声明。

打开提供的 URL,下载并重命名文件。

将更新后的 font-face 声明与 woff 文件的相对路径粘贴到 CSS 中,然后就完成了。

If you are using Google fonts I would suggest the following.

If you want the fonts to run from your localhost or server you need to download the files.

Instead of downloading the ttf packages in the download links, use the live link they provide, for example:

http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,300italic,400italic,600italic

Paste the URL in your browser and you should get a font-face declaration similar to the first answer.

Open the URLs provided, download and rename the files.

Stick the updated font-face declarations with relative paths to the woff files in your CSS, and you are done.

冬天旳寂寞 2024-09-01 08:09:08
/*
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# dejavu sans
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*default version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans.ttf'); /* IE9 Compat Modes */
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'), /* Duplicated name with hyphen */
        url('dejavu/DejaVuSans.ttf') 
        format('truetype');
}
/*bold version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans-Bold.ttf'); 
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'),
        url('dejavu/DejaVuSans-Bold.ttf') 
        format('truetype');
    font-weight: bold;
}
/*italic version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans-Oblique.ttf'); 
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'),
        url('dejavu/DejaVuSans-Oblique.ttf') 
        format('truetype');
    font-style: italic;
}
/*bold italic version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans-BoldOblique.ttf'); 
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'),
        url('dejavu/DejaVuSans-BoldOblique.ttf') 
        format('truetype');
    font-weight: bold;
    font-style: italic;
}
/*
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# dejavu sans
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*default version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans.ttf'); /* IE9 Compat Modes */
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'), /* Duplicated name with hyphen */
        url('dejavu/DejaVuSans.ttf') 
        format('truetype');
}
/*bold version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans-Bold.ttf'); 
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'),
        url('dejavu/DejaVuSans-Bold.ttf') 
        format('truetype');
    font-weight: bold;
}
/*italic version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans-Oblique.ttf'); 
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'),
        url('dejavu/DejaVuSans-Oblique.ttf') 
        format('truetype');
    font-style: italic;
}
/*bold italic version*/
@font-face {
    font-family: 'DejaVu Sans';
    src: url('dejavu/DejaVuSans-BoldOblique.ttf'); 
    src: 
        local('DejaVu Sans'),
        local('DejaVu-Sans'),
        url('dejavu/DejaVuSans-BoldOblique.ttf') 
        format('truetype');
    font-weight: bold;
    font-style: italic;
}
十雾 2024-09-01 08:09:08

我在 styles.less 中添加了这样的自定义字体。在这里,我在“src”中添加了多个 url 作为后备。

您可以在此处阅读更多内容: https:/ /developer.mozilla.org/en-US/docs/Web/CSS/@font-face/src#specifying_fallbacks_for_older_browsers

@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-LightItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-LightItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-LightItalic.otf') format('opentype');
    font-weight: 300;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Light.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Light.woff') format('woff'),
        url('/fonts/EuclidSquare-Light.otf') format('opentype');
    font-weight: 300;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-RegularItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-RegularItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-RegularItalic.otf') format('opentype');
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Regular.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Regular.woff') format('woff'),
        url('/fonts/EuclidSquare-Regular.otf') format('opentype');
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-MediumItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-MediumItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-MediumItalic.otf') format('opentype');
    font-weight: 500;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Medium.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Medium.woff') format('woff'),
        url('/fonts/EuclidSquare-Medium.otf') format('opentype');
    font-weight: 500;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-SemiboldItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-SemiboldItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-SemiboldItalic.otf') format('opentype');
    font-weight: 600;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Semibold.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Semibold.woff') format('woff'),
        url('/fonts/EuclidSquare-Semibold.otf') format('opentype');
    font-weight: 600;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-BoldItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-BoldItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-BoldItalic.otf') format('opentype');
    font-weight: bold;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Bold.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Bold.woff') format('woff'),
        url('/fonts/EuclidSquare-Bold.otf') format('opentype');
    font-weight: bold;
}

body {
    font-family: EuclidSquare, Lato, sans-serif;
}

I added a custom font like this to my styles.less. Here I have added more one url in 'src' as a fallback.

More you can read here: https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/src#specifying_fallbacks_for_older_browsers

@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-LightItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-LightItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-LightItalic.otf') format('opentype');
    font-weight: 300;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Light.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Light.woff') format('woff'),
        url('/fonts/EuclidSquare-Light.otf') format('opentype');
    font-weight: 300;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-RegularItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-RegularItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-RegularItalic.otf') format('opentype');
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Regular.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Regular.woff') format('woff'),
        url('/fonts/EuclidSquare-Regular.otf') format('opentype');
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-MediumItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-MediumItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-MediumItalic.otf') format('opentype');
    font-weight: 500;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Medium.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Medium.woff') format('woff'),
        url('/fonts/EuclidSquare-Medium.otf') format('opentype');
    font-weight: 500;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-SemiboldItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-SemiboldItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-SemiboldItalic.otf') format('opentype');
    font-weight: 600;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Semibold.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Semibold.woff') format('woff'),
        url('/fonts/EuclidSquare-Semibold.otf') format('opentype');
    font-weight: 600;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-BoldItalic.woff2') format('woff2'),
        url('/fonts/EuclidSquare-BoldItalic.woff') format('woff'),
        url('/fonts/EuclidSquare-BoldItalic.otf') format('opentype');
    font-weight: bold;
    font-style: italic, oblique;
}
@font-face {
    font-family: EuclidSquare;
    src: url('/fonts/EuclidSquare-Bold.woff2') format('woff2'),
        url('/fonts/EuclidSquare-Bold.woff') format('woff'),
        url('/fonts/EuclidSquare-Bold.otf') format('opentype');
    font-weight: bold;
}

body {
    font-family: EuclidSquare, Lato, sans-serif;
}

寂寞陪衬 2024-09-01 08:09:08

对于创建 React 应用程序,请参阅我的 其他SO在这里回答

  1. 如果您选择将css文件直接链接到您的public/index.html
@font-face {
  font-family: "FontName"; <---
  font-style: normal; <---
  font-weight: normal;
  src: url("path-to-assets/fonts/FontName.ttf") format("truetype");
}
@font-face {
  font-family: "FontName"; <---
  font-style: italic; <---
  font-weight: normal;
  src: url("path-to-assets/fonts/FontName-Italic.ttf") format("truetype");
}
  1. 如果您选择链接css< /code> 通过 Js 文件进行捆绑:
@font-face {
  font-family: "FontName"; <---
  font-style: normal; <---
  font-weight: normal; /* normal | 300 | 400 | 600 | bold | etc */
  src: url("path-to-assets/fonts/FontName.ttf") format("truetype");
}
@font-face {
  font-family: "FontNameItalic"; <---
  font-style: normal; <----
  font-weight: normal; /* normal | 300 | 400 | 600 | bold | etc */
  src: url("path-to-assets/fonts/FontName-Italic.ttf") format("truetype");
}

For Create React App see my other SO answer here

  1. If you choose to link the css file directly to your public/index.html:
@font-face {
  font-family: "FontName"; <---
  font-style: normal; <---
  font-weight: normal;
  src: url("path-to-assets/fonts/FontName.ttf") format("truetype");
}
@font-face {
  font-family: "FontName"; <---
  font-style: italic; <---
  font-weight: normal;
  src: url("path-to-assets/fonts/FontName-Italic.ttf") format("truetype");
}
  1. If you choose to link the css file via Js for bundling:
@font-face {
  font-family: "FontName"; <---
  font-style: normal; <---
  font-weight: normal; /* normal | 300 | 400 | 600 | bold | etc */
  src: url("path-to-assets/fonts/FontName.ttf") format("truetype");
}
@font-face {
  font-family: "FontNameItalic"; <---
  font-style: normal; <----
  font-weight: normal; /* normal | 300 | 400 | 600 | bold | etc */
  src: url("path-to-assets/fonts/FontName-Italic.ttf") format("truetype");
}
安穩 2024-09-01 08:09:08

如果您使用的是 Google 字体,我建议使用 @import url("@import url('https://fonts.googleapis.com/css2?family=Saira+Condensed:wght@900&display=swap ');") 并指定 font-family: 'Saira Condensed', sans-serif; 作为 CSS 规则。

If you are using a Google Font, I suggest using @import url("@import url('https://fonts.googleapis.com/css2?family=Saira+Condensed:wght@900&display=swap');") and specifying font-family: 'Saira Condensed', sans-serif; as the CSS Rule.

最美的太阳 2024-09-01 08:09:08

@font-face 规则非常无情!

您需要确保语法和格式描述符正确。请参阅W3C 规范
在OP的情况下:

@font-face {
    font-family: "DejaVu Sans";
    src: url("./fonts/DejaVuSans.ttf") format("ttf");
}

不会工作,因为正确的格式描述符是“truetype”而不是“ttf” - 而完全省略描述符会起作用(在大多数现代浏览器中) )。

示例 1:正确的描述符

@font-face {
  font-family: 'Dejavu Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}
@font-face {
  font-family: 'Dejavu Sans';
  font-style: italic;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans-Oblique.ttf) format("truetype");
}
@font-face {
  font-family: 'Dejavu Sans';
  font-style: normal;
  font-weight: 700;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans-Bold.ttf) format("truetype");
}
@font-face {
  font-family: 'Dejavu Sans';
  font-style: italic;
  font-weight: 700;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans-BoldOblique.ttf)
      format("truetype");
}

body{
  font-size:10vmin;
}

.dejavue{
    font-family: 'Dejavu Sans';
}
<p><strong>Ham</strong>burge<em>fons<strong>tiv</strong></em></p>
<p class="dejavue"><strong>Ham</strong>burge<em>fons<strong>tiv</strong></em></p>

例 1.2:错误的描述符——尽管路径是正确的

@font-face {
  font-family: 'Dejavu Sans2';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("ttf");
}

@font-face {
  font-family: 'Dejavu Sans3';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}



body{
  font-size:10vmin;
}

.dejavue2{
    font-family: 'Dejavu Sans2';
}

.dejavue3{
    font-family: 'Dejavu Sans3';
}
<p>Hamburgefonstiv (default serif font)</p>
<p class="dejavue2">Hamburgefonstiv (ttf - won’t work)</p>
<p class="dejavue3">Hamburgefonstiv (truetype - works!)</p>

另一个类似的语法错误是 format("opentype") 而不是 format("otf")

描述符字体格式扩展名
"woff2"WOFF 2.0.woff2
"woff"WOFF 1.0.woff
"truetype"TrueType (glyf).ttf
"opentype"OpenType (CFF, CFF2).otf

多种格式、旧版支持和过时格式

所有现代浏览器都支持最紧凑的 woff2 格式。
但是,您可能仍然需要 truetypes,特别是对于可能无法处理/解码 woff2 或 woff 等 Web 字体容器格式的本机应用程序或转换器(例如 pdf-conversion)。

字体源的顺序:从上到下

如果您需要旧支持,请确保堆叠字体源
最现代/紧凑到遗留如下:

 @font-face {
  font-family: 'Dejavu Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff2"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}

多个源必须用“,”逗号分隔。
浏览器将选择第一个有效的字体文件。换句话说,如果您指定较大的“truetype”文件作为第一个 src,它将加载该文件,尽管它也可以处理更多的 comapct“woff2”候选文件。
(字体规则的顺序并不重要!)。

“Bulletproof @font-face 语法”

如果您需要支持过时的浏览器 Internet Explorer 或非常早期的 Webkit 浏览器 - 请查看 Paul Irish 的帖子

对于现代浏览器:摆脱 .eot.svg 因为它只是使您的规则更容易出现语法错误。

例2.1:字体加载优先级

@font-face {
  font-family: 'Dejavu SansWoff2';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff2"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}

@font-face {
  font-family: 'Dejavu SansTTF';
  font-style: normal;
  font-weight: 400;
  src: 
  url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype"),
  url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff2"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff");
}

body{
  font-size:10vmin;
}


.dejavuTTF{
  font-family: 'Dejavu SansTTF';
}

.dejavuWoff2{
  font-family: 'Dejavu SansWoff2';
}
<p>Hamburgefonstiv</p>

<p class="dejavuWoff2">Hamburgefonstiv</p>
<p class="dejavuTTF">Hamburgefonstiv</p>

本地字体或网络资源

简而言之:始终放弃 local() 方法,因为如果您需要在所有浏览器上提供一致的字体渲染,它是非常不可靠的。 (主要是它不可靠,因为字体系列可以有大量不同的字体名称 - 取决于操作系统和字体版本)。

始终检查您的开发工具中是否有渲染/加载的字体

字体信息应在计算样式视图中显示“网络资源”。此外,网络选项卡应显示已加载字体文件的条目。
如果没有 - 您会看到本地安装的字体并非在所有设备上都可用。
开发工具

network tab

可变字体

当您使用可变字体时,您可以减少 @font-face 规则的数量。通常您最多需要 2 条规则(一条用于常规样式,另一条用于斜体样式)

/* latin */
@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 300 800;
  font-stretch: 75% 100%;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/opensans/v40/mem6YaGs126MiZpBA-UFUK0Zdc0.woff2) format('woff2');
}

/* latin */
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 300 800;
  font-stretch: 75% 100%;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/opensans/v40/mem8YaGs126MiZpBA-UFVZ0b.woff2) format('woff2');
}


body{
  font-family: 'Open Sans';
  font-size:10vmin;
}

.cnd{
  font-stretch:75%;
}
<p><em>Ham</em>Hamburge<strong>fonstiv</strong></p>
<p class="cnd"><em>Ham</em>Hamburge<strong>fonstiv</strong></p>

注意:我们需要通过添加 2 个值(以空格分隔)来指定字体变体范围:

font-weight: 100 1000;
font-stretch: 0% 200%;

Italic、Oblique、Slanted

Italic 和 Oblique 指的是印刷分类。斜体通常具有明显不同的字母形状 - 例如,常规(直立)样式中的两个存储“a”,而不是许多字体中的一个存储设计。
倾斜是指一种设计概念,其中“斜体”看起来更像是倾斜的字母设计 - 事实上它们仍然是手动设计的,但类似于直立设计。 font-style:oblique 已经过时了 – 只需使用 italic 即可。

“倾斜”与可变字体更相关。某些字体包含倾斜角度的无缝插值,可以用作斜体的替代品

@font-face {
  font-family: 'Roboto Flex';
  font-style: oblique 0deg 10deg;
  font-weight: 100 1000;
  font-stretch: 25% 151%;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/robotoflex/v26/NaPLcZLOBv5T3oB7Cb4i0xG2bBVmtU5Wc7yZcSAuwtiL.woff2) format('woff2');
}


body{
  font-size:10vmin;
  font-family: 'Roboto Flex';
}

.slnt1{
  font-style: oblique 3deg
}

.slnt2{
  font-style: oblique 10deg
}
<p>Hamburgefonstiv</p>
<p class="slnt1">Hamburgefonstiv</p>
<p class="slnt2">Hamburgefonstiv</p>

@font-face rules are quite unforgiving!

You need to ensure the syntax and format descriptors are correct. See W3C specs.
In case of the OP:

@font-face {
    font-family: "DejaVu Sans";
    src: url("./fonts/DejaVuSans.ttf") format("ttf");
}

wouldn't work as the correct format descriptor is "truetype" not "ttf" - whereas omitting the descriptor completely would work (in most modern browsers).

Example 1: correct descriptor

@font-face {
  font-family: 'Dejavu Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}
@font-face {
  font-family: 'Dejavu Sans';
  font-style: italic;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans-Oblique.ttf) format("truetype");
}
@font-face {
  font-family: 'Dejavu Sans';
  font-style: normal;
  font-weight: 700;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans-Bold.ttf) format("truetype");
}
@font-face {
  font-family: 'Dejavu Sans';
  font-style: italic;
  font-weight: 700;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans-BoldOblique.ttf)
      format("truetype");
}

body{
  font-size:10vmin;
}

.dejavue{
    font-family: 'Dejavu Sans';
}
<p><strong>Ham</strong>burge<em>fons<strong>tiv</strong></em></p>
<p class="dejavue"><strong>Ham</strong>burge<em>fons<strong>tiv</strong></em></p>

Example 1.2: incorrect descriptor – though paths are correct

@font-face {
  font-family: 'Dejavu Sans2';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("ttf");
}

@font-face {
  font-family: 'Dejavu Sans3';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}



body{
  font-size:10vmin;
}

.dejavue2{
    font-family: 'Dejavu Sans2';
}

.dejavue3{
    font-family: 'Dejavu Sans3';
}
<p>Hamburgefonstiv (default serif font)</p>
<p class="dejavue2">Hamburgefonstiv (ttf - won’t work)</p>
<p class="dejavue3">Hamburgefonstiv (truetype - works!)</p>

Another similar syntax error would be format("opentype") instead of format("otf").

descriptorFont FormatExtension
"woff2"WOFF 2.0.woff2
"woff"WOFF 1.0.woff
"truetype"TrueType (glyf).ttf
"opentype"OpenType (CFF, CFF2).otf

Multiple formats, legacy support and obsolete formats

All modern browsers support the most compact woff2 format.
However, you may still need truetypes especially for native applications or converters (e.g pdf-conversion) that may not be able to handle/decode web font container formats like woff2 or woff.

Order of font sources: from top to bottom

In case you need legacy support make sure to stack the font sources from
most modern/compact to legacy like so:

 @font-face {
  font-family: 'Dejavu Sans';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff2"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}

Multiple sources must be separated by a "," comma.
A browser will choose the first valid font file. In other words if you specify the larger "truetype" file as the first src it will load this file although it can also handle the more comapct "woff2" candidate.
(The order of the font-face rule doesn't matter!).

"Bulletproof @font-face Syntax"

In case you need to support outdated browsers internet explorer or very early webkit browsers - have a look at Paul Irish's post

For modern browsers: get rid of .eot and .svg as it it only makes your rule more prone to syntax errors.

Example 2.1: font loading priority

@font-face {
  font-family: 'Dejavu SansWoff2';
  font-style: normal;
  font-weight: 400;
  src: url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff2"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype");
}

@font-face {
  font-family: 'Dejavu SansTTF';
  font-style: normal;
  font-weight: 400;
  src: 
  url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.ttf) format("truetype"),
  url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff2"),
    url(https://cdn.jsdelivr.net/gh/herrstrietzel/testfiles@main/fonts/dejavu/DejaVuSans.woff2) format("woff");
}

body{
  font-size:10vmin;
}


.dejavuTTF{
  font-family: 'Dejavu SansTTF';
}

.dejavuWoff2{
  font-family: 'Dejavu SansWoff2';
}
<p>Hamburgefonstiv</p>

<p class="dejavuWoff2">Hamburgefonstiv</p>
<p class="dejavuTTF">Hamburgefonstiv</p>

Local fonts or network resource

In short: always ditch the local() method as it is highly unreliable if you need to provide a consistent fonts rendering across all browsers. (Mainly it's unreliable because a font-family can have a plethora of different font names – depending on OS and font version).

Always check your DEV tools for rendered/loaded fonts

The font info should show a "network resource" in the computed styles view. Besides the network tab should show entries for loaded font files.
If not - you're seeing a locally installed font that won't be available on all devices.
Dev tools

network tab

Variable Fonts

When you're using a variable font you can reduce the number of @font-face rules. Usually you need at max 2 rules (one for regular and another for italic style)

/* latin */
@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 300 800;
  font-stretch: 75% 100%;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/opensans/v40/mem6YaGs126MiZpBA-UFUK0Zdc0.woff2) format('woff2');
}

/* latin */
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 300 800;
  font-stretch: 75% 100%;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/opensans/v40/mem8YaGs126MiZpBA-UFVZ0b.woff2) format('woff2');
}


body{
  font-family: 'Open Sans';
  font-size:10vmin;
}

.cnd{
  font-stretch:75%;
}
<p><em>Ham</em>Hamburge<strong>fonstiv</strong></p>
<p class="cnd"><em>Ham</em>Hamburge<strong>fonstiv</strong></p>

Note: We need to specify a font-variant range by adding 2 values – separated by a space:

font-weight: 100 1000;
font-stretch: 0% 200%;

Italic vs Oblique vs Slanted

Italic and Oblique refer to typographic classifications. An italic usually has distinctively different letter shapes - e.g a two storage "a" in regular (upright) style as opposed to a one storage design in a lot of fonts.
Oblique refers to a design concept where "italics" rather look like skewed letter designs – in fact they are still manually designed but similar to the upright design. font-style:oblique is rather obsolete – just use italic.

"slanted" is more relevant to variable fonts. Some fonts contain a seamless interpolation of the slanting angle that can be used as a replacement for italics

@font-face {
  font-family: 'Roboto Flex';
  font-style: oblique 0deg 10deg;
  font-weight: 100 1000;
  font-stretch: 25% 151%;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/robotoflex/v26/NaPLcZLOBv5T3oB7Cb4i0xG2bBVmtU5Wc7yZcSAuwtiL.woff2) format('woff2');
}


body{
  font-size:10vmin;
  font-family: 'Roboto Flex';
}

.slnt1{
  font-style: oblique 3deg
}

.slnt2{
  font-style: oblique 10deg
}
<p>Hamburgefonstiv</p>
<p class="slnt1">Hamburgefonstiv</p>
<p class="slnt2">Hamburgefonstiv</p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文