html - 将字体添加到网站

标签 html css fonts

我正在尝试将 Ubuntu 字体添加到我的网站,我希望它在网站上通用,所以所有内容都使用该字体,但我首先将它添加到我的 h1 标签中看看它是否有效,但它不起作用。我已将字体上传到我的服务器。

到目前为止,这是我的 CSS:

@font-face {
    font-family: 'Ubuntu';
    src: url('ubuntu/Ubuntu-R.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

h1, h2, h3, h4, h5, h6 {
    font-family: 'Ubuntu', sans-serif;
    font-weight : normal;
    margin-top: 10px;
    letter-spacing: -1px;
}

h1 { 
    font-family: 'Ubuntu', sans-serif;
    color: #000;
    margin-bottom : 0.2em;
    font-size : 3em; /* 96 / 16 */
    line-height : 1.4;
}

像往常一样,我可能真的很愚蠢。我看过其他示例,但看不出我做错了什么。

最佳答案

这个问题可以通过两种方式解决。

您可以将 google font cdn 添加到您网站的 head 标签(如果已经托管)或者如果您有有效的互联网访问权限,那么您无需担心将其添加到本地文件夹中。

<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>

像这样

或者可能是你的文件路径位置不对

如果你的字体打包在根目录下,你可以很容易地调用它

src: url('Ubuntu-R.ttf') format('truetype');

但如果它在文件夹中(例如 ubuntu)

可以是src: url('ubuntu/Ubuntu-R.ttf') format('truetype');

但是如果在很长的路径中..尝试做这样的事情

src: url(../ubuntu/Ubuntu-R.ttf);

然后调用 font-family 属性,如果它位于本地文件夹中,则只调用“一个”名称。像这样

@font-face {
  font-family: Ubuntu;
  src: url(ubuntu/Ubuntu-R.ttf);
}

.myclass{
    font-family:Ubuntu; // The exact name for the @font-face
 }

希望对你有帮助

关于html - 将字体添加到网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30684675/

相关文章:

html - 使用 CSS 和 Span/A 标签连接到图形图像作为按钮

html - Height:100% 和 Margin 问题,延伸超过 100%

javascript - Material Design 缩放字体

javascript - 单击其中的 Bootstrap 切换按钮时如何禁用 anchor 标记单击

javascript - 如何在用户退出应用后自动登录到登录页面?

html - 使 div 元素透明,悬停时内容不透明

css - 如何在 JAVAFX 中调用存在于不同包中的 CSS 文件

Android TextView 与 WebView 字体渲染

ios - Xamarin Storyboard 中未显示自定义字体

javascript - 如何从 HTML 中选定项目的服务器检索数据