简体   繁体   English

使用CSS3 / HTML5的自定义字体?

[英]Using a custom font with CSS3/HTML5?

I have this code in the beginning of my CSS stylesheet (linked to my index.html, of course): 我在CSS样式表的开头有这个代码(当然链接到我的index.html):

@font-face {
    font-family: "Calibri";
    src: local("Calibri"), local("Calibri"), url("fonts/Calibri-Bold.otf") format("truetype");
}

And i'm using: 我正在使用:

#id {
font-family: Calibri, Verdana, Arial, sans-serif;
}

But it still doesn't work. 但它仍然无效。 What's wrong with my code? 我的代码出了什么问题?

You have local("Calibri") repeated twice in your src property. 您在src属性中重复了两次local("Calibri")

Also, keep in mind that IE does not support local() so if you are viewing your site in IE, it won't load the font. 另外,请记住,IE不支持local()因此如果您在IE中查看您的站点,它将不会加载该字体。 On top of that, IE, to my knowledge, only supports the EOT format. 最重要的是,据我所知,IE仅支持EOT格式。

For an OTF font, use format("opentype") (you have "truetype"). 对于OTF字体,请使用format("opentype") (您有“truetype”)。

One more thing: If this is Microsoft's Calibri font, keep in mind that the license may not permit this type of use. 还有一件事:如果这是Microsoft的Calibri字体,请记住许可证可能不允许这种类型的使用。 I'm not familiar with the license so can't say for sure if this is the case. 我不熟悉许可证,所以不能确定是否是这种情况。

In general, the accepted code to use at the moment is this: 一般来说,目前使用的接受代码是:

@font-face {
  font-family: 'Graublau Web';
  src: url('GraublauWeb.eot');
  src: local('☺'),
    url("GraublauWeb.woff") format("woff"),
    url("GraublauWeb.otf") format("opentype"),
    url("GraublauWeb.svg#grablau") format("svg");
}

as suggested by Paul Irish: http://paulirish.com/2009/bulletproof-font-face-implementation-syntax/ 正如Paul Irish所建议的那样: http//paulirish.com/2009/bulletproof-font-face-implementation-syntax/

The smiley for local is because you can't be sure the user's local file is the one you intend to be (read the page for the details, it's an interesting read.) 本地的笑脸是因为你不能确定用户的本地文件是你想要的那个(阅读页面了解详情,这是一个有趣的阅读。)

已解决:它支持TTF(非OTF)文件。

Not an expert on the matter but you could try this tool here . 不是这方面的专家,但你可以在这里尝试这个工具。

It might at least help generate an example of what you should have. 它至少可以帮助您生成应该具有的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM