![](/img/trans.png)
[英]Is there a way to fade a Google Fonts web font on load instead of flashing once it's downloaded?
[英]How to use a downloaded font in a django project instead of google fonts api
我無法在任何地方找到解決我的問題的方法,所以我希望能得到一些幫助。 我是編程新手,也是 django 的新手。 我按照以下教程進行操作,直到第 4 部分開始。
到目前為止,一切都很好。 現在我在網上找到了一種我想使用的新字體,而不是教程使用的谷歌字體,用於頂部欄中的 django“徽標”(它稱為深紅色泡沫)。
問題是,我不知道如何在我的 base.html 文件中使用它以及如何處理我的 css 文件(我已經看到您顯然需要使用這些文件)。 我的 static 文件的組織方式如下:
-- static
|--fonts
| |--crimson_foam.ttf
|
|--css
|-- app2.css
|-- bootstrap files
我試過這樣:
模板/base.html
{% load static %}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}Django Boards{% endblock %}</title>
<link href="{% static 'fonts/crimson_foam.ttf' %}" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/app2.css' %}">
</head>
Rest of the html file
我嘗試了 2 個不同的.css 文件版本:
靜態/css/app2.css
版本 1:
title {
color:#FAB4FF;
font-family: "Crimson foam"; #didnt know what to put there
}
版本 2:
.navbar_brand {
font-family: "Crimson foam", regular;
}
老實說,我對 django 有多少了解讓我不知所措。 我知道有類似的線程,但他們不是在談論 html 基本文件,而且我還不夠先進,無法看到在哪里進行更改。
提前感謝大家閱讀這篇長文!
對於每個人,找到他的方式來解決我的問題,我找到了答案:
我的解決方案使用名為@font-face 的 css 方法。
為了達到想要的結果,我們必須將以下代碼添加到我們的 css 文件中:
css/app2.css
@font-face {
font-family: "Any name you want to give your font for further use";
src: url("either an absolute path from your homedirectory or a relative path");
}
# in my Case the code looked like this:
@font-face {
font-family: "Crimson Foam";
src: url("../fonts/crimson_foam.ttf);
}
這樣,您就可以將名稱為“Crimson Foam”的字體添加到可用的 fonts 池中。 您可以將許多@font-face 方法放在一個.css 文件中。
要使用剛剛聲明的字體名稱,您可以執行以下兩項操作之一:
html-tag.your_method_name {
font-family: "One of the fonts you assigned with @font-face", style (like regular or cursive);
font-size: A percantage (like 150%) or pixel like "36px";
}
# In my case, that looked like this:
h1.crimson_method {
font-family: Crimson Foam, regular;
font-size: 30px;
}
.method_name {
font-family: "";
font-size: "";
}
完成此操作后,您還必須在 html 文件的 class="something" 部分中提及您的方法名稱。 這可能看起來像這樣:
{% load static %}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title class="crimson_method">{% block title %}Django Boards{% endblock %}</title>
</head>
Rest of the html file
我在問題的 html 部分中使用的鏈接用於引用一個網站,我從該網站獲得了谷歌 fonts。 還有一種實現 google fonts 的方法,但是對於這個主題,在 web 上有很多像這樣的好教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.