Html 如何使用 CSS 包含字体 .ttf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24990554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include a font .ttf using CSS?
提问by Jerielle
I have a problem with my code. Because I want to include a global font for my page and I downloaded a .ttf file. And I include it in my main CSS but my font wont change.
我的代码有问题。因为我想为我的页面包含一个全局字体,所以我下载了一个 .ttf 文件。我将它包含在我的主 CSS 中,但我的字体不会改变。
Here's my simple code:
这是我的简单代码:
@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
}
I don't know where did I go wrong. Can you help me with this? Thanks.
我不知道我哪里出错了。你能帮我解决这个问题吗?谢谢。
回答by Abhinav Gauniyal
Only providing .ttf file for webfont won't be good enough for cross-browser support. The best possible combination at present is using the combination as :
仅为 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 */
}
This code assumes you have .eot , .woff , .ttf and svg format for you webfont. To automate all this process , you can use : Font-Squirrel : Web Font Generator.
此代码假设您有 .eot 、 .woff 、 .ttf 和 svg 格式的 webfont。要自动化所有这些过程,您可以使用:Font-Squirrel:Web 字体生成器。
Also , modern browsers are shifting towards .woff font , so you can probably do this too : :
此外,现代浏览器正在转向 .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 */
}
Read more here : http://css-tricks.com/snippets/css/using-font-face/
在这里阅读更多:http: //css-tricks.com/snippets/css/using-font-face/
Look for browser support : Can I Use fontface
寻找浏览器支持:我可以使用 fontface
回答by user3883419
Did you try format?
你试过格式化吗?
@font-face {
font-family: 'The name of the Font Family Here';
src: URL('font.ttf') format('truetype');
}
Read this article: http://css-tricks.com/snippets/css/using-font-face/
阅读这篇文章:http: //css-tricks.com/snippets/css/using-font-face/
Also, might depend on browser as well.
此外,也可能取决于浏览器。
回答by INTODAN
You can use font face like this:
您可以像这样使用字体:
@font-face {
font-family:"Name-Of-Font";
src: url("yourfont.ttf") format("truetype");
}
回答by BlakeWebb
I know this is an old post but this solved my problem.
我知道这是一个旧帖子,但这解决了我的问题。
@font-face{
font-family: "Font Name";
src: url("../fonts/font-name.ttf") format("truetype");
}
notice src:url("../fonts/font-name.ttf");
we use two periods to go back to the root directory and then into the fonts folder or wherever your file is located.
请注意,src:url("../fonts/font-name.ttf");
我们使用两个句点返回根目录,然后进入字体文件夹或文件所在的任何位置。
hope this helps someone down the line:) happy coding
希望这对某人有所帮助:) 快乐编码