Roboto字体在CSS中无法使用

6

我有CSS和XHTML文件。我已下载了所有ROBOTO字体并将其放在我的“webapps/fonts/”文件夹中。

在我的XHTML中,我提到了CSS路径,

'<link href="../css/tab_ux.css" rel="stylesheet" type="text/css" />'

我的CSS文件中有以下样式:

@font-face {
   font-family:roboto-bold;
   src: url('../fonts/Roboto-Bold.tff') @ttf;
}

.UX_FontClass {
   font-family: roboto-bolditalic !important;
   font-size : 25px !important;
}

OutputText as styleClass="UX_FontClass"中提到了XHTML。

尽管字体在任何浏览器中都无法使用。我在代码中做错了什么?或者我漏掉了什么吗?

5个回答

6

你应该使用Google字体,它非常易于使用。

https://www.google.com/fonts#UsePlace:use/Collection:Robot

例如:

<head>
 <link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
</head>
<body>
<p style="font-family: 'Roboto', sans-serif;">Hello World</p>
</body>

1
如果你不信任链接,就像其他人一样,只需在谷歌中搜索“谷歌字体Roboto”。 - r33drum
只需记住字体名称周围的单引号,那就是我的问题所在... - thoni56

3
您正在使用自定义字体,因此需要添加一些字体类型格式,例如 ttfeotsvg,以适配 iPhone、iPad 设备。

注意:某些浏览器支持不同的字体类型,因此您需要添加 ttfsvgeot

@font-face {
    font-family: 'Roboto';
    src: url('Roboto-ThinItalic-webfont.eot');
    src: url('Roboto-ThinItalic-webfont.eot?#iefix') format('embedded-opentype'),
         url('Roboto-ThinItalic-webfont.woff') format('woff'),
         url('Roboto-ThinItalic-webfont.ttf') format('truetype'),
         url('Roboto-ThinItalic-webfont.svg#RobotoThinItalic') format('svg'); (under the Apache Software License). 
    font-weight: 200;
    font-style: italic;
}

记得在 UX_FontClass 类中添加以下代码。
.UX_FontClass {font-family: 'Roboto', Arial, Helevtica, sans-serif; }

所以我想下载.eot、.woff和.svg格式的Roboto字体,并将它们放在同一个文件夹中,对吗? - user3114967
不,目前有一些在线工具可以生成所有字体类型。你只需要将相同的字体类型放在同一个文件夹下即可。 - Kheema Pandey
你可以使用以下工具,它将为您提供所有字体格式:https://everythingfonts.com/font-face 或 http://www.fontsquirrel.com/tools/webfont-generator - Kheema Pandey
如果我的解决方案对您有用,那么您可以接受它并关闭此答案。 - Kheema Pandey

0
为什么不使用Google字体?
在您的HTML头部放置:
<link href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,100italic,300italic,400italic,500italic,500,700,700italic,900,900italic' rel='stylesheet' type='text/css'>

在你的 CSS 中使用:

font-family: 'Roboto', sans-serif;

出于安全考虑,我们不想公开此网页链接。您能否提供其他实现方式的建议? - user3114967
@user3114967 安全目的 - 如果你真的很关心这个问题,请使用https。 - jbutler483

0
错误在于在@font-face子句中定义了一个名为roboto-bold的字体,但后来尝试使用名为roboto-bolditalic的字体。这不是同一字体家族!
解决方案:确保名称匹配。
你可能是想说

font-family:'roboto-bold'; font-style:italic;

或者,既然你也在定义大小,你可以使用font缩写

font:italic 25px 'roboto-bold';

而且没有必要使用!important


-2

3
先生,OP不想使用谷歌字体库,因为存在安全问题。我不知道您的答案与其他提供相同解决方案的人有何不同。 - Kheema Pandey
@KheemaPandey 还得到了点赞。肯定是 Stack Overflow 的用户没有仔细阅读原帖的评论! - dippas
OP在问题中添加了[google-webfonts]标签,这可能导致了混淆。 - Mr Lister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接