Using a custom font in a Django project without relying on the Google Fonts API

I've hit a roadblock in my programming journey and I'm seeking help. As a newcomer to both programming and Django, I've been following a helpful tutorial up until part 4.

Everything was going well until I stumbled upon a new font online called Crimson Foam that I'd like to use for the Django "logo" in the top bar, instead of the Google font used in the tutorial.

The issue is, I'm unsure how to incorporate this font into my base.html file and what steps to take with my CSS files (I know they are required). Here's how my static files are organized:

-- static
 |--fonts
 | |--crimson_foam.ttf
 |
 |--css
   |-- app2.css
   |-- bootstrap files

I attempted the following setup:

templates/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

I experimented with two versions of the .css file:

static/css/app2.css

Version 1:

title {
    color:#FAB4FF;
    font-family: "Crimson foam";      #didnt know what to put there
}

Version 2:

.navbar_brand {
    font-family: "Crimson foam", regular;
}

To be honest, I am feeling overwhelmed by the vast amount of information related to Django. While there are similar threads discussing this topic, they don't specifically address the HTML base file, which is where I'm struggling. Any assistance would be greatly appreciated!

Thank you all in advance for taking the time to read through this lengthy explanation!

Answer №1

For anyone searching for the solution to my query, I have found an answer:

The technique I used involves a css method known as @font-face.

To achieve the desired outcome, we need to include the following code in our css file:

css/app2.css

@font-face {
    font-family: "Choose any name for your font";
    src: url("either an absolute path or a relative path from your directory");
}

# In my example, the code appeared like this:

@font-face {
    font-family: "Crimson Foam";
    src: url("../fonts/crimson_foam.ttf);
}

This way, you add the font named "Crimson Foam" to your list of available fonts. Multiple @font-face methods can be included in one .css file.

To use the newly declared font, you can do either of the following:

  1. Assign it to a specific html tag:
html-tag.your_method_name {
    font-family: "One of the fonts assigned with @font-face", style (such as regular or cursive);
    font-size: A percentage (e.g., 150%) or pixel value like "36px";
}

# In my case, it looked like this:

h1.crimson_method {
    font-family: Crimson Foam, regular;
    font-size: 30px;
}
  1. You can also create a font method without associating it with a specific tag, which may appear as follows:
.method_name {
    font-family: "";
    font-size: "";
}

Once you have done that, you must also reference your method name in your html file within the class="something" section. This could look like the following:

{% 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

The links mentioned in the html part of my question were used to link to a site where I obtained google fonts. There is also a method for incorporating google fonts, but there are numerous tutorials available on the web for that topic, such as this one.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to showcase data from input fields within a bootstrap modal dialog?

After the user has entered their details and clicks submit, I would like to present the information in a Bootstrap modal with a confirmation button below. This serves as a preview of the data before it is saved to the database. Here's what I have so ...

How can an input field with a checkbox type be properly placed within a table data tag in a table nested within an AngularJS modal?

Below is the code for my modal: <!-- modal for viewing permissions --> <div id="modal-user-permissions" class="modal"> <div class="modal-content"> <h4 id= ...

Troubleshooting problem with Ajax responseText

Can an ajax responseText be received without replacing the existing content? For instance: <div id="content"> <p>Original content</p> </div> Typically, after running an ajax request with a responseText that targets id="conten ...

Django is successfully loading CSS, but images and javascript files are not found with a 404 error

Currently working on a Django website project where I have successfully created a login page. However, upon testing it on the development server, I noticed that the CSS loads perfectly fine, but the images and JavaScript files are returning a 404 error, ev ...

Exploring XPath: strategies for excluding text within nested elements

if I come across some HTML structure like this: <div class=unique_id> <h1 class="parseasinTitle"> <span> Game Title </span> </h1> Game Developer </div> Is there a unique way to extract only the "Game Develo ...

Tips for adjusting the color of a row when hovering

Can I modify the row color on hover in material-table using props? Similar to this example. Appreciate your help. ...

I noticed a significant space below the footer on my webpage. As a first-time website creator, are there any solutions to this

I'm currently in the process of working on my very first website for a study project using HTML and CSS. There's an issue where there is a large gap below the footer that I can't seem to eliminate. I've only completed 3 pages so far, bu ...

Reset the queue for the slideDown animation using jQuery

I am experiencing an issue with my code where multiple animation effects are running simultaneously. Specifically, when I hover over navbarli1 and then move to another li element with the same navbarli1 class, the slide-down effect is not working as expect ...

Eliminate any additional spacing within the pre/code tags

I am currently utilizing prism.js for code highlighting. I have encountered an issue where there are unnecessary white spaces at the top and bottom of my output. You can view a live example here. <pre> <code class="language-css"> &lt ...

Is there a way to showcase every published WordPress page in a drop-down menu?

I am having trouble displaying all the published pages within my WordPress site as a dropdown list. Here is the code I have attempted to use: <div class="header-right"> <?php $pages = get_pages(); $posts = get_pages(array( ...

Streamlining the process of formatting URLs?

I was pondering if there is a more efficient method to accomplish this task. I extract links from a webpage, but many of them are relative. for example /index.html for instance /home.index.html Currently, my approach involves adding the home URL to compe ...

Challenges encountered with Material-UI elements

Attempting to implement the http://www.material-ui.com/#/components/drawer (Docked Example) component from Material-UI in conjunction with ReactJS. An error is encountered with the "=" sign in this line: handleToggle = () => this.setState({open: !this ...

What is the best way to align three images horizontally using bootstrap?

While working on my JavaScript class and creating one-page web apps, I had the idea to create a central hub to store and link them all together. Similar to Android or iOS layouts, I designed a page with icons that serve as links to each app. I managed to ...

What is the best way to integrate HTML templates into AngularJS?

I am currently working on developing an HTML fragment that will eventually function as an image slider. At this point, it only consists of a test div element: slider.html: <div>TEST</div> In order to incorporate this into my index.html, I hav ...

Is there a way to refresh only a specific div when clicked?

My preferred tech stack includes Django and Python. Here is the scenario: In models.py : class Contacts(models.Model): lastname = models.CharField(max_length=150) firstname = models.CharField(max_length=150) In views.py def home(request): ...

The total sum of various divs containing a mix of numbers and letters

I have a group of div elements with the same class, each containing a value in the format (X1), (X2),... I need to add up these numeric values only, like 1 + 2 + .... Since these divs are generated dynamically, I won't know how many there will be. How ...

Firefox won't trigger the `beforeunload` event unless I interact with the webpage by clicking on it

In my quest to handle the beforeunload event in Firefox, I've encountered a small hurdle. It seems to be working smoothly, but only if the user physically interacts with the page by clicking on it or entering text into an input field. Below is the co ...

The HTML code displays the changes in output after resizing the screen

Currently, I am utilizing canvas(graph) within my Angular JS UI5 application. My main goal is to increase the width of the graph. However, whenever I attempt to adjust the size of the Graph, it remains unchanged. Interestingly, when I shrink the screen, th ...

Adjusting the color of text based on the fulfillment of a condition

Is there a way to change the text color of a variable based on its value in PHP? For example, if the $status is set to admin, can we display it in red when echoed out using <h3>Status: <?php echo $status; ?></h3>? I'm not quite sure ...

When combining <a> with the class line-through, the line does not appear

I am currently utilizing a class to replace the deprecated strike in HTML: .entfall { text-decoration: line-through; } However, when using Firefox 38.x and the following code: <span class="entfall"><a href="some link">text</a></span ...