有没有一种方法可以在网站上添加一些自定义字体,而不使用图像,Flash或其他图形?

例如,我在一个婚礼网站上工作,我为这个主题找到了很多漂亮的字体。但是我找不到在服务器上添加那种字体的正确方法。我如何将CSS字体包含到HTML中?如果没有图像,这可能吗?


当前回答

这可以通过CSS来完成:

<style type="text/css">
@font-face {
    font-family: "My Custom Font";
    src: url(http://www.example.org/mycustomfont.ttf) format("truetype");
}
p.customfont { 
    font-family: "My Custom Font", Verdana, Tahoma;
}
</style>
<p class="customfont">Hello world!</p>

如果你使用TrueType-Fonts (TTF), Web Open Font Format (WOFF)或Embedded Opentype (EOT),所有常规浏览器都支持它。

其他回答

Safari和Internet Explorer都支持CSS @font-face规则,但是它们支持两种不同的嵌入式字体类型。Firefox计划在不久的将来支持与Apple相同的类型。SVG可以嵌入字体,但还没有得到广泛支持(没有插件)。

我认为我所见过的最可移植的解决方案是使用JavaScript函数将标题等替换为生成的图像,并使用你所选择的字体缓存在服务器上——这样你就可以简单地更新文本,而不必在Photoshop中进行处理。

这可以通过CSS来完成:

<style type="text/css">
@font-face {
    font-family: "My Custom Font";
    src: url(http://www.example.org/mycustomfont.ttf) format("truetype");
}
p.customfont { 
    font-family: "My Custom Font", Verdana, Tahoma;
}
</style>
<p class="customfont">Hello world!</p>

如果你使用TrueType-Fonts (TTF), Web Open Font Format (WOFF)或Embedded Opentype (EOT),所有常规浏览器都支持它。

@font-face {
font-family: "CustomFont";
src: url("CustomFont.eot");
src: url("CustomFont.woff") format("woff"),
url("CustomFont.otf") format("opentype"),
url("CustomFont.svg#filename") format("svg");
}

请参阅文章《美丽网页排版的50个有用的设计工具》以获得其他方法。

我只用过Cufon。我发现它很可靠,很容易使用,所以我一直用它。

它看起来只适用于ie浏览器,但是在谷歌上快速搜索“html嵌入字体”会得到http://www.spoono.com/html/tutorials/tutorial.php?id=19

如果你想保持平台无关性(你应该!),你就必须使用图像,或者使用标准字体。