如何将外部字体加载到 HTML 文档中?
如何将外部字体文件加载到 HTML 文档中。
例子: 使用 HTML CSS 和/或 JAVASCRIPT 将同一目录中的 TTF 文件中的文本“blah blah blah blah blah blah blah”设为自定义字体
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
看看这篇一篇列出的文章。相关的 CSS 是:
以上内容适用于 Chrome/Safari/FireFox。正如 Paul D. Waite 在评论中指出的,如果将字体转换为 EOT 格式。
好消息是,这在旧版浏览器中似乎会优雅地降级,因此只要您意识到并接受并非所有用户都会看到相同字体的事实,就可以安全使用。
Take a look at this A List Apart article. The pertinent CSS is:
The above will work in Chrome/Safari/FireFox. As Paul D. Waite pointed out in the comments you can get it to work with IE if you convert the font to the EOT format.
The good news is that this seems to degrade gracefully in older browsers, so as long as you're aware and comfortable with the fact that not all users will see the same font, it's safe to use.
Paul Irish 有一种方法可以解决大多数常见问题。请参阅他的防弹@font-face文章:
最终变体,它可以阻止 IE 下载不必要的数据,并且可以在 IE8、Firefox、Opera、Safari 和 Chrome 中运行,如下所示:
他还链接到 生成器 它将把字体转换成您需要的所有格式。
正如其他人已经指定的那样,这仅适用于最新一代的浏览器。最好的选择是将其与 Cufon 之类的东西结合使用,并且仅在浏览器不支持
@font-face
时才加载 Cufon。Paul Irish has a way to do this that covers most of the common problems. See his bullet-proof @font-face article:
The final variant, which stops unnecessary data from being downloaded by IE, and works in IE8, Firefox, Opera, Safari, and Chrome looks like this:
He also links to a generator that will translate the fonts into all the formats you need.
As others have already specified, this will only work in the latest generation of browsers. Your best bet is to use this in conjunction with something like Cufon, and only load Cufon if the browser doesn't support
@font-face
.CSS3 提供了一种使用 @font-face 规则来实现这一点的方法。
http://www.w3.org/TR/css3 -webfonts/#the-font-face-rule
http://www.css3.info/preview/web-fonts-with-font-face/
这里有多种不同的方法,可以在不支持@font-face规则的浏览器中使用。
CSS3 offers a way to do it with the @font-face rule.
http://www.w3.org/TR/css3-webfonts/#the-font-face-rule
http://www.css3.info/preview/web-fonts-with-font-face/
Here is a number of different ways which will work in browsers that don't support the @font-face rule.
关于 Jay Stevens 的回答:“可在 HTML 文件中使用的字体必须存在于用户的计算机上并且可以通过 Web 浏览器访问,因此除非您想通过单独的外部进程将字体分发到用户的计算机,否则它可以没完没了。”这是真的。
但还有另一种使用 javascript / canvas / flash 的方法 - 非常好的解决方案给出了 cufon: http://cufon.shoqolate .com/generate/ 库,生成一个非常好用的外部字体方法。
Regarding Jay Stevens answer: "The fonts available to use in an HTML file have to be present on the user's machine and accessible from the web browser, so unless you want to distribute the fonts to the user's machine via a separate external process, it can't be done." That's true.
But there is another way using javascript / canvas / flash - very good solution gives cufon: http://cufon.shoqolate.com/generate/ library that generates a very easy to use external fonts methods.
如果您想支持除 CSS3 以外的更多浏览器,您可以查看开源库 cufon javascript 库
如果你想做更多时髦的事情,这里是API。
主要优点:允许您做您想做/需要的事情。
主要缺点:在某些浏览器中不允许进行文本选择,因此适合在标题文本上使用(但如果需要,您可以在所有网站中使用它)
If you want to support more browsers than the CSS3 fancy, you can look at the open source library cufon javascript library
And here is the API, if you want to do more funky stuff.
Major Pro: Allows you to do what you want / need.
Major Con: Disallows text selection in some browsers, so use is appropiate on header texts (but you can use it in all your site if you want)
Microsoft 有一种包含嵌入字体的专有 CSS 方法 (http: //msdn.microsoft.com/en-us/library/ms533034(VS.85).aspx),但可能不建议这样做。
我之前使用过 sIFR ,因为它效果很好 - 它使用 Javascript 和 Flash 来动态替换正常的某些 Flash 中的文本包含与您想要的字体相同的文本(该字体嵌入在 Flash 文件中)。这不会影响文本周围的标记(它通过使用 CSS 类来工作),您仍然可以选择文本,如果用户没有 Flash 或禁用了 Flash,它将优雅地降级为任何字体的文本您在 CSS 中指定(例如 Arial)。
Microsoft have a proprietary CSS method of including embedded fonts (http://msdn.microsoft.com/en-us/library/ms533034(VS.85).aspx), but this probably shouldn't be recommended.
I've used sIFR before as this works great - it uses Javascript and Flash to dynamically replace normal text with some Flash containing the same text in the font you want (the font is embedded in a Flash file). This does not affect the markup around the text (it works by using a CSS class), you can still select the text, and if the user doesn't have Flash or has it disabled, it will degrade gracefully to the text in whatever font you specify in CSS (e.g. Arial).
试试这个
Try this
我没有看到任何对 Raphael.js 的引用。所以我想我应该把它放在这里。 Raphael.js 向后兼容一直到 IE5 和非常早期的 Firefox 以及所有其他浏览器。可以时使用 SVG,不能时使用 VML。你用它所做的就是在画布上画画。有些浏览器甚至允许您选择生成的文本。 Raphael.js 可以在这里找到:
http://raphaeljs.com/
它可以像创建纸质绘图一样简单区域,指定字体、字体粗细、大小等...然后告诉它把文本字符串放到纸上。我不确定它是否绕过了许可问题,但它正在绘制文本,所以我相当确定它确实规避了许可问题。但请咨询您的律师以确定。 :-)
I did not see any reference to Raphael.js. So I thought I'd include it here. Raphael.js is backwards compatible all the way back to IE5 and a very early Firefox as well as all of the rest of the browsers. It uses SVG when it can and VML when it can not. What you do with it is to draw onto a canvas. Some browsers will even let you select the text that is generated. Raphael.js can be found here:
http://raphaeljs.com/
It can be as simple as creating your paper drawing area, specifying the font, font-weight, size, etc... and then telling it to put your string of text onto the paper. I am not sure if it gets around the licensing issues or not but it is drawing the text so I'm fairly certain it does circumvent the licensing issues. But check with your lawyer to be sure. :-)
作为另一种方式,您可以使用 Google Fonts CSS API。尝试在此处查找您的字体。
这是非常有用的东西。 Google Fonts 拥有庞大的免费字体库。您甚至可以请求单个字母进行优化。
As an extra way, you can use Google Fonts CSS API. Try to find your font here.
It's very useful thing. Google Fonts has a big library of free fonts. You can request even single letters for optimization.