如果字体不可用,请用图像替换文本

发布于 2024-12-13 06:28:16 字数 754 浏览 5 评论 0原文

我有一个网页,它使用非网络安全字体(@font-face...)作为标题。

如果查看者的浏览器不支持此功能,这看起来会很糟糕,因此有没有办法检测此功能并在不支持的情况下用图像替换文本?

@font-face {
    font-family: 'awesomefont';
    src: url('fonts/awesomefont-webfont.eot');
    src: url('fonts/awesomefont-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/awesomefont-webfont.woff') format('woff'),
         url('fonts/awesomefont-webfont.ttf') format('truetype'),
         url('fonts/awesomefont-webfont.svg#awesomefont') format('svg');
    font-weight: normal;
    font-style: normal;
}

h1 {
    font-family: 'awesomefont'; /* Substitute with image if font not supported */
}

p {
    font-family: 'awesomefont', Arial, sans-serif; /* main body text can use alternative font */
}

I have a web page which uses a non-web-safe font (@font-face...) for a header.

This is going to look pretty bad if the viewer's browser does not support this, so is there a way to detect this capability and replace the text with an image if it is not supported?

@font-face {
    font-family: 'awesomefont';
    src: url('fonts/awesomefont-webfont.eot');
    src: url('fonts/awesomefont-webfont.eot?#iefix') format('embedded-opentype'),
         url('fonts/awesomefont-webfont.woff') format('woff'),
         url('fonts/awesomefont-webfont.ttf') format('truetype'),
         url('fonts/awesomefont-webfont.svg#awesomefont') format('svg');
    font-weight: normal;
    font-style: normal;
}

h1 {
    font-family: 'awesomefont'; /* Substitute with image if font not supported */
}

p {
    font-family: 'awesomefont', Arial, sans-serif; /* main body text can use alternative font */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生共白头 2024-12-20 06:28:16
@font-face {
  font-family: 'Nosifer Caps';
  font-style: normal;
  font-weight: 400;
  src: local('Nosifer Caps Regular'), local('NosiferCaps-Regular'), url('http://themes.googleusercontent.com/static/fonts/nosifercaps/v1/5Vh3eVJZ2pCbwAqfFmh1F3hCUOGz7vYGh680lGh-uXM.woff') format('woff');
}

p {
  font-family: 'Nosifer Caps', cursive; // is this not the point of defining a websafe font after your non-websafe-font????
}

或者你想总是为你的标题指定字体吗?在这种情况下,我什至不会经历处理自定义字体的麻烦。只需创建您的图像并 100% 地使用它即可。为一个标题图像加载整个字体库的开销是愚蠢的。

@font-face {
  font-family: 'Nosifer Caps';
  font-style: normal;
  font-weight: 400;
  src: local('Nosifer Caps Regular'), local('NosiferCaps-Regular'), url('http://themes.googleusercontent.com/static/fonts/nosifercaps/v1/5Vh3eVJZ2pCbwAqfFmh1F3hCUOGz7vYGh680lGh-uXM.woff') format('woff');
}

p {
  font-family: 'Nosifer Caps', cursive; // is this not the point of defining a websafe font after your non-websafe-font????
}

or do you want to ALWAYS have said font for your header.. in which case I wouldn't even go through the hassle of dealing with custom fonts. Just create your image and use that 100% of the time. The overhead of loading an entire font lib for one header image is just silly.

农村范ル 2024-12-20 06:28:16

我不确定是否有办法满足您的要求。但我通常使用 cufon 在我的网站上使用自定义字体。它使用画布从文本创建图像。如果浏览器不支持 javascript,它只会显示原始文本

http://cufon.shoqolate.com/generate/

I'm not sure if there is a way to do what you're asking. But I generally use cufon for using custom fonts on my websites. It uses canvas to create an image from the text. If a browser does not support javascript, it simply displays the raw text

http://cufon.shoqolate.com/generate/

桃扇骨 2024-12-20 06:28:16

一种选择是使用 JS 库之一来测量字符串的宽度来检测字体是否可用。不幸的是,JS 中不提供直接执行您想要的操作的功能。

以下是一些使用字符串测量技术的实现:

One option is to use one of the JS libraries which measure the width of the string to detect if the font is available. Unfortunately, functionality is not available in JS to do what you want directly.

Here are some implementations which use the string measurement technique:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文