如何使用 CSS 包含字体 .ttf?

新手上路,请多包涵

我想为我的页面包含一个全局字体。我下载了一个 .ttf 文件,并将其包含在我的 CSS 中,但我的字体不会改变。

这是我的代码:

 @font-face {
    font-family: 'oswald';
    src: url('/font/oswald.regular.ttf');
}

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    font-size:100%;
    font:inherit;
    font-family: oswald;
    vertical-align:baseline
}

我哪里做错了?

原文由 Jerielle 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 891
2 个回答

只为 webfont 提供 .ttf 文件不足以支持跨浏览器。目前最好的组合是使用组合作为:

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

此代码假定您的 webfont 具有 .eot 、 .woff 、 .ttf 和 svg 格式。要自动执行所有这些过程,您可以使用: Transfonter.org

此外,现代浏览器正在转向 .woff 字体,因此您也可以这样做:

 @font-face {
    font-family: 'MyWebFont';
    src: url('myfont.woff') format('woff'), /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
         url('myfont.ttf') format('truetype'); /* Chrome 4+, Firefox 3.5, Opera 10+, Safari 3—5 */
}

在此处阅读更多内容:http: //css-tricks.com/snippets/css/using-font-face/


寻找浏览器支持: Can I Use fontface

原文由 Abhinav Gauniyal 发布,翻译遵循 CC BY-SA 4.0 许可协议

你试过格式吗?

 @font-face {
  font-family: 'The name of the Font Family Here';
  src: URL('font.ttf') format('truetype');
}

阅读这篇文章: http ://css-tricks.com/snippets/css/using-font-face/

此外,也可能取决于浏览器。

原文由 user3883419 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏