如何使用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
}

我哪里错了?

387439 次浏览

你试过格式化了吗?

@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/

此外,还可能依赖于浏览器。

只为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/


查找浏览器支持:我可以使用FontFace吗

你可以像这样使用字体:

@font-face {
font-family:"Name-Of-Font";
src: url("yourfont.ttf") format("truetype");
}

我知道这是一个旧的职位,但这解决了我的问题。

@font-face{
font-family: "Font Name";
src: url("../fonts/font-name.ttf") format("truetype");
}

请注意,src:url("../fonts/font-name.ttf");,我们使用两个句点返回到根目录,然后返回到Fonts文件夹或您的文件所在的位置。

希望这能帮助一些人:)快乐编码

检查控制台以确保资源已加载。根据文件和文件夹结构,您的URL可能是错误的。请注意以下两者之间的区别:

src: url('/VT323/VT323-Regular.ttf') format('truetype');


src: url('VT323/VT323-Regular.ttf') format('truetype');


src: url('../VT323/VT323-Regular.ttf') format('truetype');