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

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


当前回答

@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");
}

其他回答

@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");
}

如果您使用ASP。NET,它很容易生成基于图像的字体,而不需要在服务器上安装(如添加到已安装的字体库)字体,通过使用:

PrivateFontCollection pfont = new PrivateFontCollection();
pfont.AddFontFile(filename);
FontFamily ff = pfont.Families[0];

然后用这种字体在图形上绘图。

只需简单地提供链接到实际的字体像这样,你就会很好

<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Montserrat'   rel='stylesheet'>
<style>
body {
font-family: 'Montserrat';font-size: 22px;
}
</style>
</head>
<body>

<h1>Montserrat</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>


</body>
</html>

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

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

如果你说的非标准字体,你指的是标准格式的自定义字体,下面是我怎么做的,它适用于我目前为止检查过的所有浏览器:

@font-face {
    font-family: TempestaSevenCondensed;
    src: url("../fonts/pf_tempesta_seven_condensed.eot") /* EOT file for IE */
}
@font-face {
    font-family: TempestaSevenCondensed;
    src: url("../fonts/pf_tempesta_seven_condensed.ttf") /* TTF file for CSS3 browsers */
}

所以你只需要TTF和eot字体。网上的一些工具可以进行转换。

但如果你想附加非标准格式的字体(位图等),我不能帮助你。